How to resolve the algorithm 99 bottles of beer step by step in the Forth programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm 99 bottles of beer step by step in the Forth programming language
Table of Contents
Problem Statement
Display the complete lyrics for the song: 99 Bottles of Beer on the Wall.
The lyrics follow this form: ... and so on, until reaching 0 (zero). Grammatical support for 1 bottle of beer is optional. As with any puzzle, try to do it in as creative/concise/comical a way as possible (simple, obvious solutions allowed, too).
Let's start with the solution:
Step by Step solution about How to resolve the algorithm 99 bottles of beer step by step in the Forth programming language
Source code in the forth programming language
: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, pass it around" ;
: .verse .wall cr .beer cr
1- .take cr .wall cr ;
: verses begin cr .verse ?dup 0= until ;
99 verses
DECIMAL
: BOTTLES ( n -- )
DUP
CASE
1 OF ." One more bottle " DROP ENDOF
0 OF ." NO MORE bottles " DROP ENDOF
. ." bottles " \ DEFAULT CASE
ENDCASE ;
: , [CHAR] , EMIT SPACE 100 MS CR ;
: . [CHAR] . EMIT 300 MS CR CR CR ;
: OF ." of " ; : BEER ." beer " ;
: ON ." on " ; : THE ." the " ;
: WALL ." wall" ; : TAKE ." take " ;
: ONE ." one " ; : DOWN ." down, " ;
: PASS ." pass " ; : IT ." it " ;
: AROUND ." around" ;
: POPONE 1 SWAP CR ;
: DRINK POSTPONE DO ; IMMEDIATE
: ANOTHER S" -1 +LOOP" EVALUATE ; IMMEDIATE
: HOWMANY S" I " EVALUATE ; IMMEDIATE
: ONELESS S" I 1- " EVALUATE ; IMMEDIATE
: HANGOVER ." :-(" CR QUIT ;
: BEERS ( n -- ) \ Usage: 99 BEERS
POPONE
DRINK
HOWMANY BOTTLES OF BEER ON THE WALL ,
HOWMANY BOTTLES OF BEER ,
TAKE ONE DOWN PASS IT AROUND ,
ONELESS BOTTLES OF BEER ON THE WALL .
ANOTHER
HANGOVER ;
2 beers
2 bottles of beer on the wall,
2 bottles of beer ,
take one down, pass it around,
One more bottle of beer on the wall.
One more bottle of beer on the wall,
One more bottle of beer ,
take one down, pass it around,
No more bottles of beer on the wall.
ok
You may also check:How to resolve the algorithm Binary search step by step in the PL/I programming language
You may also check:How to resolve the algorithm User input/Text step by step in the Frink programming language
You may also check:How to resolve the algorithm Keyboard input/Keypress check step by step in the Perl programming language
You may also check:How to resolve the algorithm Input loop step by step in the Jsish programming language
You may also check:How to resolve the algorithm Globally replace text in several files step by step in the Factor programming language