Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

The core problem 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]

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]

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]
Tweeted twitter.com/#!/StackCodeReview/status/488241990086443008
Source Link
200_success
  • 145.5k
  • 22
  • 190
  • 478

Haskell FizzBuzz with a type system

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]
lang-hs

AltStyle によって変換されたページ (->オリジナル) /