News for hackers. Stuff that matters.
Re:FizzBuzz in Haskell [pdf] by tromp on Monday November 21, 2022 @06:38PM (#33699763)
Here's the resulting Haskell code

    fizzbuzz n = (test 3 "fizz" . test 5 "buzz") id (show n) where
      test d s x if  n `mod` d == 0 then const (s ++ x "") else x

    main = mapM_ (putStrLn . fizzbuzz) [0..100]
you can compile with ghc and run...
Re:FizzBuzz in Haskell [pdf] by bradrn on Monday November 21, 2022 @09:36PM (#33701051)
A minor comment, but I’d reformat this a bit to make its structure clearer:

    fizzbuzz n = go id (show n)
      where
        go = test 3 "fizz" . test 5 "buzz"

        test d s x =
          if n `mod` d == 0 
          then const (s ++ x "")
          else x
Re:FizzBuzz in Haskell [pdf] by IAmYourDensity on Monday November 21, 2022 @10:13PM (#33701316)
Explanation:

If both tests fail, return the 'id' function which when applied to (show n) will return the number as a string.

If one or both tests succeed, return a 'const' function which will simply return the appropriate string and ignore the (show n).

This example was illuminating. It helped me realize I'm wasting too much time with Haskell.