Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Forth Mac > Re: 99 bottles ...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 7 of 8 Topic 72 of 159
Post > Topic >>

Re: 99 bottles of beer

by ward@[EMAIL PROTECTED] (ward mcfarland) Jul 14, 2004 at 10:14 AM

Gnarlodious <gnarlodiousNULL@[EMAIL PROTECTED]
> wrote:

> http://www.99-bottles-of-beer.net/f.html#Forth

Cute, but buggy.

Try

1 .bottles .s

oops!  Nothing on the stack for the end of loop check in nbottles, so
make this change to avoid the underflow:

   ." Pass it around," CR
   1-
\  ?dup     \ WRONG !!!
   dup      \ RIGHT 
IF



But its still not quite right.  To see it fail, try:
 0 nbottles   ( or pass a negative # bottles)

Oops, that isn't what should result


So, we could make this change:

: nbottles ( n -- )
\  BEGIN  .bottles  ?dup NOT UNTIL
  BEGIN  .bottles  dup 0> not UNTIL drop


Now it doesn't infinite loop, but  0 nbottles gives this respose:

0 bottles of beer on the wall,
0 bottles of beer,
Take one down,
Pass it around,
-1 bottles of beer on the wall;

Which isn't right either.


I decided it wasn't well factored:  

..BOTTLES was displaying the initial state, decrementing the #bottles,
then redisplaying.  This is what was mishandling "0 nbottles".  



I decided to start over and after a few variants, I decided I like this
one:


\ 99 BOTTLES OF BEER REDUX

: ShowNumberBottles ( n -- )
   dup 0 >
   IF
      dup 1 =
      IF
         drop
         ." One bottle"
      ELSE
         . ." bottles"
      THEN
   ELSE
      drop
      ." No more bottles"
   THEN
   ."  of beer"
;

: DrinkAnotherBottle ( n -- n' )
\  also finishes displaying the stanza
\  dup 0 >      \ we can skip this, if we KNOW that n will be positive
\  IF
      cr dup ShowNumberBottles  [char] , emit
      cr ." Take "
      dup 1 > IF ." one" ELSE ." it" THEN
      ."  down, Pass it around,"
      1-            \ decrement N
      cr dup ShowNumberBottles  ."  on the wall."
\  THEN
;


: ShowFirstLine ( n -- )
   cr ShowNumberBottles  ."  on the wall"
;


: DRINK ( n -- )
   BEGIN
      dup 0 >
   WHILE
       cr
      dup   ShowFirstLine  [char] , emit
      DrinkAnotherBottle
   REPEAT
   drop
   ."  :("
;


: DRUNK ( -- )   \ the way WE used to sing it
   BEGIN
      99 drink
      cr
      0 ShowFirstLine ." :("
      0 ShowFirstLine ." :("
      cr ." Let's get up and order some more!"
      cr
      99 ShowFirstLine ." :)"
      cr cr
   AGAIN
;

99 DRINK
 




 8 Posts in Topic:
99 bottles of beer
Gnarlodious <gnarlodio  2004-07-13 20:50:23 
Re: 99 bottles of beer
Gregory Weston <gwesto  2004-07-14 00:02:40 
Re: 99 bottles of beer
Gnarlodious <gnarlodio  2004-07-14 00:05:59 
Re: 99 bottles of beer
Gregory Weston <gwesto  2004-07-14 01:39:21 
Re: 99 bottles of beer
Doug Hoffman <dhoffman  2004-07-14 10:04:39 
Re: 99 bottles of beer
Ed Williams <cedward@[  2004-07-14 09:20:50 
Re: 99 bottles of beer
ward@[EMAIL PROTECTED] (  2004-07-14 10:14:52 
Re: 99 bottles of beer
Doug Hoffman <dhoffman  2004-07-14 22:00:22 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 16:50:10 CDT 2008.