I have this program which just prints out the command line arguments.
echoArgs :: IO ()
echoArgs = do
line <- getArgs
print line
What I wanted to know is that why does this fail when I type:
echoArgs :: IO ()
echoArgs = do
line <- getArgs
putStrLn line
and also why doesn't it work when I change it to:
echoArgs :: IO String
echoArgs = do
line <- getArgs
let line' = read line :: String
putStrLn line'
Because
getArgs :: IO [String]
so line
in do { line <- getArgs ; ... }
do { line <- getArgs ; ... }
is
line :: [String]
but putStrLn :: String -> IO ()
expects a String
argument, not a list of String
s.
Similarly, read :: Read a => String -> a
also expect a String
argument, not a list of String
s argument.
See also: The Guide to Types in do
-notation, In Vivid Colors .
print
produces a String
from whatever argument you give it.
putStrLn
, on the other hand, expects a String
as an argument. (Indeed, print = putStrLn . show
.) Similarly, read
expects a String
as an argument; in effect, it deserializes when what you are trying to do is serialize the list.
getArgs
has type IO [String]
, which means that line
is not a String
, but both String
and Show a => [a]
have a Show
instance which print
can use to make a String
out of it.
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.