[Haskell-cafe] Last statement in 'do' must be an expression error.

Udo Stenzel u.stenzel at web.de
Thu Aug 17 06:59:03 EDT 2006

Szymon Z??bkiewicz wrote:
> The compiler tells me thats there's an error on line 10:
> "The last statement in a 'do' construct must be an expression"

I think, you have reached the point where treating do-notation as magic
won't help you.  Remember, 

> do
>     nr1 <- read (prompt "enter 1. number: ")
>     nr2 <- read (prompt "enter 2. number: ")

is syntactic sugar for

>     read (prompt "enter 1. number: ") 	>>= \nr1 ->
>     read (prompt "enter 2. number: ")		>>= \nr2 ->

and it obvious that something is missing after the last arrow.  That's
the expression the compiler is complaining about.  After the
translation, it is also completely clear, that there is no "variable"
which is ever "declared" and could be "assigned".

On a side note, using "trap values" like the special 0 is an ugly style
inherited from C.  You might want to get used to explicit
representations for missing values.  Compare this:

> read_new :: Maybe (Int, Int) -> IO (Int, Int)
> read_new (Just ab) = return ab
> read_new Nothing   = do
>	n1 <- read_prompt "enter 1. number: "
>	n2 <- read_prompt "enter 2. number: "
>       return (n1, n2)
>   where
>     read_prompt p = prompt p >>= readIO

Also note the 'read_prompt' function; I'm pretty sure you got the types
of 'prompt' and 'read' messed up, too.  So in anticipation of your next
question: 'read'ing the 'prompt' action is not the same as 'read'ing the
result of the 'prompt' action.  Only the latter makes sense.

"Enthusiasm is contagious, and so is boredom." -- Paul Graham
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: Digital signature
Url : http://www.haskell.org//pipermail/haskell-cafe/attachments/20060817/f3fc0011/attachment.bin

More information about the Haskell-Cafe mailing list