Last active
August 29, 2015 14:09
-
-
Save atomictom/4eee59965151c27e817e to your computer and use it in GitHub Desktop.
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
| import Control.Monad | |
| import Control.Applicative | |
| import Data.Monoid | |
| import Data.Maybe | |
| main :: IO() | |
| main = do | |
| putStr "results: \n" | |
| putStr $ fizzbuzz5 20 | |
| fizzbuzz :: Int -> String | |
| fizzbuzz to = unlines $ map displayInt [1..to] | |
| where | |
| displayInt = fromMaybe . show <*> 3 `displays` "fizz" <> 5 `displays` "buzz" | |
| (d `displays` s) n = if n `mod` d == 0 then Just s else Nothing | |
| fizzbuzz2 :: Int -> String | |
| fizzbuzz2 to = unlines $ map displayInt [1..to] | |
| where | |
| displayInt n = fromMaybe (show n) $ mfilter (not . null) (Just (fizz ++ buzz)) | |
| where | |
| fizz = testInt 3 "fizz" | |
| buzz = testInt 5 "buzz" | |
| testInt d s = if n `mod` d == 0 then s else "" | |
| fizzbuzz3 :: Int -> String | |
| fizzbuzz3 to = unlines $ map displayInt [1..to] | |
| where | |
| displayInt n = fromMaybe (show n) (msum [liftM2 (++) fizz buzz, fizz, buzz]) | |
| where | |
| fizz = testInt 3 "fizz" | |
| buzz = testInt 5 "buzz" | |
| testInt d s = if n `mod` d == 0 then Just s else Nothing | |
| data Output = Fizz | Buzz | Num | |
| fizzbuzz4 :: Int -> String | |
| fizzbuzz4 n = unlines $ zipWith3 test (cycle [Num, Num, Fizz]) (cycle [Num, Num, Num, Num, Buzz]) [1..n] | |
| where | |
| test Fizz Buzz _ = "fizzbuzz" | |
| test Fizz _ _ = "fizz" | |
| test _ Buzz _ = "buzz" | |
| test _ _ num = show num | |
| fizzbuzz5 :: Int -> String | |
| fizzbuzz5 to = unlines $ zipWith3 test (pat 3 "fizz") (pat 5 "buzz") [1..to] | |
| where | |
| pat n str = cycle (replicate (n - 1) "" ++ [str]) | |
| test s1 s2 n = head $ ((<$) <*> guard . not . null) (s1 ++ s2) ++ [show n] | |
| fizzbuzz6 :: Int -> String | |
| fizzbuzz6 to = unlines $ map displayInt [1..to] | |
| where | |
| displayInt n = if null fizz_or_buzz then show n else fizz_or_buzz | |
| where | |
| fizz_or_buzz = testInt 3 "fizz" ++ testInt 5 "buzz" | |
| testInt d s = if n `mod` d == 0 then s else "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment