Write a function from IO a -> a?

后端 未结 4 854
野的像风
野的像风 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:38

    Not going into any details, if you're in a do block, you can (informally/inaccurately) consider <- as getting the value out of the IO.

    For example, the following function takes a line from getLine, and passes it to a pure function that just takes a String

    main = do
      line <- getLine
      putStrLn (wrap line)
    
    wrap :: String -> String
    wrap line = "'" ++ line ++ "'"
    

    If you compile this as wrap, and on the command line run

    echo "Hello" | wrap
    

    you should see

    'Hello'
    

提交回复
热议问题