Skip to content

Instantly share code, notes, and snippets.

@eduardoleon
Created August 29, 2024 09:38
Show Gist options
  • Save eduardoleon/6df6ece235d7de7bf9b17407fe89285e to your computer and use it in GitHub Desktop.
Save eduardoleon/6df6ece235d7de7bf9b17407fe89285e to your computer and use it in GitHub Desktop.
Split list into chunks of 2 or 3 elements
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 : []
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