转到内容

99 道榆木问题/第 10 道问题/解决方案

来自维基文库,面向开放世界的开放书籍

解决方案 1:递归版本

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'
华夏公益教科书