-7

编写一个高阶函数,atEach f xs将默认函数f应用于列表的每个元素xs

atEach succ [1 to 5] = [2,3,4,5,6]
atEach length ["Haskell", "go", "forward"] = [7,5,8]
4

1 回答 1

5

正如 dave4420 已经指出的那样,您atEach似乎是标准map功能(如果不是,请澄清)。如果是这种情况,您有不同的方法来实现它,例如:

-- direct recursion
atEach _ [] = []
atEach f (x:xs) = ??? 

-- list comprehension
atEach f xs = [??? | x <- xs]

--using a fold
atEach f = foldr ??? []

我不想破坏乐趣,所以你可以尝试填写???

于 2013-05-06T09:36:08.623 回答