Skip to content

Instantly share code, notes, and snippets.

@naveensrinivasan
Created September 20, 2011 17:15
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 naveensrinivasan/1229698 to your computer and use it in GitHub Desktop.
Save naveensrinivasan/1229698 to your computer and use it in GitHub Desktop.
Pack consecutive duplicates of list elements into sublists.
//P09 Pack consecutive duplicates of list elements into sublists.
// https://sites.google.com/site/prologsite/prolog-problems/1
let packDups l =
let result = List.foldBack (
fun ele (flist,(mainlist:List<List<_>>)) ->
match (flist,mainlist) with
|([],_) -> (ele::flist,mainlist)
|(h::_,_) when ele = h -> ((ele::flist),mainlist)
|(_::_,[]) -> ([ele], [flist])
|(_::_,_) -> ([ele],flist::mainlist)
) l ([], [[]] )
fst(result)::snd(result)
packDups ["a";"a";"a";"a";"b";"c";"c";"a";"a";"d";"e";"e";"e";"e"]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment