[Haskell-beginners] How works this `do` example?

Baa aquagnu at gmail.com
Thu Jul 13 08:29:56 UTC 2017


Hello, Dear List!

Consider, I have:

  request1 :: A -> Connection -> IO ()
  request2 :: A -> Connection -> IO A

How does it work -

  resp <- getConnection
    >>= do request1 myA
           request2 anotherA

?!

It is compiled but seems that does not execute `request1`...

`request1 myA` gets `Connection` value, good. But it does not return
`IO Connection`! It returns `IO ()`. But how does `request2 anotherA`
get `Connection` value too? Because this is not compiled sure:

  resp <- getConnection
    >>= request1 myA >>= request2 anotherA

I tried this:

    module Main where

    f1 :: Int -> IO ()
    f1 i = do
      print "f1!"
      print i
      return ()

    f2 :: Int -> IO Int
    f2 i = do
      print "f2!"
      print i
      return i

    f0 :: IO Int
    f0 = pure 10

    main :: IO ()
    main = f0
      >>= do f1
             f2
      >> print "end"

and I get output:

    "f2!"
    10
    "end"

which means that `f1` is not executing in `do..`-block, but how does `f2` get 10 as input?!


==
Cheers,
  Paul


More information about the Beginners mailing list