< 99 Elm Problems < Problem 10

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