『ふつうのHaskellプログラミング』 の第3章を参考にして map 関数を mymap という関数名で作ってみた。

mymap 関数の引数に square 関数と整数のリストを渡す例のソースコード。


$ cat ./mymap.hs
main = print $ mymap square [1, 2, 3] 
 
square n = n * n
 
mymap :: (a -> b) -> [a] -> [b]
mymap f [] = []
mymap f (x:xs) = f x : mymap f xs

実行結果。


$ runghc ./mymap.hs
[1,4,9]

tags: haskell

Posted by NI-Lab. (@nilab)