Updated April 20, 2023
Definition on Haskell Where Function
Haskell where is not a function rather it is a keyword that is used to divide the more complex logic or calculation into smaller parts, which makes the logic or calculation easy to understand and handle. If inside our function we have some complex logic or calculation then we can divide that logic into smaller parts using where keyword. Also where clause is responsible to generate the desired output as well. Where is a keyword or function which is available in Haskell, we do not require to use any external library for this to work. In the coming section of the tutorial, we will have better understanding of the where clause in detail also we will see its implementation and usage.
Syntax:
As we discussed that it is a keyword which used with a function definition, by the use of this we can divide our compels logic into smaller parts. Let’s take a look at its syntax for better understanding for beginners to see below;
variable_name = varibale_name_2
where
// here your logic or calculations will go
As you can see in the above lines of syntax we are trying to use where clause hereafter this we can write our logic, which mainly represents the smaller part of more compels calculation. Let’s take a look at the practice syntax for beginners which will give them more clarity on this see below;
e.g.:
demo = val
where
// logic
It is just a basic practice syntax, in the coming section of the tutorial we will see the internal working and implementation of where clause in the Haskell programing language.
How does Where Function work in Haskell?
As we already know that where is a keyword or a function that helps to dived the complex calculations into smaller parts. Also, it helps us to get the desired output by dividing the logic into several parts. By the use of this, we can easily calculate the value without any mistake, also it is readable by the other developers as well. This function is helpful when we have several parameters in the function, so in this case, we can break the single line expression to multiple parts using the where clause in Haskell. Where clause in Haskell is always defined at the function definition and it comes under the declarative category unlike Let in Haskell. where clause in the Haskell programming is bound to syntactic construct. but he use of where clause or function in Haskell we can remove the repetitive code as well, instead, we can hold the value into some variable and used it later.
Areas to use with where clause in Haskell, let’s take a look at how we can use where clause or function in Haskell see below;
1) Inside function defection: Where clause can be used with a function definition, where we can dive the logic into smaller parts. This divination helps us to optimize the code as well. Let’s take one example to understand this scenario see below;
e.g. :
without where :
demo :: (RealFloat a) => a -> a -> String
demo w h
| w / h ^ 2 <= 18.5 = "some msg"
| w / h ^ 2 <= 25.0 = "some msg 2"
| w / h ^ 2 <= 30.0 = "some msg 3"
| otherwise = "success part "
As you can see in the above line of code we have to defined and calculate the weight of the person three times here. It is increasing the number of lines. also making the code repetitive. So in such scenarios, we can replace our code with where
clause. By the use of the where clause we can able to calculate the value of the comparing variable once after this, we can use this same variable to return the result. This will help us to reduce and optimize our code and also make it easier to understand as well.
2) With pattern matching: Where clause can also be used with pattern matching in Haskell. By the use of this, we can match the pattern and return the result according to that. Let’s take a sample example to understand its syntax better see below;
e.g. :
function defination
where your_pattern
As you can see in the above lines of code we are using where keywords only to make it work with pattern matching in Haskell. As you can see it is very easy to use and understand.
Examples
Below are some of the different examples:
Example #1
In this example we are trying to add multiple parameters using the where function in Haskell. This is a sample example for beginners to understand and start using this while programming.
Code:
add :: (Float, Float) -> (Float, Float)
add (a,b) = (x1, x2) where
x1 = 10 + a
x2 = 100 + b
main = do
putStrLn "Demo to show wherefunction in Haskell !!!"
putStrLn "Calling function to calculate the values using where function !!!"
let result1 = add(10, 20)
let result2 = add(200, -30)
let result3 = add(150, 56)
let result4 = add(-98, -34)
let result5 = add(-10, 67)
let result6 = add(45, -45)
print("result one is ")
print (result1)
print("result two is ")
print (result2)
print("result three is ")
print (result3)
print("result four is ")
print (result4)
print("result five is ")
print (result5)
print("result six is ")
print (result6)
Output:
Example #2 – For string with where function
Code:
addstring:: (String, String) -> (String, String)
addstring (a,b) = (x1, x2) where
x1 = a ++ "i am string one"
x2 = b ++ "i am string two"
main = do
putStrLn "Demo to show wherefunction in Haskell !!!"
putStrLn "Calling function to calculate the values using where function !!!"
let result1 = addstring("hello ", "bye ")
let result2 = addstring("world ", "happy ")
let result3 = addstring("shopping ", "everyday ")
let result4 = addstring("enjoy ", "moment ")
let result5 = addstring("good day ", "always ")
let result6 = addstring("great ", "things ")
print("result one is ")
print (result1)
print("result two is ")
print (result2)
print("result three is ")
print (result3)
print("result four is ")
print (result4)
print("result five is ")
print (result5)
print("result six is ")
print (result6)
Output:
Conclusion
By the use of where we can do pattern matching as well, it also helps us to handle the complex logic for our program very efficiently. If we use where clause in the program then the code becomes very readable and easy to understand and maintain. One more thing it is an inbuilt feature of Haskell programming without the need od an external library.
Recommended Articles
We hope that this EDUCBA information on “Haskell Where” was beneficial to you. You can view EDUCBA’s recommended articles for more information.