高阶函数映射定义:
map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x:map f xs
那么如何用模式匹配/使用函数组合来编写revmap函数?
答案 0 :(得分:1)
假设您想要类似OCaml的rev_map
功能:
revmap :: (a -> b) -> [a] -> [b]
revmap f xs = go xs []
where
go [] acc = acc
go (x:xs) acc = go xs (f x : acc)