99 Bottles of Beer

30分プログラム、その537。"99 Bottles of Beer"を出力してみよう。
wikipedia:HQ9+のページを見ていたら、

9コマンドは『99 Bottles of Beer』(アメリカの数え歌で、プログラミングの例題でよく利用される)の歌詞を出力する。

と書いてあった。

よし、例題でよく出力されるならやってみよう。

使い方

1> bottles:print(99).
99 Bottles of beer on the wall
99 Bottles of beer
Take one down and pass it around

98 Bottles of beer on the wall
98 Bottles of beer
Take one down and pass it around

...

2 Bottles of beer on the wall
2 Bottles of beer
Take one down and pass it around

1 Bottles of beer on the wall
1 Bottles of beer
Take one down and pass it around

0 Bottles of beer on the wallok

ソースコード

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

bottle(0) ->
    "0 Bottles of beer on the wall";
bottle(N) ->
    S =io_lib:format(
	 "~p Bottles of beer on the wall\n"
	 "~p Bottles of beer\n"
	 "Take one down and pass it around\n\n",
	 [N,N]),
    [S|bottle(N-1)].

print(N) ->
    io:fwrite(bottles:bottle(N),[]).