Skip to content

Instantly share code, notes, and snippets.

@VoQn
Last active September 23, 2023 14:16
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save VoQn/8025389 to your computer and use it in GitHub Desktop.
Save VoQn/8025389 to your computer and use it in GitHub Desktop.
まぁHaskellさんの真骨頂はモナド(あくまでも市井で言われてるようなレベルの話)より型と型クラスじゃないかなーと最近は思いますね
{-# LANGUAGE GeneralizedNewtypeDeriving #-}
newtype FizzBuzz = FizzBuzz Int deriving (Enum, Eq, Ord, Num, Real, Integral)
instance Show FizzBuzz where
show x = (fizz ++ buzz) `or` show asInt where
fizz | ifMultipleOf 3 = "fizz" | otherwise = []
buzz | ifMultipleOf 5 = "buzz" | otherwise = []
ifMultipleOf n = x `mod` n == 0
or [] ys = ys
or xs __ = xs
asInt = (fromIntegral x) :: Int
main = mapM_ putStrLn $ map show ([1..] :: [FizzBuzz])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment