The easiest way is to use fmapone that has the following type:
fmap :: (Functor f) => (a -> b) -> f a -> f b
IOimplements Functor, which means that we can specialize the above type, substituting IOfor f, to get:
fmap :: (a -> b) -> IO a -> IO b
, , a b s IO. :
getLine :: IO String
>>> getLine
Test<Enter>
Test
>>> fmap (map toUpper) getLine
Test<Enter>
TEST
? , map toUpper :
map toUpper :: String -> String
String a String. , .
fmap (map toUpper):
fmap (map toUpper) :: IO String -> IO String
IO. IO, .
do, :
getUpperCase :: IO String
getUpperCase = do
str <- getLine
return (map toUpper str)
>>> getUpperCase
Test<Enter>
TEST
, :
fmap f m = do
x <- m
return (f x)
, - Monad, Functor , . , liftM fmap:
liftM :: (Monad m) => (a -> b) -> m a -> m b
liftM f m = do
x <- m
return (f x)
liftM fmap, , .
, IO, :
, . fmap.