DEV Community

Discussion on: An Examination of Fizzbuzz

Collapse
 
carstenk_dev profile image
Carsten • Edited

nice one - here is basically the same (I did not care about the prompt) in Haskell:

module FizzBuzz where

import Data.Function ((&))
import Data.Maybe (fromMaybe)
import Data.Monoid ((<>))


fizzBuzz :: Int -> Int -> [String]
fizzBuzz from to = fizzBuzzer <$> [from .. to]

----------------------------------------------------------------------

fizzBuzzer :: Int -> String
fizzBuzzer n = Nothing
  <> ifMultiple 3 "Fizz" n
  <> ifMultiple 5 "Buzz" n
  &  nIfEmpty n


ifMultiple :: Int -> String -> Int -> Maybe String
ifMultiple d out n
  | n `mod` d == 0 = Just out
  | otherwise      = Nothing


nIfEmpty :: Int -> Maybe String -> String
nIfEmpty n = fromMaybe (show n)

isn't it nice how that just translates


PS: the n there should probably be refactored into a Reader-Monad :P