Write a function from IO a -> a?

后端 未结 4 853
野的像风
野的像风 2021-01-22 17:57

Take the function getLine.

it has a type

getLine :: IO String

How do I extract the String from this IO value. More generally, how do I

4条回答
  •  野性不改
    2021-01-22 18:29

    If you know C then consider the question "How can I get the string from gets?" An IO String is not some string that's made hard to get to, it's a procedure that can return a string - like reading from a network or stdin. You want to run the procedure to obtain a string.

    A common way to run IO actions in a sequence is do notation:

    main = do
       someString <- getLine
       -- someString :: String
       print someString
    

    In the above you run the getLine operation to obtain a String value then use the value however you wish.

提交回复
热议问题