[Haskell-cafe] Shortening if-then-else

tpledger at ihug.co.nz tpledger at ihug.co.nz
Sun Nov 27 16:55:24 EST 2005


Arjan van IJzendoorn wrote:
 | > Is there a shorter way to write the if-then-else part
below?
 | >        if (cmdType cmd) /= (CmdSitError Server)
 | >           then return $ Just seat_num
 | >           else return Nothing
 |
 | return $ if cmdType cmd /= CmdSitError Serv
 | 		then Just seat_num else Nothing


There's a subtle change in semantics when we move the 'if'
inside the 'return'.

The original code requires the condition to be evaluated as
part of the do-expression's monad's structure, but the
translated code defers it.

'return $! if ...' would be closer to the original.

Regards,
Tom


More information about the Haskell-Cafe mailing list