编写一个更高阶atEach f xs
的函数,将默认函数f
应用于列表xs
的每个元素。
atEach succ [1 to 5] = [2,3,4,5,6]
atEach length ["Haskell", "go", "forward"] = [7,5,8]
答案 0 :(得分:5)
正如dave4420已经指出的那样,您的atEach
似乎是标准map
函数(如果没有,请澄清)。如果是这种情况,您可以采用不同的方式来实现它,例如:
-- direct recursion
atEach _ [] = []
atEach f (x:xs) = ???
-- list comprehension
atEach f xs = [??? | x <- xs]
--using a fold
atEach f = foldr ??? []
我不想破坏乐趣,所以你可以尝试填写???
。