-
-
Save eduardoleon/6df6ece235d7de7bf9b17407fe89285e to your computer and use it in GitHub Desktop.
Split list into chunks of 2 or 3 elements
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
split :: [a] -> [[a]] | |
split [a,b,c,d] = [a,b] : [c,d] : [] | |
split (a:b:c:xs) = [a,b,c] : split xs | |
split [] = [] | |
split xs = 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
fun loop (xs, a :: b :: c :: d :: nil) = (c :: d :: nil) :: (a :: b :: nil) :: xs | |
| loop (xs, a :: b :: c :: x) = loop ((a :: b :: c :: nil) :: xs, x) | |
| loop (xs, nil) = xs | |
| loop (xs, x) = x :: xs | |
fun split xs = rev (loop (nil, xs)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment