作者:娶位红太郎_442 | 来源:互联网 | 2022-12-03 09:17
我试图η-减少功能
foldr :: (a -> b -> b) -> b -> BinaryTree a -> b
foldr combiner base tree = foldMap combiner tree base where
foldMap = ...
同
foldMap :: (a -> b -> b) -> BinaryTree a -> b -> b
按预期工作.
我减少了η
foldr combiner base tree = foldMap combiner tree base
至
foldr combiner = flip $ foldMap combiner where
...
这按预期工作.看起来我应该能够完全η-reduce以获得无点功能
foldr = flip $ foldMap where
...
但是,这会导致编译错误
Couldn't match type ‘a -> b -> b’ with ‘BinaryTree t0’
Expected type: (a -> b -> b) -> b -> BinaryTree a -> b
Actual type: BinaryTree t0 -> (t0 -> b -> b) -> b -> b
有可能η-减少更远,如果是这样,怎么样?
1> Igor Drozdov..:
引发错误,因为g b = f $ a b
不等同于g = f $ a
.
在第一种情况下,您将获得以下评估顺序:
应用功能a
于b
(调用a
具有b
作为参数)
将函数f
应用于结果
在第二种情况:
将功能f
应用于a
因此,你刚才flip
的foldMap
功能,但实际上要flip
的foldMap
功能后传递combiner
给它.这导致我们得出结论,你实际上想要的是组合,即.
功能:
foldr = flip . foldMap where
...
这个答案是完全正确的(+1),但我要补充一点,我认为`翻转.foldMap`的可读性低于原始代码.确实,'翻转.f`不会立即触发"哦,它是'f`,第二和第三个参数被翻转",就像原始代码一样.