Friday, December 9, 2011

99 bottles

Here's my version of this classic:
USING: combinators io kernel math
math.parser quotations sequences ;
IN: bottles

: bottle(s) ( n -- s ) 1 = "bottle " " bottles " ? ;

: (line) ( bottle(s) -- string )
dup {
{ 1 [ bottle(s) "1 " ] }
{ 0 [ bottle(s) "No more" ] }
[ bottle(s) swap number>string ]
} case swap "of beer" 3append ;

: (line-1) ( bottle(s) -- str )
(line) " on the wall" append ;

: line-1 ( bottle(s) -- )
(line-1) "," append print ;

: line-2 ( bottle(s) -- )
(line) "." append print ;

: line-3 ( bottle(s) -- )
0 = "Go to the store, buy some more,"
"Take one down, pass it around," ? print ;

: line-4 ( bottle(s) -- )
1 - dup 0 < [ drop 99 ] when
(line-1) "!\n" append print ;

: sing-lines ( x seq -- )
[ 1quotation ] map [ call( x -- ) ] with each ;

: sing-verse ( bottle(s) -- )
{ line-1 line-2 line-3 line-4 } sing-lines ;

: sing-song ( -- )
100 iota <reversed> [ sing-verse ] each ;

MAIN: sing-song

No comments:

Post a Comment