文字列を長さ1の文字列のリストに分割したい(Haskell編)

2016年11月19日
1 分

以前、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 &#096Char -> b0' with actual type &#096[a0]'
    In the first argument of &#096map', namely &#096([])'
    In the expression: map ([]) "abcdefg"
    In an equation for &#096it': it = map ([]) "abcdefg"
</pre>

ダメだった。
いや、空リストに cons すればいいのか。
<pre>
Prelude> map (:[]) "abcdefg"
["a","b","c","d","e","f","g"]
</pre>

出来た。