简体   繁体   中英

What is the type of <- in Haskell?

I have a working program

main = do

  inpStr <- getLine
  putStrLn ( "Hello " ++ inpStr )

where

putStrLn :: String -> IO ()

and

getLine :: IO String  

From this can I conclude that the type of <- is

IO a -> a

?

Unfortunately, it is not a regular function, but a language construct. You can use it only in do blocks to "extract" value from some context, such as IO .

do blocks and <- is just a syntax sugar for such things called Monad s (and IO is one of them).

There are some other examples of such contexts, which you can use with do and <- , such as lists, optional, or nullable, values (such as Maybe Int ), stateful computations and so on.

Some links:

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