Mathematical Operator

Addition Operator :

the addition (+) operator is used for addition of two number.


main = do 
   let var1 = 7 
   let var2 = 3 
   putStrLn "The addition of the two numbers is:" 
   print(var1 + var2) 
   
  // output is 
  10

Subtraction Operator - :

Subtraction operator is used for subtraction operation between two number.


main = do 
   let var1 = 7 
   let var2 = 3 
   putStrLn "The subtraction of the two numbers is:" 
   print(var1 - var2) 
   
  // output is 
  4

Multiplication Operator * :

Multiplication Operator is used for multiplying a number with other numbers


main = do 
   let var1 = 7 
   let var2 = 3 
   putStrLn "The multiplication of the two numbers is:" 
   print(var1 * var2) 
   
  // output is 
  21

Division Operator / :

Division Operator is used for dividing a number and provide result


main = do 
   let var1 = 6
   let var2 = 3 
   putStrLn "The Division of the Two Numbers is:" 
   print(var1/var2)
   
   //output
   2.0

There is no modulo (%) operator in haskell, but we have function for it

Deep Arithmetic Operators

quot :

Returns the quotient of the two numbers. This is the result of division which is then truncated towards zero.


ghci> 6 `quot` 3
2
ghci> 7 `quot` 3
2
ghci>
ghci> 7 `quot` 80
0
ghci>

rem :

Returns the remainder from the quotient.


ghci> 6 `rem` 3
0
ghci> 7 `rem` 2
1
ghci> 7 `rem` 4
3
ghci>

div :

Similar to "quot", but is rounded down towards minus infinity.


ghci> 6 `div` 6
1
ghci> 6 `div` 2
3
ghci> 6 `div` 4
1
ghci>

mod :

Returns the modulus of the two numbers. This is similar to the remainder, but has different rules when "div" returns a negative number.


ghci> 4 `mod` 2
0
ghci> 7 `mod` 2
1
ghci> 7 `mod` 4
3
ghci>



Below are three "raise to the power" operators which work differently and take different argument types.

** :

Takes two floating point numbers and uses logarithms to compute the power. It can also be used with integers but result will be in Floating point number. Even though it squares the number but it will not change the sign(+ or - of the number


ghci> 3 ** 2
9.0
ghci> 4 ** 4
256.0
ghci> 10 ** 3
1000.0
ghci>
ghci> -4 ** 2
-16.0
ghci>

^^ :

Takes a fractional number (i.e. a floating point or a ratio, of which more later) and raises it to a positive or negative integer power.


^ :

Takes any numerical type and raises it to a positive integer power.

Above two did not work for me

Comparison Operators

Numbers, characters and strings can be compared using the usual comparison operators to produce a Bool value

== Equal :

Compares given two value and results True if both value are same otherwise False


ghci> 9 == 9
True
ghci> 9 == 7
False
ghci>

/= Not equal :

Compares given two value and results True if both value are not same otherwise false


ghci> 9 /= 7
True
ghci> 9 /= 9
False
ghci>

< Less than :

Compares the two given value and results True if the left side value is lesser than right side value otherwise result in False


ghci> 9 < 4
False
ghci> 9 < 35
True
ghci>

> Greater than :

Compares the two given value and results True if the left side value is Greater than right side value otherwise result in False


ghci> 9 > 3
True
ghci> 9 > 56
False
ghci>

<= Less than or equal :

Compares two values and if the left side value is lesser or equal to the right side value then results True otherwise False


ghci> 9 <= 78
True
ghci> 9 <= 9
True
ghci> 9 <= 5
False
ghci>

>= Greater than :

Compares two values and if the left side value is greater or equal to the right side value then results True otherwise False


ghci> 9 >= 5
True
ghci> 9 >= 9
True
ghci> 9 >= 67
False
ghci>

Sequence / Range Operator

Sequence or Range is a special operator in Haskell. It is denoted by "(..)". You can use this operator while declaring a list with a sequence of values.

.. Operator generates the interval between the given numbers. if you want to print or generate a list containing 1 to 100 number you can simply use like below.


ghci> [1..30]
[1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30]
ghci>

I same way you can generate the characters as well buy mentioning the from and to alphabet. The list will be generated based on the ASCII values.

 ['a'..'z']
"abcdefghijklmnopqrstuvwxyz"
ghci>
ghci> ['B'..'Z']
"BCDEFGHIJKLMNOPQRSTUVWXYZ"
ghci>
ghci> ['A'..'z']
"ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz"
ghci>


No possible : It is not possible to create a decreasing list with number or with alphabets


ghci> [10..0]
[]
ghci>
ghci> ['a'..'Z']
""
ghci>

About Author

Myself KarthiQ, I am the author of this blog, I know ways to write a good article but some how I donot have the skills to make it to reach people, would you like help me to reach more people By sharing this Article in the social media.

Share this Article Facebook
Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions

Recent Addition

new tutorial Protractor Online Training : I am starting online training course for Protractor with Typescript from 15th November 2018.

You can attend first 3 classes for free, the total course fee is INR 10,000

The course time would be 8.00 PM(IST) for the first three classes

The course time would be 8.00 PM(IST) for the first three classes

If you are interested to learn then you can join the course by sending email to chercher.tech@gmail.com

or Register below


Subscribe
 
Join My Facebook Group
Join Group