-
-
Save CIAvash/a02ea4c1c52ae8d483add1984d42f352 to your computer and use it in GitHub Desktop.
Intersperse function in Perl 6 and Haskell
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
intersperse :: a -> [a] -> [a] | |
intersperse _ [] = [] | |
intersperse sep (x:xs) = x : prependToAll sep xs | |
prependToAll :: a -> [a] -> [a] | |
prependToAll _ [] = [] | |
prependToAll sep (x:xs) = sep : x : prependToAll sep xs |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
multi intersperse ($, []) { [] } | |
multi intersperse ($sep, [$x, **@xs]) { | |
$x, |prepend_to_all($sep, @xs) | |
} | |
multi prepend_to_all ($, []) { [] } | |
multi prepend_to_all ($sep, [$x, **@xs]) { | |
$sep, $x, |prepend_to_all($sep, @xs); | |
} | |
say intersperse 0, [1,2,3]; | |
say intersperse <X Y>, [<a b>, <c d>, <e f>]; | |
# say intersperse(0, 1..*); | |
# say intersperse(0, 1...*); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment