Skip to content

Instantly share code, notes, and snippets.

@twingo-b
Created April 16, 2015 07:08
Show Gist options
  • Select an option

  • Save twingo-b/3a7a27eaba80f976372d to your computer and use it in GitHub Desktop.

Select an option

Save twingo-b/3a7a27eaba80f976372d to your computer and use it in GitHub Desktop.
-- sum' [1, 2, 3, 4, 5] == 15
sum' :: [Int] -> Int
sum' [] = 0
-- sum' [x] = x
sum' (x:xs) = x + sum'(xs)
-- 実装書いてね!
-- product' [] == 1
-- product' [1, 2, 3, 4, 5] == 120
product' :: [Int] -> Int
product' [] = 1
-- product' [x] = x
product' (x:xs) = x * (product' xs)
-- 実装書いてね!
-- map' (\x -> x + 3) [1, 2, 3] == [4, 5, 6]
map' :: (a -> b) -> [a] -> [b]
map' _ [] = []
-- map' f [x] = [f x]
map' f (x:xs) = f x : map' f xs
main = do
putStrLn . show $ sum' [1, 2, 3, 4, 5]
putStrLn . show $ product' [1, 2, 3, 4, 5]
putStrLn . show $ map' (\x -> x + 3) [1, 2, 3]
putStrLn $ map' toUpper "hogehoge"
@twingo-b
Copy link
Copy Markdown
Author

ghci> main
15
120
[4,5,6]
HOGEHOGE

@twingo-b
Copy link
Copy Markdown
Author

map' f [x] = [f x]

map' f (x:[])

にパターンマッチするからいらない

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment