Skip to content

Instantly share code, notes, and snippets.

@haiiro-shimeji
Last active December 10, 2015 16:18
Show Gist options
  • Save haiiro-shimeji/4460480 to your computer and use it in GitHub Desktop.
Save haiiro-shimeji/4460480 to your computer and use it in GitHub Desktop.
fizzbuzz
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