Jump to content

99 Elm Problems/Problem 10/Solutions

From Wikibooks, open books for an open world

Solution 1: Recursive version

runLengthEncode list =
    case list of
        [] -> []
        [ x ] -> [ (x, 1) ]
        x :: xs ->
            case runLengthEncode xs of
                [] -> []
                (x', n) :: xs' ->
                    if x == x' then
                        (x, n + 1) :: xs'
                    else
                        ( x, 1 ) :: (x', n) :: xs'