使用模式匹配或函数组合实现revmap函数

时间:2014-01-09 05:47:43

标签: haskell map pattern-matching reverse function-composition

高阶函数映射定义:

map :: (a -> b) -> [a] -> [b]
map f [] = []
map f (x:xs) = f x:map f xs

那么如何用模式匹配/使用函数组合来编写revmap函数?

1 个答案:

答案 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)