Skip to content

Instantly share code, notes, and snippets.

@koko-u
Created November 9, 2012 16:02
Show Gist options
  • Save koko-u/4046497 to your computer and use it in GitHub Desktop.
Save koko-u/4046497 to your computer and use it in GitHub Desktop.
すごいHaskell読書会の演習
slice :: Int -> Int -> [a] -> [a]
slice i j [] = []
slice i j all@(x:xs)
| i < 0 = slice 0 j all
| i > j = []
| i == 0 = take j all
| i > 0 = slice (i-1) (j-1) xs
fib :: Int -> Int
fib 1 = 1
fib 2 = 1
fib n
| n <= 0 = error "not defined"
| otherwise = fib(n-1) + fib(n-2)
fizzBuzz :: Int -> Int -> [String]
fizzBuzz n m = [ fb x | x <- [n..m]]
where fb x
| x `mod` 15 == 0 = "FizzBuzz"
| x `mod` 5 == 0 = "Buzz"
| x `mod` 3 == 0 = "Fizz"
| otherwise = show x
goodNum :: Int -> Bool
goodNum x
| x < 100 = False
| x > 999 = False
| otherwise = (x `mod` sum1 x == 0) && (x `mod` sum2 x == 0)
where sum1 x = (x `div` 10) + (x `mod` 10)
sum2 x = (x `div` 100) + (x `mod` 100)
goodNums = take 4 [x | x <- [100..], x `mod` 10 /= 0, goodNum x]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment