我正在编写一个函数 my_map,它接受一个一元函数和一个列表,并返回通过将函数映射到输入列表的所有元素而产生的列表。
Main> my_map (^3) [1..5]
[1,8,27,64,125]
Run Code Online (Sandbox Code Playgroud)
我是这样试的:
my_map :: (a -> b) -> [a] -> [b]
my_map f [] = []
my_map f (x:xs) = foldr (\x xs -> (f x):xs) [] xs
Run Code Online (Sandbox Code Playgroud)
但是在上面运行之后,我只得到[8,27,64,125]. 第一个数字1未显示在输出中。
有谁能够帮助我?
haskell ×1