I’m rather new to Haskell, and I’m currently using LearnYouAHaskell.
I am trying to take a string separated by white space, and break it into a list of smaller word strings.
My current program:
main = do
putStrLn "Insert a string to convert: "
-- Input string
line <- getLine
words line;
But in this case, it tells me I’m having an IO error.
TO my understanding, getLine is an action, and so since this is impure, I have to bind it to “line”. Line is an accurate representation of getLine, which is an IO String.
However, shouldn’t line be a string?
When I try to use words on line, it tells me
“Couldn’t match expected type “IO a0” with actual type [String]
As if line isn’t a string.
Furthermore, can I use :t line in the program itself when I make it to see if it’s actual of the right type or not?
I apologize for the novice question, but I’m a bit stuck.
EDIT:
I did something similar in GHCI, and it tells me that my type is in fact a normal string.. I don’t get it.
Prelude> line <- getLine
"Hello fellows"
Prelude> :t line
line :: String
Prelude> words line
["Hello","fellows"]
Why doesn’t that work?
In haskell if you want to return a value, you have to say so:
words lineisn’t an IO action, it’s a list of strings, so it can’t be a statement in adoblock.return :: Monad m => a -> m aand in this case we can specialise it to the typea -> IO aand then to[String] -> IO [String]. Each of the statements in yourdoblock must beIOstatements.Taking it further:
If you want to compile your program, you should have
main :: IO(), which means you shouldn’t return your list.If, for example, you wanted to
processthose strings into a single string then output that, you could doalthough I’d personally write that last line as
putStrLn $ process.words $ line.Your interaction in GHCi
is using the fact that GHCi isn’t actually just running in the IO monad. In GHCi, if your input is a valid line in a
doblock, it’ll get run, but if it’s pure code it’ll get evaluated and printed. (An interactive interpreter like this is often called a REPL for Read-Eval-Print-Loop.)