Language Forth
(ANS standard)
Date: | 07/12/05 |
Author: | Ian Osgood |
URL: | n/a |
Comments: | 12 |
Info: | http://www.forth.org/ |
Score: | ![]() |
\ ANS Forth, well-factored :noname dup . ." bottles" ; :noname ." 1 bottle" ; :noname ." no more bottles" ; create bottles , , , : .bottles dup 2 min cells bottles + @ execute ; : .beer .bottles ." of beer" ; : .wall .beer ." on the wall" ; : .take ." Take one down and pass it around" ; : .verse .wall ." , " .beer ." ." cr 1- .take ." , " .wall ." ." cr ; : verses begin cr .verse ?dup 0= until ; 99 verses
Download Source | Write Comment
Download Source | Write Comment
Add Comment
Please provide a value for the fields Name,
Comment and Security Code.
This is a gravatar-friendly website.
E-mail addresses will never be shown.
Enter your e-mail address to use your gravatar.
Please don't post large portions of code here! Use the form to submit new examples or updates instead!
Comments
verses is ( n -- )
All the other words are ( n -- n )
Now, this isn't a weird language- Befunge, False, Brainf*ck, and Malbolge are weird ones. >
: MANY ( n) ?DUP IF . ELSE ." no more " THEN ; ( the number)
: BOTTLES ( n) ." bottle" 1 - IF ." s" THEN ; ( handle plural)
: BEER ( n) CR DUP MANY BOTTLES ." of beer" ;
: WALL ." on the wall" ;
: DRINK CR ." Take one down and pass it around" ;
: VERSE ( n) DUP BEER WALL DUP BEER DRINK 1- BEER WALL CR ;
: VERSES ( n) 1 SWAP DO I VERSE -1 +LOOP ;
I don't like putting dots in the names of words as it stops it reading nicely. FORTH naming conventions are all very well, but only when they help it to be more readable. In this case there is no confusion so no dots are needed.
I'm still inclined to use upper case for the code, partly because then the human comments stand out as being obviously different to the code. I also use techniques like right-justifying the comments so the right brackets line up neatly (e.g. for MANY and BOTTLES). It's more obvious when there are more comments.
DRINK is named DRINK. (haha!) Should I have called it TAKE? It's arguable either way. TAKE certainly matches the lyrics. Somehow, I prefer DRINK.
This is the full version, including "go to the store...". This makes it two lines longer since it needs ANOTHER and BUY. I've used the original wording (i.e. "store"
: MANY ( n) ?DUP IF . ELSE ." no more " THEN ; ( the number)
: BOTTLES ( n) ." bottle" 1 - IF ." s" THEN ; ( handle plural)
: BEER ( n) CR DUP MANY BOTTLES ." of beer" ;
: WALL ." on the wall" ;
: DRINK CR ." Take one down and pass it around." ;
: BUY CR ." Go to the store and buy some more." ;
: ANOTHER ( n-n) ?DUP IF DRINK 1- ELSE BUY 99 THEN ;
: VERSE ( n) DUP BEER WALL DUP BEER ANOTHER BEER WALL CR ;
: VERSES ( n) 0 SWAP DO I VERSE -1 +LOOP ;
Note that if you start with 99 bottles, you need ONE HUNDRED verses, to include the one which starts with "no more bottles". That means you need to start at 99 VERSE and go all the way down to 0 VERSE, hence the +LOOP includes 0.
example is the best. You should submit it with the comment (complete lyrics)