[Haskell-cafe] Is there a null statement that does nothing?

Brent Yorgey byorgey at seas.upenn.edu
Tue Oct 27 10:52:49 EDT 2009


On Wed, Oct 21, 2009 at 07:49:14PM -0500, Tim Wawrzynczak wrote:
> Yes, an if statement must have both 'then' and 'else' branches.  As an
> example, what if you had
> 
> let a = if b == 2 then True else False
> 
> and you were missing an else branch?  What would 'a' get assigned to?
> 
> The if statement "returns" a value so must have both branches.

By the way, it helps to be precise with language here: "statement"
usually indicates an instruction which causes some effect to happen.
"Expression" indicates something which evaluates to a value.  Haskell
doesn't have "if statements" but it does have "if expressions".  It
makes sense for an if *statement* (in imperative languages like C) to
have an optional else clause, since "do nothing" is a perfectly valid
*statement*.  But it doesn't make sense for an if *expression* to have
a missing else, since it must evaluate to something and (in general)
there is no "null value" that could be used.  Of course, as others
have noted, in a monadic context there IS a special "null value",
namely return (), which can be used to indicate "do nothing".

-Brent


More information about the Haskell-Cafe mailing list