Haskell – The Basic Operators

Haskell

Preface

We will learn about the basic Haskell operators, most of which are similar to common programming languages.

Comment

Is not compiled part, just to make notes for coder easy to understand. To comment on a line, we put 2 signs - front.

n :: Int
n = 3
-- n = 5 

And to comment on multiple lines, we put in pairs of signs {- -}

{-
main :: IO()
main = return()
-}

Algebraic operations

For example:

-- Addition 
p1 = 2 + 5  -- Result = 7 

-- Subtraction 
p2 = 10 - 6 -- Result= 4 

-- Multiplication 
p3 = 10 * 10 -- Result= 100 

-- Division 
p4 = 10 / 2 -- Result= 2.0. Note that the result is a real number. 

-- Division takes the remainder 
p5 = 10 `rem` 2 -- Result= 0. 10 divide 2 gives 5 and remainder 0 

-- Power 
p6 = 10 ^ 10 -- Result= 100 
p7 = 2 ^ 3 -- Result= 8.0. The result is the real number type 
p8 = 6 ** 6 -- Result= 46656.0. The result is the real number type

Bitwise operations

Before performing these operations we must use modules Data.Bits by placing commands import Data.Bits Before, we will learn more about the module later. For example:

import Data.Bits

-- AND
255 .&. 170    -- KQ = 170
-- OR
255 .|. 170    -- KQ = 255
-- XOR
255 `xor` 170  -- KQ = 85
-- Invert all the bits
complement 255 -- KQ = -256
-- Left bit shift 
shiftL 6 2     -- KQ = 24, translates the bits of the number 6 to the left 2 bits
-- Right bit shift
shiftR 6 1     -- KQ = 3, translates the bits of number 6 to the right by 1 bit

Comparisons

For example:

-- Bigger
2 > 3   -- False
-- Greater than or equal
2 >= 3  -- False
-- Smaller
2 < 3   -- True
-- Less than or equal
2 <= 3  -- True
-- equal
2 == 3  -- False
-- Different
2 /= 3  -- True

Hope this helps!

Source link

Leave a Reply

Your email address will not be published. Required fields are marked *