Created
September 20, 2011 17:15
-
-
Save naveensrinivasan/1229698 to your computer and use it in GitHub Desktop.
Pack consecutive duplicates of list elements into sublists.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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