Skip to content

Instantly share code, notes, and snippets.

@merijn
Last active January 18, 2016 14:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save merijn/c163cc106fd245d1cf2e to your computer and use it in GitHub Desktop.
Save merijn/c163cc106fd245d1cf2e to your computer and use it in GitHub Desktop.
Efficient split in half
splitInHalf :: [a] -> ([a], [a])
splitInHalf [] = ([], [])
splitInHalf xs = go id xs xs
where
go :: ([b] -> [b]) -> [b] -> [b] -> ([b], [b])
go f xs [] = (f [], xs)
go f xs [_] = (f [], xs)
go f (x:xs) (_:_:ys) = go (f . (x:)) xs ys
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment