Question about error message

Paulo Sequeira
Sat, 17 Aug 2002 18:14:59 -0500

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} =
            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.


----- Original Message -----
From: Seth Kurtzberg <>
To: <>
Sent: Saturday, August 17, 2002 2:23 PM
Subject: Question about error message

> 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
> trying to tell me.
> The message is that the last statement in a do construct must be an
> (exact text below).
> I believe this occurs when the last statement in the do construct has a
> other than "IO ()".  The documentation (or perhaps Simon Thompson's or
> 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
> is incorrect:
>     main = do line <- getLine
> producing the error:
> <interactive>:1: The last statement in a 'do' construct must be an
> (This also limits the ability to enter monadic programming to the
> interpreter.)
> TIA,
> --
> Seth Kurtzberg
> M. I. S. Corp
> 1-480-661-1849 (GMT-7)
> _______________________________________________
> Glasgow-haskell-users mailing list