99 道 Elm 難題/問題 11
外觀
編寫一個函式以執行長度對列表進行編碼,但不用像問題 10 中使用元組一樣,而要定義一個數據型別,它可以表示單個元素或多個相同元素的執行。
import Html exposing (text)
import List
type Item a
= Single a
| Multiple Int a
runLengthEncode : List a -> List (Item a)
-- your implementation goes here
main = text <| toString <|
runLengthEncode ['a', 'a', 'a', 'b', 'c', 'c', 'd', 'e', 'e', 'e', 'e', 'e']
結果
[Multiple 3 'a', Single 'b', Multiple 2 'c', Single 'd', Multiple 5 'e']