Haskell - How can I use pure functions inside IO functions?

前端 未结 4 1878
清歌不尽
清歌不尽 2021-01-19 00:14

How can I use pure functions inside IO functions? :-/

For example: I\'m reading a file (IO function) and I want to parse its context, a string, by using a pure funct

4条回答
  •  庸人自扰
    2021-01-19 00:25

    You can also consider liftM function from Control.Monad.
    A little example to help you (run it into ghci as you are under the IO Monad)

    $ import Control.Monad -- to emerge liftM
    $ import Data.Char     -- to emerge toUpper
    $ :t map to Upper -- A pure function
    map toUpper :: [Char] -> [Char]
    $ :t liftM 
    liftM :: Monad m => (a1 -> r) -> m a1 -> m r
    $ liftM (map toUpper) getLine 
    

提交回复
热议问题