Haskell:使用展开定义地图功能

时间:2016-03-05 21:32:38

标签: haskell functional-programming haskell-platform

我有以下Haskell方法

unfold :: (a -> Bool) -> (a -> b) -> (a -> a) -> a -> [b]
unfold p h t x
    | p x       = []
    | otherwise = (h x):(unfold p h t (t x))

如何使用此给定的展开方法定义常规前奏图f x方法。

1 个答案:

答案 0 :(得分:4)

如果你定义:

map' :: (a -> b) -> [a] -> [b]
map' f = unfold null (f.head) tail

然后

\> map' show [1..5]
["1","2","3","4","5"]
\> map' (+1) []
[]