简体   繁体   中英

(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.

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 .

The code works, however I would like to avoid naming the intermediate results (r1-r4).

If I wasn't working with monads, I could simply write 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 ). (>=>) is composition for Kleisli arrows ( a -> mb ).

import Control.Monad

r :: Int -> Maybe Int
r = f1 >=> f2 >=> f3 >=> f4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM