99 Elm Problems/Problem 9/Solutions

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Solution 1: Recursive version

pack list =
    case list of
        [] -> []
        [ x ] -> [ [ x ] ]
        x :: xs ->
            case pack xs of
                [] -> []
                x' :: xs' ->
                    if List.member x x' then
                        (x :: x') :: xs'
                    else
                        [ x ] :: x' :: xs'