Haskell - if-else statement



Here is the general syntax of using the if-else conditional statement in Haskell.

if<Condition> then <True-Value>else <False-Value> 

In the above expression,

  • Condition − It is the binary condition which will be tested.

  • True-Value − It refers to the output that comes when the Condition satisfies

  • False-Value − It refers to the output that comes when the condition does not satisfy.

As Haskell codes are interpreted as mathematical expressions, the above statement will throw an error without else block. The following code shows how you can use the if-else statement in Haskell −

main = do   
   let var = 23 
   if var `rem` 2 == 0 
      then putStrLn "Number is Even" 
   else putStrLn "Number is Odd"

In the above example, the given condition fails. Hence, the else block will be executed. It will produce the following output −

Number is Odd 
haskell_decision_making.htm
Advertisements