Updated April 19, 2023
Definition of Haskell not
In Haskell not is a function that is used to check the variable value, to use not in Haskell we have ‘not’ keyword. Also, it is an in-built function in Haskell, so we do not require to induce any dependency or install any library. we can use this directly inside the program. In short, if we want to check any variable value or perform any logic based on the condition based on the evaluation of variable value then we can go for not function in Haskell, it is very easy to use and handle while programming. In the coming section of the tutorial, we will see the internal working of the not function also the implementation and its usage while doing programming.
Syntax:
As we discussed not function can be used to check the variable value, and it returns us a Boolean value after evaluation. Let’s take a look at the syntax of the not function in Haskell for better understating and usage for beginners see below;
not ‘your value to be checked’
As you can see in the above line of syntax, we are using not keyword to check the variable value, and it will return us Boolean after evaluation. Let’s take a look at one practice syntax for better implementation to see below;
e.g. :
not False
As you can see in the above line of syntax it is very easy to use and handle in Haskell, in the coming section of the article we will see the internal working and its usage in better detail which will help beginners to start using it while programming.
How not function works in Haskell?
As we already know that now, not function is used to check the variable value, it returns us the Boolean value. It works as the opposite of equal we can say. In this section, we will first see the signature if the not function in detail given by the Haskell documentation followed by the function details. Let’s get started:
Not function signature:
Bool -> Bool: This is the signature of the not function given by the official documentation of the Haskell, it returns us the Boolean Value. Also, it gives us the two basic rule for this see below;
1) not True == False
2) not False == True
This is given by the official documentation of Haskell for not function in Haskell. By the use of this, we can return a Boolean value from the function based on the evolution of the function. Also, it is an in-built function of the Haskell programming language, so we do not require to install or include any dependency for this. Let’s take a closer look at the return type of not function in Haskell for better understanding see below;
1) Return type: This function will return us the Boolean value in return. It will return us the true or false as the evolution based on the passing variable as the value. we have to keep two conditions in mind which are described below;
1) not True == False: This is the first condition, If the value or variable passed after the not function is evaluated to be true then the not function will return us the false Boolean value as the result. This condition satisfies this statement.
2) not False == True: This is the second condition, If the value or variable passed after the not function is evaluated to be false then the not function will return us the true Boolean value as the result. This condition satisfies this statement.
In this way not function works in Haskell, as we already saw the conditions for this now we will see one sample example for this to understand it better see below;
e.g. :
main = do
print(not (5>10))
As you can see in the above line of code we are trying to compare the value and get the result as Boolean value by using the not function in Haskell. Here we have not to import any packages to use this function inside the Haskell program. it is in built function in Haskell. First, we have defined the main module to use the not function inside it. For this, we have used the not keyword in Haskell programming, and here we are trying to check the numbers, we are checking whether 5 is greater than 10 or not. But we know that this is a false condition so not function will return us True as the result of this. That means it works opposite of equal we can say in Haskell.
Examples
In this example, we are trying to compare the values of the numbers by using the not function in Haskell. Also for this, we are trying to use the in-built not function, this is a sample example for beginners to understand it better and start using it while doing programming in Haskell.
Code:
main = do
print("Demo to show the not function in Haskell !!")
print("using not function here !!")
let val1 = not (5>10)
let val2 = not (2>0)
let val3 = not (100 == 100)
let val4 = not (23 < 78)
let val5 = not ( 10 > 50)
let val6 = not (4 <=90)
let val7 = not (25 > 31)
let val8 = not (1< 0)
print("printing the result here !!")
print("result one is::" , val1)
print("result two is::" , val2)
print("result three is::" , val3)
print("result four is::" , val4)
print("result five is::" , val5)
print("result six is::" , val6)
print("result seven is::" , val7)
print("result eight is::" , val8)
Output:
Conclusion
By the use of not function, we can check the variable value and according to it, we can apply our logic to it. We can have several not functions with some conditions inside it to check the values. This function is very easy to handle and use also; it is readable and easy to understand as well by the developers.
Recommended Articles
We hope that this EDUCBA information on “Haskell not” was beneficial to you. You can view EDUCBA’s recommended articles for more information.