文字列を長さ1の文字列のリストに分割したい(Haskell編)
以前、Scheme では書いた。 cf. 文字列を長さ1の文字列のリストに分割したい - blog.PanicBlanket.com 今日は Haskell でやってみよう。
Prelude> map (\ c -> [c]) "abcdefg"
["a","b","c","d","e","f","g"]
というわけで、とりあえず出来たわけだけどなんだか冗長できれいじゃない。 こんなふうじゃダメなんだろうか。
Prelude> map ([]) "abcdefg"
<interactive>:3:6:
Couldn't match expected type `Char -> b0' with actual type `[a0]'
In the first argument of `map', namely `([])'
In the expression: map ([]) "abcdefg"
In an equation for `it': it = map ([]) "abcdefg"
</pre>
ダメだった。
いや、空リストに cons すればいいのか。
<pre>
Prelude> map (:[]) "abcdefg"
["a","b","c","d","e","f","g"]
</pre>
出来た。
