从箭头符号转换 [英] Convert from arrow notation

查看:188
本文介绍了从箭头符号转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍然试图弄清楚箭头符号和在Haskell中定义的箭头类型的语义之间的相似之处。特别是,这个问题似乎有一个很小的计数器的典型例子用箭头符号表示:

  counter :: ArrowCircuit a => Bool Int 
counter = proc reset - >做
rec输出< - returnA - <如果重置,那么0 else else
next< - 延迟0 - <输出+ 1
returnA - <输出

有人可以告诉我如何将其转换回Haskell2010而不使用箭头符号吗?

解决方案

  { -  | 
+ --------- +
> Bool> --------------> |
| > ------------------>诠释>
+ --------- + | arr f |
/ ---->延迟0> - > > --------- \
| + --------- + | | |
| + --------- + |
| |
\ -------------------------------------- /

- }
counter':: ArrowCircuit a => Bool Int
counter'= loop $ second(delay 0)>>> arr f
其中
f(reset,next)= let output = if reset 0 then else next
next'= output + 1
in(output,next')

递归 rec 部分使用。使用下一个((code>))将 reset 转换为输出的内部部分并产生新的下一个值)只是一个具有两个输入和两个输出的纯函数。


I'm still trying to get a hang of the parallels between arrow notation and the semantics of the Arrow typeclasses defined in Haskell. In particular, this question seems to have a very canonical example of a small counter written with arrow notation:

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

Can someone show me how to convert this back into Haskell2010 without arrow notation?

解决方案

{- |
                     +---------+
 >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')

The recursive rec part is implemented using loop. The inner part that converts reset to output using next (and producing new next value) is just a pure function with two inputs and two outputs.

这篇关于从箭头符号转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆