はじめに
Ninety-nine Problems, Solved in Elm を毎日ふれることで、Elm に親しもうという『日記』です。詳細は 99 PSiE [Problem 1] をご覧ください。
問題
Decompress the run-length encoded list generated in Problem 11.
(引用元:Problem 12)
成果物
import Html
type RleCode a = Run Int a | Single a
-- my solution
rleDecode_ listOfRleCode = List.map fromRleCode listOfRleCode |> List.concat
fromRleCode rleCode =
case rleCode of
Run num b -> List.repeat num b
Single b -> [b]
-- main = Html.text
-- <| Debug.toString
-- <| rleDecode
-- <| [Run 4 1, Single 2, Run 2 5, Single 2, Single 1]
-- Their solution
rleDecode listOfRleCode = List.concatMap fromRleCode listOfRleCode
-- answer check
main : Html.Html a
main =
Html.text
<| case test of
0 ->
"Your implementation passed all tests."
1 ->
"Your implementation failed one test."
x ->
"Your implementation failed " ++ Debug.toString x ++ " tests."
test : Int
test =
List.length
<| List.filter ((==) False)
[ rleDecode [ Run 4 1, Single 2, Run 2 5, Single 2, Single 1 ]
== [ 1, 1, 1, 1, 2, 5, 5, 2, 1 ]
, rleDecode [ Run 4 1, Single 2, Run 2 5, Single 2, Single 1 ]
== [ 1, 1, 1, 1, 2, 5, 5, 2, 1 ]
, rleDecode [ Run 4 "1", Single "b", Run 2 "5", Single "2", Single "a" ]
== [ "1", "1", "1", "1", "b", "5", "5", "2", "a" ]
]
正答例
コメント
へぇ~、List.concatMap ってこんな感じで使うのかー。