Mathematical Operator

Addition Operator :

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

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

  // output is
  10

Subtraction Operator - :

The subtraction operator is used for subtraction operation between two numbers.

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 a 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 the result will be in the 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.

The 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 values are the same otherwise False

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

/= Not equal :

Compares given two value and results True if both values are not the 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 the 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 the 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.

.. The operator generates an interval between the given numbers. if you want to print or generate a list containing 1 to 100 numbers 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 the same way you can generate the characters as well by mentioning the from and to the 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 a number or with alphabets.

ghci> [10..0]
[]
ghci>
ghci> ['a'..'Z']
""
ghci>
About Author :

I am Pavankumar, Having 8.5 years of experience currently working in Video/Live Analytics project.

Comment / Suggestion Section
Point our Mistakes and Post Your Suggestions