Last active
December 10, 2015 16:18
-
-
Save haiiro-shimeji/4460480 to your computer and use it in GitHub Desktop.
fizzbuzz
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
fizzbuzz xs = | |
let fb x | |
-- fb x 以下のブロックになるので、fb x よりも深いインデントが無いとエラー | |
| 0 == mod x 15 = "FizzBuzz" | |
| 0 == mod x 5 = "Buzz" | |
| 0 == mod x 3 = "Fizz" | |
| otherwise = show x | |
in [ fb x | x <- xs ] | |
fizzbuzz'' xs = | |
[ | |
if 0 == mod x 15 then "FizzBuzz" | |
else if 0 == mod x 5 then "Buzz" | |
else if 0 == mod x 3 then "Fizz" | |
else show x | |
| x <- xs | |
] | |
-- 再帰を用いた実装 | |
fizzbuzz''' :: [Int] -> [String] | |
fizzbuzz''' [] = [] | |
fizzbuzz''' (x:xs) = | |
let fb x | |
| 0 == mod x 15 = "FizzBuzz" | |
| 0 == mod x 5 = "Buzz" | |
| 0 == mod x 3 = "Fizz" | |
| otherwise = show x | |
in (fb x) : (fizzbuzz''' xs) | |
-- 高階関数 | |
fizzbuzz'''' xs = | |
let fb x | |
| 0 == mod x 15 = "FizzBuzz" | |
| 0 == mod x 5 = "Buzz" | |
| 0 == mod x 3 = "Fizz" | |
| otherwise = show x | |
in map (fb) xs |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment