Skip to content

Instantly share code, notes, and snippets.

@joneshf
Created May 8, 2014 23:54
Show Gist options
  • Save joneshf/cbdf67774cf4a348229a to your computer and use it in GitHub Desktop.
Save joneshf/cbdf67774cf4a348229a to your computer and use it in GitHub Desktop.
module Foo where
groupBy :: forall a. (a -> a -> Boolean) -> [a] -> [[a]]
groupBy _ [] = []
groupBy eq (x:xs) = case span (eq x) xs of
{init = ys, rest = zs} -> (x:ys) : groupBy eq zs
span :: forall a. (a -> Boolean) -> [a] -> { init :: [a], rest :: [a] }
span p (x:xs') | p x = case span p xs' of {init = ys, rest = zs} -> {init: (x:ys), rest: zs}
span _ xs = {init: [], rest: xs}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment