Skip to content

Instantly share code, notes, and snippets.

@shigemk2
Created February 4, 2015 12:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shigemk2/e0d03ea134ce034c0663 to your computer and use it in GitHub Desktop.
Save shigemk2/e0d03ea134ce034c0663 to your computer and use it in GitHub Desktop.
-- 関数はカリー化されているので、 この両側の xs は省略できます
sum' :: (Num a) => [a] -> a
sum' xs = foldl (+) 0 xs
-- 省略したのがこれ。
sum'' :: (Num a) => [a] -> a
sum'' = foldl (+) 0
oddSquareSum :: Integer
oddSquareSum = sum (takeWhile (<10000) (filter odd (map (^2) [1..])))
-- 関数合成の応用
oddSquareSum' :: Integer
oddSquareSum' = sum . takeWhile (<10000) . filter odd $ map (^2) [1..]
main = do
print $ sum' [1,2,3,4,5]
print $ sum'' [1,2,3,4,5]
print $ oddSquareSum
print $ oddSquareSum'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment