Page 2 of 7
Re: CS-101 problems and their FORTH solutions.
Posted: Mon Jul 31, 2017 5:21 pm
by Martin_H
I watched this video this morning
https://www.youtube.com/watch?v=QPZ0pIK_wsc and realized it was the perfect example of a CS-101 Forth problem. So below is my first attempt.
Code: Select all
: FIZZ? ( n -- flag)
3 MOD 0= ;
: BUZZ? ( n -- flag)
5 MOD 0= ;
: .FIZZ ( n -- flag)
FIZZ? DUP
IF ." Fizz " THEN ;
: .BUZZ ( n -- flag)
BUZZ? DUP
IF ." Buzz " THEN ;
: FIZZBUZZ ( --)
101 1 DO
I .FIZZ I .BUZZ
OR 0= IF I . THEN
CR
LOOP ;
FIZZBUZZ
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 4:54 am
by barrym95838
Stack comments?
Mike B.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 4:58 am
by nyef
A variation, tested in gforth; the use of MOD is probably painful on some implementations:
Code: Select all
: .FIZZ ." Fizz " DROP ;
: .BUZZ ." Buzz " DROP ;
: .FIZZBUZZ ." Fizzbuzz " DROP ;
HERE
' .FIZZBUZZ , ' . , ' . , ' .FIZZ , ' . ,
' .BUZZ , ' .FIZZ , ' . , ' . , ' .FIZZ ,
' .BUZZ , ' . , ' .FIZZ , ' . , ' . ,
CONSTANT BEES
: FIZZBUZZ
101 1 DO
I BEES I 15 MOD CELLS + @ EXECUTE
CR
LOOP ;
FIZZBUZZ
This is somewhat off-the-cuff, so I didn't bother trying to come up with a better name than "BEES" for the fizzing, buzzing array of execution tokens.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 12:29 pm
by Martin_H
I updated the program, but I'm not sure how valuable they are given that all functions except the main one have the same stack footprint.
@nyef, that's a really Forthy Forth program.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 2:47 pm
by nyef
@nyef, that's a really Forthy Forth program.
Thanks, I think?
Ordinarily I would not have used a DO loop, it simply isn't part of my normal mental toolkit (or even implemented in my current Forth), but since I started by hacking up a given example, I just left the iteration control alone.
Is it at least obvious how and why it works?
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 3:05 pm
by Martin_H
Is it at least obvious how and why it works?
It looks like you are indexing into a list of execution tokens based upon the remainder. The token determines the output, and most of them are the TOS, but the exceptions output text and drop TOS. It's extensible just by changing the execution token table.
I called it forthy since it uses a number some of the more powerful features of Forth that a novice wouldn't know. I am tempted to rework it using Create Does> which should do the same thing, but bury the complexity.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 3:07 pm
by barrym95838
To me it's obvious, in a kinky sort of way.

You're using a custom-tailored constant array of execution tokens that works great for Fizz=3 and Buzz=5, but it could get a bit unwieldy if you were later tasked to revise it with more "buzz-words". It appears to be efficient in what it does.
Mike B.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 3:39 pm
by Martin_H
I changed nyef's program to use create does> to bury the access to the address of BEES.
Code: Select all
: .FIZZ ( unused --)
." Fizz " DROP ;
: .BUZZ ( unused --)
." Buzz " DROP ;
: .FIZZBUZZ ( unused --)
." Fizz Buzz " DROP ;
CREATE BEES
' .FIZZBUZZ , ' . , ' . , ' .FIZZ , ' . ,
' .BUZZ , ' .FIZZ , ' . , ' . , ' .FIZZ ,
' .BUZZ , ' . , ' .FIZZ , ' . , ' . ,
DOES> ( i addr --)
SWAP TUCK
15 MOD CELLS + @ EXECUTE ;
: FIZZBUZZ
101 1 DO
I BEES
CR
LOOP ;
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 3:50 pm
by nyef
It looks like you are indexing into a list of execution tokens based upon the remainder. The token determines the output, and most of them are the TOS, but the exceptions output text and drop TOS. It's extensible just by changing the execution token table.
Yes, that's the mechanism.
I called it forthy since it uses a number some of the more powerful features of Forth that a novice wouldn't know. I am tempted to rework it using Create Does> which should do the same thing, but bury the complexity.
I... fail to see how CREATE DOES> would help here? We're trying to make one word with a specific behaviour, not multiple words with similar (parameterizable) behaviour.
Code: Select all
CREATE BEES
' .FIZZBUZZ , ' . , ' . , ' .FIZZ , ' . ,
' .BUZZ , ' .FIZZ , ' . , ' . , ' .FIZZ ,
' .BUZZ , ' . , ' .FIZZ , ' . , ' . ,
DOES> ( i addr --)
SWAP TUCK
15 MOD CELLS + @ EXECUTE ;
DOES> outside of a colon definition is not defined by the ANS standard. Easy enough to implement, now that I'm thinking about it, but not defined by the standard.
To me it's obvious, in a kinky sort of way. :D You're using a custom-tailored constant array of execution tokens that works great for Fizz=3 and Buzz=5, but it could get a bit unwieldy if you were later tasked to revise it with more "buzz-words". It appears to be efficient in what it does.
Yes, it could get unwieldy very quickly, but since the sequence repeats every ( 3 5 * => 15 ) elements this was easy enough. If the sequence length was in the 17 - 24 range, it'd be a bit iffier, and if it were longer or MOD were expensive, then I might look for another solution.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 8:24 pm
by dwight
I might write it like this for a 6502.
Code: Select all
5 constant BuzzMax
3 constant FuzzMax
0 value FuzzCnt
0 value BuzzCnt
: FuzzBuzz
cr
0 !> BuzzCnt
0 !> FuzzCnt
101 1 do
FuzzCnt dup 0= if
." Fuzz" FuzzMax !> FuzzCnt
then
BuzzCnt dup 0= if
." Buzz" BuzzMax !> BuzzCnt
then
or if
I .
then
-1 +!> BuzzCnt
-1 +!> FuzzCnt
cr
loop ;
untested
Dwight
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 9:05 pm
by nyef
Clearly untested: You got the initialization wrong, so the first result is Fizzbuzz, which shouldn't occur until the fifteenth.
Also note that, unlike the other solutions, this one must be run iteratively. Yes, that's part of the problem statement, but the other versions can easily be refactored (in one case is already refactored) to have a word which takes a number and prints it as though it were part of the fizzbuzz sequence, thus the lower limit on the DO loop is arbitrary, not so here.
!> and +!> are non-standard, as well, FWIW. !> is clearly TO by any other name, but I see no standard equivalent of +!>.
Re: CS-101 problems and their FORTH solutions.
Posted: Tue Aug 01, 2017 9:59 pm
by Martin_H
I think Dwight's idea is to avoid the use of the mod operator which is probably expensive on an eight bit machine.
I've never seen !> and +!> before. I didn't know create does> outside of a word definition wasn't standard as most Forth implementations I have used included it.
Re: CS-101 problems and their FORTH solutions.
Posted: Wed Aug 02, 2017 12:34 am
by dwight
Ok, a little off.
I don't do much with ANSI standard so I miss some.
Most of the Forths I use have !>, =: or TO. They
all do the same thing.
I didn't realize that there was no +TO. It is most useful.
Code: Select all
5 constant BuzzMax
3 constant FuzzMax
0 value FuzzCnt
0 value BuzzCnt
: FuzzBuzz
BuzzMax to BuzzCnt
FuzzMax to FuzzCnt
101 1 do
BuzzCnt 1- to BuzzCnt
FuzzCnt 1- to FuzzCnt
FuzzCnt dup 0= if
." Fuzz" FuzzMax to FuzzCnt
then
BuzzCnt dup 0= if
." Buzz" BuzzMax to BuzzCnt
then
or if
I .
then
cr
loop ;
Yes, I was writing it to avoid MOD that is slow on a 6502.
I'm not sure what is meant by "this one must be run iteratively"?
What makes any of the solutions iteratively and mine not?
Do you mean it should be run like:
Bees ( EndCount StartCount - )
That is a minor change.
Dwight
Re: CS-101 problems and their FORTH solutions.
Posted: Wed Aug 02, 2017 2:16 am
by Martin_H
@Dwight, what does !> and +!> do? They are impervious to Google as I can't find a Forth word list that contains them.
Re: CS-101 problems and their FORTH solutions.
Posted: Wed Aug 02, 2017 2:22 am
by nyef
@Dwight, what does !> and +!> do? They are impervious to Google as I can't find a Forth word list that contains them.
From context, !> is another name for TO, and +!> would be +TO if such a word existed (add the value at the top of the stack to the following VALUE word).
I'm really not sure if I like VALUE and TO or not.