我仍在试图了解箭头符号与Haskell中定义的箭头类型的语义之间的相似之处。特别是,this question似乎有一个非常典型的用箭头符号编写的小型计数器示例:
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吗?
发布于 2014-08-12 08:44:50
{- |
+---------+
>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将output转换为output(并生成新的next值)的内部部分只是一个纯函数,具有两个输入和两个输出。
发布于 2014-08-12 13:55:21
函数代码中的并行性是使用状态op。折成一叠
import Data.List
counter :: (Int, Int) -> Bool -> (Int, Int)
counter (_, previous_next) reset =
let output = if reset then 0 else previous_next
next = output +1
in (output, next)
runCounter :: [Bool] -> (Int, Int)
runCounter = foldl' counter (0,1)
main = do
let resets = [True, False, True, False, False]
result = fst $ runCounter resets
print result https://stackoverflow.com/questions/25256138
复制相似问题