< 99 Elm Problems < Problem 9

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'
This article is issued from Wikibooks. The text is licensed under Creative Commons - Attribution - Sharealike. Additional terms may apply for the media files.