
Perhaps it will help if you recall the meaning of the do notation, according
to the Haskell98 Report.
do {e} = e
do {e;stmts} = e >> do {stmts}
do {p <- e; stmts} =
let
ok p = do {stmts}
ok _ = fail "..."
in e >>= ok
do {let decls; stmts} = let decls in do {stmts}
Note that "do p <-e" only has meaning when is followed by something else
(e.g. an expresion). One way to explain it is that it only makes sense to
introduce "p" to use it later (e.g. you are reading a string and binding it
to "p" to parse it later). If you are only interested in the side effects of
"getLine", you should write:
main = do getLine
This will effectively read a line and discard it afterwards. Try it.
Paulo.
----- Original Message -----
From: Seth Kurtzberg
Hello, all
There is an error message that I get from GHCI that I do not understand. Usually it is caused by a syntax error or an indentation error, but I have occasionally seen it in other contexts and I'd like to understand what it is trying to tell me.
The message is that the last statement in a do construct must be an expression (exact text below).
I believe this occurs when the last statement in the do construct has a type other than "IO ()". The documentation (or perhaps Simon Thompson's or Paul Hudak's book; they get jumbled together in my mind) states that all statements in a do construct have type "IO ()"; but this is clearly not correct, as in:
do line <- getLine putStrLn line
where the first statement has type IO String.
Here is an example. This code is useless but it is not obvious to me why it is incorrect:
main = do line <- getLine
producing the error:
<interactive>:1: The last statement in a 'do' construct must be an expression
(This also limits the ability to enter monadic programming to the interpreter.)
TIA,
-- Seth Kurtzberg M. I. S. Corp seth@cql.com 1-480-661-1849 (GMT-7)
_______________________________________________ Glasgow-haskell-users mailing list Glasgow-haskell-users@haskell.org http://www.haskell.org/mailman/listinfo/glasgow-haskell-users