型の紹介 - 型の基礎 - 型変数 - filter関数、map関数、型シグネチャ
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT2(型の紹介)、LESSON 11(型の基礎)、11.5(練習問題)、Q11-1の解答を求めてみる。
コード
sample.hs
main :: IO ()
main = do
print $ filter even [1 .. 10]
print $ myFilter even [1 .. 10]
print $ myFilter even []
print $ map (+ 1) [1 .. 10]
print $ myMap (+ 1) [1 .. 10]
print $ myMap (+ 1) []
myFilter :: (a -> Bool) -> [a] -> [a]
myFilter f [] = []
myFilter f (x : xs) =
if f x
then x : myFilter f xs
else myFilter f xs
myMap :: (a -> b) -> [a] -> [b]
myMap f [] = []
myMap f (x : xs) = f x : myMap f xs
入出力結果(Terminal, Zsh)
% runghc sample1.hs
[2,4,6,8,10]
[2,4,6,8,10]
[]
[2,3,4,5,6,7,8,9,10,11]
[2,3,4,5,6,7,8,9,10,11]
[]
%