Discussion:
99 bottles of beer
(too old to reply)
Gnarlodious
2004-07-13 20:50:23 UTC
Permalink
http://www.99-bottles-of-beer.net/f.html#Forth
Gregory Weston
2004-07-14 00:02:40 UTC
Permalink
Post by Gnarlodious
http://www.99-bottles-of-beer.net/f.html#Forth
Great page. I've contributed one of the longest entries there.
--
Standard output is like your butt. Everyone has one. When using a bathroom,
they all default to going into a toilet. However, a person can redirect his
"standard output" to somewhere else, if he so chooses. - Jeremy Nixon
Gnarlodious
2004-07-14 00:05:59 UTC
Permalink
Post by Gregory Weston
Post by Gnarlodious
http://www.99-bottles-of-beer.net/f.html#Forth
Great page. I've contributed one of the longest entries there.
Which one? I even found a Mops entry. Great site!


-- Gnarlie
Ignoranus: a person who is both stupid AND an asshole
Gregory Weston
2004-07-14 01:39:21 UTC
Permalink
Post by Gnarlodious
Post by Gregory Weston
Post by Gnarlodious
http://www.99-bottles-of-beer.net/f.html#Forth
Great page. I've contributed one of the longest entries there.
Which one? I even found a Mops entry. Great site!
-- Gnarlie
Ignoranus: a person who is both stupid AND an asshole
Centura's SQL Windows. It's listed under 'C' because nobody can tell
where the company name ends and the product name begins (largely because
they keep changing that point). The vast majority of the entry as posted
is boilerplate crud that every Centura program will have.
--
Standard output is like your butt. Everyone has one. When using a bathroom,
they all default to going into a toilet. However, a person can redirect his
"standard output" to somewhere else, if he so chooses. - Jeremy Nixon
Doug Hoffman
2004-07-14 10:04:39 UTC
Permalink
Post by Gnarlodious
Post by Gregory Weston
Post by Gnarlodious
http://www.99-bottles-of-beer.net/f.html#Forth
Great page. I've contributed one of the longest entries there.
Which one? I even found a Mops entry. Great site!
Unfortunately it's a pretty poor example of Mops code.

There's an instance variable ( n ) which never gets used. A record{ .. }
declaration for no purpose. Private and Public method declarations for no
apparent reason. In my opinion creating a class/object to solve this
problem was not only unneccessary but it got in the way. Non-OOP fans would
love to see this object code to prove their point that OOP has no value
added.

My .02

-Doug
Ed Williams
2004-07-14 13:20:50 UTC
Permalink
Post by Doug Hoffman
Post by Gnarlodious
Post by Gregory Weston
Post by Gnarlodious
http://www.99-bottles-of-beer.net/f.html#Forth
Great page. I've contributed one of the longest entries there.
Which one? I even found a Mops entry. Great site!
Unfortunately it's a pretty poor example of Mops code.
There's an instance variable ( n ) which never gets used. A record{ .. }
declaration for no purpose. Private and Public method declarations for no
apparent reason. In my opinion creating a class/object to solve this
problem was not only unneccessary but it got in the way. Non-OOP fans would
love to see this object code to prove their point that OOP has no value
added.
My .02
-Doug
Nah, that's worth .25 nowadays, what with inflation and all.

Ed W.
ward mcfarland
2004-07-14 14:14:52 UTC
Permalink
Post by Gnarlodious
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
Doug Hoffman
2004-07-14 22:00:22 UTC
Permalink
Ward presents an excellent example of Forth coding (and proper thought
processes while doing so).

No OOP required.
Post by ward mcfarland
Post by Gnarlodious
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
." Pass it around," CR
1-
\ ?dup \ WRONG !!!
dup \ RIGHT
IF
0 nbottles ( or pass a negative # bottles)
Oops, that isn't what should result
: nbottles ( n -- )
\ BEGIN .bottles ?dup NOT UNTIL
BEGIN .bottles dup 0> not UNTIL drop
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.
.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
\ 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
Loading...