Skip to content

Instantly share code, notes, and snippets.

@merijn
Last active Jan 18, 2016
Embed
What would you like to do?
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