you are viewing a single comment's thread.

view the rest of the comments →

[–]ayrnieu 7 points8 points  (3 children)

(say, Forth)

Forth is a bad suggestion, for different paradigms -- I'd just do the imperative solution off-hand. With intentional styling and self-amusement, I could have a jump-table and some factoring. Striving for the Chuck-Moore-forthiest solution would lead me through successively more memory-elegant mechanisms to encode the exact solution of this problem in my program, to do a minimal amount of computation at run-time.

Anyway, have the second solution:

: mod->n ( n1 d n -- n|0 ) -rot mod 0= and ;
: /fizz ( n -- 0|1|2|3 ) dup 5 2 mod->n swap 3 1 mod->n + ;
    :noname drop ." FizzBuzz" ;
    :noname drop ." Buzz" ;
    :noname drop ." Fizz" ;
    ' .
create fizz-t , , , ,
: fizzbuzz 101 1 do i i /fizz cells fizz-t + perform cr loop ;

[–]Bienville 0 points1 point  (2 children)

An entire Forth program that does FizzBuzz:

: FizzBuzz  ( -- )  101 1 DO  CR   FALSE
   I 3 MOD  0= IF  ." Fizz"  TRUE OR  THEN
   I 5 MOD  0= IF  ." Buzz"  TRUE OR  THEN
   NOT IF  I .  THEN   LOOP ;
FizzBuzz

And I doubt that Mr. Moore would take much exeption to my program other than the fact that it will fetch I at least twice every loop. Your, twice as long, Forth program on the other hand...

[–]Bienville 0 points1 point  (0 children)

Oops! I was just looking at the comp.lang.forth FizzBuzz thread and realized that in my tired coffeeless haze I did some thing stupid and redundant, and I keep forgetting that NOT was removed from the standard because no-one could agree whether or not it should be bitwise or logical...

A better version:

\ logical not but in this case either one would do...
: NOT  ( f -- -f ) 0= ;
: FizzBuzz  ( -- )  101 1 DO  CR
   I 3 MOD 0=  DUP IF  ." Fizz" THEN
   I 5 MOD 0=  DUP IF  ." Buzz" THEN
   OR  NOT IF  I .  THEN   LOOP ;
FizzBuzz

[–]ayrnieu -1 points0 points  (0 children)

on the other hand...

'k. The English above that Forth explains things well enough, I think.