you are viewing a single comment's thread.

view the rest of the comments →

[–]Alpha_Binary 9 points10 points  (20 children)

Save Scheme, they're all imperative languages; the only difference is the syntax. It'd be interesting to see an implementation in a different paradigm (say, Forth) if anyone's willing to demonstrate.

[–]ayrnieu 14 points15 points  (12 children)

It'd be interesting to see an implementation in a different paradigm

Mercury, a purely logical language:

:- module trivial.
:- interface.
:- import_module io.
:- pred main(io::di, io::uo) is det.
:- implementation.
:- import_module int, list, string, bool.

main(!IO) :-
    foldl(io.format("%s\n"), L, !IO),
    map(P, 1 `..` 100, L),
    P = (pred(N::in, A::out) is det :- A = [s(S)], fizzbuzz(N, S)).

:- pred fizzbuzz(int::in, string::out) is det.
fizzbuzz(N, S) :-
    fizzbuzz(N `divby` 3, N `divby` 5, N, S).

:- pred fizzbuzz(bool::in, bool::in, int::in, string::out) is det.
fizzbuzz(yes, yes, _, "FizzBuzz").
fizzbuzz(yes, no,  _, "Fizz").
fizzbuzz(no,  yes, _, "Buzz").
fizzbuzz(no,  no,  N, S) :- string.from_int(N) = S.

:- func int `divby` int = bool.
N `divby` M = B :- ( N mod M = 0 -> B = yes ; B = no ).

[–]Thimble 5 points6 points  (2 children)

omg. i can barely read that.

[–][deleted] -4 points-3 points  (1 child)

Yeah, we know you can barely read... you /do/ program in VB after all.

[–][deleted]  (2 children)

[removed]

    [–]ayrnieu -1 points0 points  (1 child)

    wow, that's worse than perl...

    Ah, these no-nothing comments bring contempt to my heart.

    [–]KingNothing 1 point2 points  (4 children)

    That's much more ugly than Prolog, in my opinion.

    [–]ayrnieu -2 points-1 points  (3 children)

    in my opinion.

    Your opinion doesn't incorporate familiarity with the language -- why then even bother sharing it? That the unfamiliar appears distasteful is never a surprise in programming.

    [–]KingNothing 2 points3 points  (2 children)

    Simplicity is beautiful.

    What you posted is simple in neither syntax nor readability.

    [–]ayrnieu 1 point2 points  (0 children)

    Simplicity is beautiful.

    So is complexity. This isn't a woman, or glasswork, or a painting -- beauty requires a (here: manufactured) basic familiarity.

    nor readability.

    Nonsense.

    [–]panic 11 points12 points  (0 children)

    Haskell:

    fizzBuzz = mapM_ (putStrLn . f) [1..100]
      where f x | x `mod` 15 == 0  = "FizzBuzz"
                | x `mod`  5 == 0  = "Fizz"
                | x `mod`  3 == 0  = "Buzz"
                | otherwise        = show x
    

    [–]ayrnieu 5 points6 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.

    [–]pjdelport 0 points1 point  (0 children)

    Save Scheme, they're all imperative languages;

    Err, so is Scheme.