The core problem is simple enough, but I've developed a type system that I'm not sure about. The intention is, if the result is a number, calling
num $ fizzBuzz 16, for example, lets me retrieve
16 as an
Int. However, it seems awfully repetitive, to have to mention "Fizz" so many times in the code. Can it be done better?
data NoiseNum = Fizz | Buzz | FizzBuzz | Num Int deriving (Eq) instance Show NoiseNum where show FizzBuzz = "FizzBuzz" show Fizz = "Fizz" show Buzz = "Buzz" show (Num n) = show n num :: NoiseNum -> Int num (Num n) = n fizzBuzz :: Int -> NoiseNum fizzBuzz n | n `mod` 15 == 0 = FizzBuzz | n `mod` 3 == 0 = Fizz | n `mod` 5 == 0 = Buzz | otherwise = Num n main = do mapM_ putStrLn $ map (show . fizzBuzz) [1..100]