Introduction to Haskell if
In general, if statement in any programming language is a conditional statement, which is used to check the specific condition and based on it we execute the logic of our application. If statement is always followed by the else statement in any programming language. if block or statement will always be executed if the expression is evaluating to be true, if it is not true then else part will be executed accordingly. In short, if statement is said to be a conditional statement, which is used to check the given expression is true or false. In the coming section of the tutorial, we will discuss the internal working, implementation, and its usage in Haskell while programming.
Syntax
As discussed if statement is used to check the given expression and according to it we execute our logic, let’s take a look at its syntax in Haskell for better understanding see below;
if (your condition) then (true value here) else (false value here)
As you can see in the above line of syntax we have if and else statement associates together. Let’s taken n closer look at the practice syntax for beginners to understand it better see below;
Example:
if variable condition
then some logic
else other logic
As it is pretty much clear from the above line of syntax, it is very easy to implement and handle in Haskell, in the coming section of the tutorial we will see the internal working and detail explanation about its syntax for beginners to understand it better.
How does if statement work in Haskell?
As we already know that if statement is used to check the given expression is evaluating to be true or not. This statement falls under the category of a conditional statement in any programming language. In this section we will see its flow chart in detail how it works and what conditional it executes in detail let’s get started to see below;
1. First it will take the condition to the if statement.
2. This condition will evaluate and return us a Boolean value of True or either False based on the computation
3. If the condition is evaluating to be True then it will execute the code of if block.
4. If the condition is evaluating as False then it will execute the corresponding else block for that statement.
5. At the last it will exit from the flow, after executing the corresponding logic.
Below see the flow chart attach for the if statement in Haskell;
Now we will have closer look at the signature of the if statement in detail for better understanding see below;
If signature : if (your condition) then (true value here) else (false value here)
This is the signature given by the Haskell official documentation which can be found on their official website we well. If we have closer look at the signature, then it is pretty much clear to us how we can use and implement this while programming let’s take a detailed look at each mentioned point in detail see below;
1. condition: This condition will represent the expression that is going to be evaluated once we pass this and based on this result corresponding block of code will be executed.
2. False value: If the evaluation of the above condition is coming out to be false then this will execute.
3. True value: If the evaluation of the above condition is coming out to be true then this will execute.
4. then: we are using the keyword to represent the true and false condition separation here. It will always come after the if statement because it represents here the true condition for that expression.
Now we will see one sample code for better understanding, which will clear the usage and implementation more see below;
Example:
main = do
let demo = 10
if demo == 10
then putStrLn "equal"
else putStrLn "not equal"
As you can see in the above line of code we are trying to compare our variable with some value. First, we have defined the main module, and inside that, we are writing our logic with the if signature. First, we have declared one variable named ‘demo’ which contains a value of 10. After that, we have defined the if statement, which is comparing the emo variable with some value, if the value is the same then if block will be executed, otherwise it will execute the else statement in the output.
Examples
1) In this example we are trying to use the if statement in Haskell, it is an in-built function. Here we are trying to compare multiple values by using the if statement and printing the corresponding logic according to it. This is a sample example for beginners to understand it better and start using it while programming for better expression comparison.
Example:
main = do
print("demo to show if statement in Haskell !!")
let val1 = 10
let val2 = 20
let val3 = 500
let val4 = 9
let val5 = 909
let val6 = 30
let val7 = 1.1
print("using if statement here to check the values of variable !!")
if val1 == 10
then putStrLn "equal"
else putStrLn "not equal"
if val2 == 1000
then putStrLn "equal"
else putStrLn "not equal"
if val3 `rem` 2 == 0
then putStrLn "divisiable b 2"
else putStrLn "not divisiable b 2"
if val4 == 23
then putStrLn "equal"
else putStrLn "not equal"
if val5 == 900
then putStrLn "equal"
else putStrLn "not equal"
if val6 == 78
then putStrLn "equal"
else putStrLn "not equal"
if val7 == 1.1
then putStrLn "equal"
else putStrLn "not equal"
Output:
Conclusion
As you already saw it is very easy to use, readable, and easy to maintain so that we can if statement in our programming by following the proper syntax given by the Haskell programming. Also, it is an in-built functionality of Haskell so we do not require to include any dependency for this.
Recommended Articles
We hope that this EDUCBA information on “Haskell if” was beneficial to you. You can view EDUCBA’s recommended articles for more information.