Skip to content

Instantly share code, notes, and snippets.

@ashaindlin
Created December 7, 2014 16:32
Show Gist options
  • Save ashaindlin/3394f7b6923fa03e92ea to your computer and use it in GitHub Desktop.
Save ashaindlin/3394f7b6923fa03e92ea to your computer and use it in GitHub Desktop.
groupBy with a fold
myGroupBy :: (a -> a -> Bool) -> [a] -> [[a]]
myGroupBy p xs = foldr f v xs
where
v = []
f x ys = if (length ys > 0 && p x (head (head ys)))
then (x:(head ys)):(tail ys)
else [x]:ys
@ashaindlin
Copy link
Author

Compare with the official definition. Honestly, I don't think this is too bad.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment