Haskellでリフル・シャッフル
こないだのリフル・シャッフルを Haskell でやってみた。
Prelude> concat $ zip [0,2,4,6,8] [1,3,5,7,9]
:2:10:
Couldn't match type `(a1, b0)' with `[a0]'
Expected type: [[a0]]
Actual type: [(a1, b0)]
In the return type of a call of `zip'
In the second argument of `($)', namely
`zip [0, 2, 4, 6, ....] [1, 3, 5, 7, ....]'
In the expression:
concat $ zip [0, 2, 4, 6, ....] [1, 3, 5, 7, ....]
あれ。そうか、zip はタプルのリストを返すんだっけ。
Prelude> zip [0,2,4,6,8] [1,3,5,7,9]
[(0,1),(2,3),(4,5),(6,7),(8,9)]
じゃあ、foldr を使ってみようか。
Prelude> foldr (\(x,y) acc -> x:y:acc) [] $ zip [0,2,4,6,8] [1,3,5,7,9]
[0,1,2,3,4,5,6,7,8,9]
うまくいった。
いや、zipWith のほうがいいか?
Prelude> concat $ zipWith (\x y -> x:y:[]) [0,2,4,6,8] [1,3,5,7,9]
[0,1,2,3,4,5,6,7,8,9]
