Hacker News new | ask | show | jobs
by chucky 2730 days ago
The Haskell example is quite similar to the Scala/Rust example as well. I guess you could rewrite the Haskell version to match your Scala version pretty closely as well if you prefer doing the tuple construction and then matching on the tuple, like in your Scala version, instead of doing it directly inside the pattern match.

Something like this, with the caveat that I haven't done any proper coding in Haskell in years and I don't have an interpreter installed to verify correctness.

  fb :: (Integer, Integer) -> String
  fb (mod3, mod5)
    | (0, 0)    = "FizzBuzz"
    | (0, _)    = "Fizz"
    | (_, 0)    = "Buzz"
    | otherwise = show n
  
  main = putStrLn $ unlines $ map fb $ map (\x -> (x % 3, x % 5)) [1..100]
4 comments

FizzBuzz in Coconut looks similar:

  def fizzbuzz(n):
      case (n % 3, n % 5):
          match (0, 0): return "FizzBuzz"
          match (0, _): return "Fizz"
          match (_, 0): return "Buzz"
      else: return n |> str

  (
      range(1, 100)
      |> map$(fizzbuzz)
      |> x -> '\n'.join(x)
      |> print
  )
The step in the composition chain breaking down the integers into ordered pairs of the remainders is classy. Well done. For added points we now need an ADT to represent the different possible results of the computation :)
Not exactly write - you would use have to use either view patterns or a case...of statement rather than guards here. But the general gist is right.
You can do it like that in rust too, so I assume it works in Haskell.