Haskell 修复列表功能(地图、邮编等)
Haskell List function (map, zip, etc..) with fix
我尝试学习 haskell 并进行练习 - 尝试使用函数修复重写标准列表操作(map、foldr、zip、iterate 等)。
我有重复的例子:
repeat a = fix $ \xs -> a : xs
并且进一步简化了
repeat a = fix (a:)
repeat = fix . (:)
谁能帮我做地图?
抱歉我的英语不好,提前谢谢你。
要使用fix
,需要将递归定义写成
map = .... something involving map ....
然后,我们让
map = fix (\m -> .... something involving m ....)
例如,
map = \f xs -> case xs of
[] -> []
y:ys -> f y : map f ys
所以,
map = fix (\m f xs -> case xs of
[] -> []
y:ys -> f y : m f ys)
或者,由于参数 f
对于每个递归调用都是相同的,我们可以让
map f = \xs -> case xs of
[] -> []
y:ys -> f y : map f ys
并获得
map f = fix (\m xs -> case xs of
[] -> []
y:ys -> f y : m ys)
我尝试学习 haskell 并进行练习 - 尝试使用函数修复重写标准列表操作(map、foldr、zip、iterate 等)。 我有重复的例子:
repeat a = fix $ \xs -> a : xs
并且进一步简化了
repeat a = fix (a:)
repeat = fix . (:)
谁能帮我做地图? 抱歉我的英语不好,提前谢谢你。
要使用fix
,需要将递归定义写成
map = .... something involving map ....
然后,我们让
map = fix (\m -> .... something involving m ....)
例如,
map = \f xs -> case xs of
[] -> []
y:ys -> f y : map f ys
所以,
map = fix (\m f xs -> case xs of
[] -> []
y:ys -> f y : m f ys)
或者,由于参数 f
对于每个递归调用都是相同的,我们可以让
map f = \xs -> case xs of
[] -> []
y:ys -> f y : map f ys
并获得
map f = fix (\m xs -> case xs of
[] -> []
y:ys -> f y : m ys)