Skip to content

Instantly share code, notes, and snippets.

@dgfitch
Created November 3, 2010 15:50
Show Gist options
  • Save dgfitch/661247 to your computer and use it in GitHub Desktop.
Save dgfitch/661247 to your computer and use it in GitHub Desktop.
F# splitting a list into a list of lists
// there have got to be better ways to do this?
let splitList (f:'a -> bool) (list:'a list) =
let op (nested:'a list list) (thing: 'a) =
if f thing then
[] :: nested
else
match nested with
| [] -> [[thing]]
| headlist::rest -> (thing :: headlist) :: rest
let result = List.fold op [] list
result |> List.map List.rev |> List.rev
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment