Voting

Category

real language

Bookmarking

Del.icio.us Digg Diigo DZone Earthlink Google Kick.ie
Windows Live LookLater Ma.gnolia Reddit Rojo StumbleUpon Technorati

Language Forth

(ANS standard)

Date:07/12/05
Author:Ian Osgood
URL:n/a
Comments:12
Info:http://www.forth.org/
Score: (3.03 in 289 votes)
\ 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

Alternative Versions

VersionAuthorDateCommentsRate
original versionDan Reish04/20/050

Comments

>>  Commentifier said on 08/06/06 16:43:56

Commentifier What, no stack comments??

>>  Ian Osgood said on 08/08/06 19:05:29

Ian Osgood You got me...

verses is ( n -- )

All the other words are ( n -- n )

>>  JPN said on 12/01/06 17:57:11

JPN Very elegant, and very much "in the spirit of Forth."

>>  Michael said on 09/29/07 14:24:03

Michael Man, Forth is weird.

>>  Frank Earl (aka Svartalf) said on 10/27/07 18:04:14

Frank Earl (aka Svartalf) Forth is no weirder than many human languages- it was designed to be expressive and concise at the same time. This is nice and clean; ColorForth would be even more compact, I'd suspect, but be readable (well, to a Forth programmer...) all the same.

Now, this isn't a weird language- Befunge, False, Brainf*ck, and Malbolge are weird ones. >:-)

>>  Serial Drinker said on 12/16/07 01:57:07

Serial Drinker Very cool, but doesn't output the final "Go to the store..." sentence.

>>  Dugeen said on 02/22/08 16:17:29

Dugeen Sheer beauty

>>  JD said on 04/21/08 07:56:55

JD This is sweet and compact. However, I never get to go to the store and refill my cooler.

>>  Johan said on 04/12/09 01:09:54

Johan colorForth wouldn't be more compact - it lacks the ." word.

>>  Arf Dysg said on 04/07/10 18:45:02

Arf Dysg I've reduced it from 11 lines to 7 lines. I've also factored it differently.



: 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.

>>  Arf Dysg said on 04/07/10 18:58:13

Arf Dysg
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";) since it rhymes, although we Brits would say "shop".


: 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.

>>  barrym said on 07/17/10 05:37:27

barrym Arf, my brain has a difficult time grokking Forth, but it appears to me that your
example is the best. You should submit it with the comment (complete lyrics) :-)

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!

Name:

eMail:

URL:

Security Code:
  
Comment: