Instantly share code, notes, and snippets.

Embed
What would you like to do?
-- 関数はカリー化されているので、 この両側の 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