[Haskell-beginners] What is the Haskell idiom for "if then else if then"

Paul Visschers mail at paulvisschers.net
Tue Mar 31 03:53:05 EDT 2009


That works, but I guess a case statement would be nicer:
case compare r 0 of
  LT -> LEFT
  GT -> RIGHT
  EQ -> STRAIGHT

This uses the compare function, which results in a value of the Ordering
datatype, which consists of the constructors LT, EQ and GT.

Peter Hickman wrote:
> I've just written this piece of code
> 
>     if r < 0
>     then LEFT
>     else
>         if r > 0
>         then RIGHT
>         else STRAIGHT
> 
> which works just fine but it does look rather clunky (as it would in any
> language I suppose). Is there a Haskell idiom for this type of code?
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners


More information about the Beginners mailing list