我仍在尝试了解箭头表示法和 Haskell 中定义的 Arrow 类型类的语义之间的相似之处。尤其,这个问题 https://stackoverflow.com/questions/6976944/how-does-arrowloop-work-also-mfix似乎有一个非常规范的用箭头表示法编写的小计数器示例:
counter :: ArrowCircuit a => a Bool Int
counter = proc reset -> do
rec output <- returnA -< if reset then 0 else next
next <- delay 0 -< output+1
returnA -< output
有人可以告诉我如何在没有箭头符号的情况下将其转换回 Haskell2010 吗?
{- |
+---------+
>Bool>--------------> |
| >------------------>Int>
+---------+ | arr f |
/----> delay 0 >---> >---------\
| +---------+ | | |
| +---------+ |
| |
\--------------------------------------/
-}
counter' :: ArrowCircuit a => a Bool Int
counter' = loop $ second (delay 0) >>> arr f
where
f (reset, next) = let output = if reset then 0 else next
next' = output + 1
in (output, next')
递归rec
部分是使用实现的loop
。转换的内部部分reset
to output
using next
(并生产新的next
value)只是一个具有两个输入和两个输出的纯函数。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)