[英](Haskell) Syntax for chaining functions with bindings in do block
I have the following code that chains functions using a monad, binding the intermediate results.我有以下代码使用 monad 链接函数,绑定中间结果。
r :: Int -> Maybe Int
r n = do
r1 <- f1 n
r2 <- f2 r1
r3 <- f3 r2
r4 <- f4 r3
return r4
where the type signature of the f1-f4 is f1 :: Int -> Maybe Int
.其中 f1-f4 的类型签名是f1 :: Int -> Maybe Int
。
The code works, however I would like to avoid naming the intermediate results (r1-r4).该代码有效,但是我想避免命名中间结果(r1-r4)。
If I wasn't working with monads, I could simply write r = f1 . f2 . f3 . f4
如果我不使用 monad,我可以简单地写r = f1 . f2 . f3 . f4
r = f1 . f2 . f3 . f4
r = f1 . f2 . f3 . f4
. r = f1 . f2 . f3 . f4
。 Is something similar possible for monads?单子有类似的可能吗?
(.)
is composition for regular arrows ( a -> b
). (.)
是常规箭头 ( a -> b
) 的组合。 (>=>)
is composition for Kleisli arrows ( a -> mb
). (>=>)
是 Kleisli 箭头 ( a -> mb
) 的组合。
import Control.Monad
r :: Int -> Maybe Int
r = f1 >=> f2 >=> f3 >=> f4
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.