Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an if then else if control structure as an expression (in a nice way)

Tags:

haskell

I was wondering the correct and elegant way to do such a thing

function candy = case (color candy) of
    Blue -> if (isTasty candy) then eat candy
            else if (isSmelly candy) then dump candy
            else leave candy

I tried

function candy = case (color candy) of
    Blue -> dealWith candy
        where dealWith c
                | isTasty c = eat candy
                | isSmelly c = dump candy
                | otherwise = leave candy

Anyone knows how to improve on this?

MORE

I know I can use this

function candy = case (color candy) of
    Blue -> case () of
                _ | isTasty candy -> eat candy
                  | isSmelly candy -> dump candy
                  | otherwise -> leave candy

But using a case while not matching anything just doesn't seem to be the right way.

like image 303
L__ Avatar asked May 24 '13 14:05

L__


People also ask

How do you write if/then else statement?

Use the If… Then… Else statement to define two blocks of statements. One of the statements runs when the specified condition is True, and the other one runs when the condition is False. When you want to define more than two blocks of statements, use the ElseIf Statement.

How does the control structure if and if-else work?

IF-ELES: if the condition is true, then the block of code inside the IF statement is executed; otherwise, the block of code inside the ELSE is executed. IF-ELSE-IF: if the condition is true, then the block of code inside the IF statement is executed. After that, the ELSE-IF block is checked.

What's an example of an if/then else statement?

The if / then statement is a conditional statement that executes its sub-statement, which follows the then keyword, only if the provided condition evaluates to true: if x < 10 then x := x+1; In the above example, the condition is x < 10 , and the statement to execute is x := x+1 .

How do you write an if expression?

Syntax. Use the IF function, one of the logical functions, to return one value if a condition is true and another value if it's false. For example: =IF(A2>B2,"Over Budget","OK")


3 Answers

You can use guards directly in your outer case expression.

fun candy = case color candy of
    Blue | isTasty candy  -> eat candy
         | isSmelly candy -> dump candy
         | otherwise      -> leave candy
like image 87
hammar Avatar answered Oct 14 '22 17:10

hammar


You can use Multi-way if-expressions in GHC 7.6:

fun candy = case color candy of
    Blue -> if | isTasty candy -> eat candy
               | isSmelly candy -> dump candy
               | otherwise -> leave candy
like image 27
Fedor Gogolev Avatar answered Oct 14 '22 16:10

Fedor Gogolev


You can make a table-like structure using tuples. I've been known to do this:

function candy = case (color candy, isTasty candy, isSmelly candy) of
  (Blue, True, _   ) -> eat candy
  (Blue,    _, True) -> dump candy
   _                 -> leave candy
like image 27
Daniel Lyons Avatar answered Oct 14 '22 18:10

Daniel Lyons