𝖄𝕺🌎𝕿𝕽𝕺¥

𝖄𝕺🌎𝕿𝕽𝕺¥

𝕴 𝖉𝖔 𝖒𝖆𝖌𝖎𝖈
github

Haskell Note 1 - Introduction Arithmetic Operators Undefined Variables Lists Strings Types

Adding Modules#

ghci> :module + Data.Ratio

Arithmetic + - * / ** ^ %#

Basic#

ghci> 7.0 / 2.0
3.5
ghci> 7 / 2
3.5

ghci> 2 + 2
4
ghci> (+) 2 2
4

** and ^#

- ** can be used for floating-point exponentiation
ghci> 2.2**3.3
13.489468760533386
- ^ can only be used for integer exponentiation
ghci> 2.2^3.3
<interactive> error:

%#

Both the numerator and denominator of a fraction must be of integer type

ghci>11 % 29
11 % 29
ghci>it :: Ratio Integer
11 % 29

ghci>3.14 % 8
<interactive>:7:1: error:

Special Operators e.g. succ pred sqrt sin truncate round floor ceiling#

ghci>sqrt 16
4.0
ghci>succ 6
7
ghci>succ 7
8
ghci>pred 9
8
ghci>pred 8
7
ghci>sin (pi / 2)
1.0
ghci>truncate pi    --rounding down
3
ghci>round 3.5    --rounding
4
ghci>round 3.4
3
ghci>floor 3.7
3
ghci>ceiling 3.3
4

Operators#

Boolean Logic && ||#

ghci> True && False
False
ghci> False || True
True

0 does not represent False, non-zero values do not represent True

Value Comparison == < > >= <= /= not#

ghci> 1 == 1
True
ghci> 2 < 3
True
ghci> 4 >= 3.99
True
ghci> 1 /= 1    -- not equal to
False
ghci> not True  --not usage
False

Precedence ()#

ghci> :info (+)     --check the precedence of +
class (Eq a, Show a) => Num a where
  (+) :: a -> a -> a
  ...
    -- Defined in GHC.Num
infixl 6 +      --precedence level 6

Higher level means higher precedence

Undefined Variables and Defining Variables e.g. pi , e#

ghci> pi
3.141592653589793

ghci> e
<interactive> Not in scope: `e'
e does not exist, needs to be defined
ghci> let e = exp 1
ghci> e
2.718281828459045

Lists#

Elements in a list must be of the same type
Lists can have any length

[]
ghci> [3,1,3] ++ [3,7]
[3,1,3,3,7]
ghci> 1 : [2,3]
[1,2,3]
ghci> 1 : []
[1]

: can only be used to add an element to the beginning of a list
Can be followed by an empty []

Strings and Characters putStrLn ""#

ghci> "This is a string."
"This is a string."
ghci> putStrLn "Here's a newline -->\n<-- See?"
Here's a newline -->
<-- See?

Text strings are lists of individual characters

ghci> let a = ['l', 'o', 't', 's', ' ', 'o', 'f', ' ', 'w', 'o', 'r', 'k']
ghci> a
"lots of work"
ghci> a == "lots of work"
True
ghci> "" == []
True
ghci> 'a':"bc"
"abc"
ghci> "foo" ++ "bar"
"foobar"

Types#

Type names start with uppercase letters
Variable names start with lowercase letters

:set +t enables the display of types. It is an auxiliary feature of ghci

:unset +t disables the display of types

:type checks the type

Prelude Data.Ratio> :type 'a'
'a' :: Char

Prelude Data.Ratio> "foo"
"foo"

Prelude Data.Ratio> :type it
it :: [Char]
Prelude> :set +t

Prelude> 'c'    -- input expression
'c'             -- output value
it :: Char      -- type of the output value

Prelude> "foo"
"foo"
it :: [Char]

Integer types are represented by Integer. The length of values of the Integer type is only limited by the system's memory.

Loading...
Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.