One dumb way to not forget to pass along the function as you recurse deeper (for this sort of higher-order function) is to use a helper:maptree f (Leaf a) = Leaf (f a)maptree f (Node xl xr) = Node (go xl) (go xr) where go = maptree f或者(或者可能更常见):Or, alternatively (and perhaps more commonly):maptree f tree = go tree -- or eta reduce: maptree f = go where go (Leaf a) = Leaf (f a) go (Node xl xr) = Node (go xl) (go xr)在第一个示例中,我将go用作maptree f的宏.在第二个示例中,我利用了maptree的输入f在go函数内部的范围内的事实,因为go是在maptree的where子句中声明的.In the first example, I use go sort of as a macro for maptree f. In the second example, I take advantage of the fact that maptree's input f is in scope inside the go function because go is declared in a where clause of maptree. 这篇关于哈斯克尔树木图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
05-26 17:01
查看更多