昨天的维基百科 http://www.urbandictionary.com/define.php?term=Wikibender开始于这个计算器问题 https://stackoverflow.com/questions/8428554/what-is-the-comonad-typeclass-in-haskell在 Comonads 上结束于MarkCC's http://scientopia.org/blogs/goodmath/about-markcc/文章关于手指树 http://scienceblogs.com/goodmath/2010/04/finger_trees_done_right_i_hope.php.
他在文章中大量使用了Reduce
类型类。他写到这个类型类就好像它是一个非常常见且经常使用的库,但我在 hackage 上找不到它,也找不到足够的文档来真正理解代码。
有人可以帮助我理解什么吗Reduce
typeclass 正在做什么,如何(-<)
and (>-)
运算符可以工作,并且应该告诉我有关文章中的代码的什么信息(复制如下)?
代码清单来自手指树做得对(我希望) http://scienceblogs.com/goodmath/2010/04/finger_trees_done_right_i_hope.php:
清单 1:Node 的实例声明
instance Reduce Node where
reducer (-<) (Node2 a b) z = a -< (b -< z)
reducer (-<) (Node3 a b c) z = a -< (b -< (c -< z))
reducer (>-) (Node2 b a) = (z >- b) >- a
reducer (>-) (Node3 c b a) = ((z >- c) >- b) >- a
清单 2:FingerTree 的实例声明
instance Reduce FingerTree where
reducer (-<) Empty zero = zero
reducer (-<) (Single x) zero = x -< zero
reducer (-<) Deep left mid right zero = left -<' (mid -<'' (right -<' zero))
where (-<') = reducer (-<)
(-<'') = reducer (reducer (-<))
reducel (>-) zero Empty = zero
reducel (>-) zero (Single x) = zero >- x
reducel (>-) zero (Deep left mid right) = ((zero >-' left) >-'' mid) >-' right
where (>-') = reducel (>-)
(>-'') = reducel (reducel (>-))
清单 3:数据类型
data Node s = Node2 s s | Node3 s s s
data FingerTree a = Empty
| Single a
| Deep (Digit a) (FingerTree (Node a)) (Digit a)
data Digit a = [ a ]
鉴于reduce
是“折叠”函数的常见替代名称,我猜它类似于the Foldable类型类别 http://hackage.haskell.org/packages/archive/base/4.4.1.0/doc/html/Data-Foldable.html#t:Foldable。实例定义本身似乎也有意义。
The Foldable
类可以仅使用定义foldr
,它具有类型签名foldr :: (Foldable t) => (a -> b -> b) -> b -> t a -> b
,而reducer
该代码似乎是reducer :: (Reduce t) => (a -> b -> b) -> t a -> b -> b
。除了不同的参数顺序之外,它的工作原理应该是相同的。
请注意,运算符只是函数的参数——您可以将它们全部替换为f
或另一个类似的通用标识符。使用运算符作为二元函数参数的名称......是一个稍微不寻常的选择,但我猜它确实强调了折叠结构的某些方面。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)