What is the Haskell idiom for "if then else if then"

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

What is the Haskell idiom for "if then else if then"

Peter Hickman
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?

Reply | Threaded
Open this post in threaded view
|

What is the Haskell idiom for "if then else if then"

Daniel Fischer-4
Am Montag 30 M?rz 2009 17:33:02 schrieb Peter Hickman:

> 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?

Usually, people use guards:

function r
    | r < 0     = LEFT
    | r > 0     = RIGHT
    | otherwise = STRAIGHT

Another option would be a case:

    case compare r 0 of
      LT -> LEFT
      GT -> RIGHT
      EQ -> STRAIGHT

You can also have a case with guards:

case expression of
    pattern1 | condition1.1 -> result1.1
             | condition1.2 -> result1.2
    pattern2 | condition2.1 -> result2.1
    pattern3 -> result3
    _ -> defaultresult
Reply | Threaded
Open this post in threaded view
|

What is the Haskell idiom for "if then else if then"

Paul Visschers-2
In reply to this post by Peter Hickman
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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

What is the Haskell idiom for "if then else if then"

Hrushikesh Tilak
In reply to this post by Peter Hickman
http://www.haskell.org/haskellwiki/If-then-else#Replace_syntactic_sugar_by_a_function
mentions that there is no such function in Prelude.


On Mon, Mar 30, 2009 at 11:33 AM, Peter Hickman <[hidden email]> 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
> [hidden email]
> http://www.haskell.org/mailman/listinfo/beginners