n文字の文字列を全部出力するコマンド

30分プログラム、その709。n文字の文字列を全部出力するコマンドを作ってみました。

ふと、2文字のTwitterIDが空いていないか調べてみたくなりました。で、そのための準備として、aa、ab、...、zzという文字列を生成するコマンドを作りました。

あとは、これをcurlとかを組合せれば、IDの存在チェックができるはずです。

使い方

$ escript nChars.erl 1
a
b
c
d
e
...

$ escript nChars.erl 2
aa
ab
ac
ad
ae
af
ag
ah
ai
aj
...

ソースコード

-module(nChars).
-compile([export_all]).

nChars(0) -> [""];
nChars(N) ->
    [ [H|T] || H <- lists:seq($a,$z), T <- nChars(N-1) ].

main([A]) ->
    N = list_to_integer(A),
    lists:foreach(fun(S)-> io:format("~s~n",[S]) end,
    		  nChars(N)).