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

Matthias Fischmann fis at wiwi.hu-berlin.de
Thu Aug 17 04:33:22 EDT 2006


On Thu, Aug 17, 2006 at 10:18:25AM +0200, Szymon Z??bkiewicz wrote:
> To: haskell-cafe at haskell.org
> From: Szymon Z??bkiewicz <zombek2 at gmail.com>
> Date: Thu, 17 Aug 2006 10:18:25 +0200
> Subject: [Haskell-cafe] Last statement in 'do' must be an expression error.
> 
> Hi.
> 
> When trying to compilke this code:
> 
> {...}
> 8.if (a == 0) && (b == 0)
> 9.       then do
> 10.             nr1 <- read (prompt "enter 1. number: ")
> 11.             nr2 <- read (prompt "enter 2. number: ")

each do block needs to evaluate to some value when performed.  if you
have nothing to return, do something like "return ()".  (note this
only works if the type checker is happy with it.  possibly you need to
restructure parts of the code you didn't post, too.)

> 12.       else do
> 13.            let nr1 = a
> 14.                nr2 = b
> {...}
> 
> The compiler tells me thats there's an error on line 10:
> "The last statement in a 'do' construct mesy be an expression"
> 
> Could you tell me how to change it so that the "declaration" of the
> first nr1 and nr2 is still in the "then" block.

what about this code (untested)?

    do
      nr1 <- if a == 0 then read (prompt ":") else return a
      nr2 <- if b == 0 then read (prompt ":") else return b
      ...



hth,
m.
-------------- 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/0d23e7e0/attachment.bin


More information about the Haskell-Cafe mailing list