Last active
September 23, 2023 14:16
-
-
Save VoQn/8025389 to your computer and use it in GitHub Desktop.
まぁHaskellさんの真骨頂はモナド(あくまでも市井で言われてるようなレベルの話)より型と型クラスじゃないかなーと最近は思いますね
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{-# 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