The below code works fine but I have the feeling that there must be something more idiomatic way to do this within the do
-notation without having to resort to the two liftM
s?
main = do
n <- (liftM read (liftM head getArgs)) :: IO Int
putStrLN . show $ sum [1..n]
If I do:
main = do args <- getArgs arg1 <- head args n <- read arg1 :: IO Int putStrLn . show $ sum [1..n]
I get:
Expected type: String
Actual type: Char
In the first Argument of 'read', namely 'arg1'
which I do not understand. <- getArgs
returns IO [String], so <- head args
should return IO String
, but it seems to return IO Char
?! (as per error message).
So my two questions are:
- what is the most idiomatic way do do this
- what's wrong in the second version?
main = do [n] <- getArgs; print $ sum [1..read n]
\$\endgroup\$