进入内容

99 道 Elm 问题/问题 11/解决方案

来自维基百科,一个开放世界的开放书籍

解决方案 1:递归版本

runLengthEncode list =
    case list of
        [] -> []
        [ x ] -> [ Single x ]
        x :: xs ->
            case runLengthEncode xs of
                [] -> []
                Single x' :: xs' ->
                    if x' == x then
                        Multiple 2 x' :: xs'
                    else
                        Single x :: Single x' :: xs'
                Multiple n x' :: xs' ->
                    if x' == x then
                        Multiple (n + 1) x' :: xs'
                    else
                        Single x :: Multiple n x' :: xs'
华夏公益教科书