[英]Can one express catamorphism through Data.Function.fix?
I have this lovely fixana
function here that performs about 5 times faster than her sister ana
. 我这里有一个可爱的
fixana
函数,执行速度比她姐姐ana
快5倍。 (i have a criterion
report to back me on this) (我对此有一份
criterion
报告来支持我)
ana alg = Fix . fmap (ana alg) . alg
fixana alg = fix $ \f -> Fix . fmap f . alg
Can I express their cousin cata
in the same fashion? 我可以发表自己的表弟
cata
以同样的方式?
cata f = f . fmap (cata f) . unFix
It seems to me that I cannot, but I have been humbled by my SO fellows quite a few times in the past. 在我看来,我不能,但过去我曾被SO研究员谦虚几次。
Actually, this has nothing to do with catamorphisms. 实际上,这与变态无关。
Whenever a function is defined as 每当函数定义为
f = (... f ...) -- some expression involving f
one can rewrite it using fix
as 一个人可以用
fix
重写它
f = fix $ \g -> (... g ...)
In the posted code we have a slight variant 在发布的代码中,我们有一个稍微的变化
f x = (... (f x) ...)
We can regard the above as f
being defined recursively. 我们可以将上面的
f
视为递归定义。 However, it's simpler if we regard fx
(rather than f
) being defined recursively. 但是,如果我们以递归方式定义
fx
(而不是f
),则更为简单。
f x = fix $ \g -> (... g ...)
This should be more efficient than the plain translation 这应该比普通翻译更有效
f = fix $ \g x -> (... (g x) ...)
since we don't need to call g
over and over again with the same argument x
. 因为我们不需要用相同的参数
x
反复调用g
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.