Lecture 2
(["lists", "tuples"], "and data types")
F12, then click ConsoleF12, then click ConsoleCtrl+Shift+k↓, PgDn, n, j |
next slide |
↑, PgUp, p, k |
prev slide |
Esc |
enables ctrl+f globally |
Prelude> 4317 * 103
444651
Prelude> let x = 3.0
Prelude> let y = 4.0
Prelude> sqrt( x^2 + y^2 )
5.0
Prelude> 2^1000
10715086071862673209484250490600018105614048117055336074437503883703510511249361224931983788156958581275946729175531468251871452856923140435984577574698574803934567774824230985421074605062371141877954182153046474983581941267398767559165543946077062914571196477686542167660429831652624386837205668069376
Prelude> (+) 2 3
5
f(2, 3) = 2 + 3 = 5
Prelude> (^) 3 4
81
g(3, 4) = 3^4 = 81
Prelude> (True && False) || False
False
Prelude> True && 1
ERROR!
Prelude> 30 < 31
True
Lets define a function royal
Prelude> let royal p = if p>9000 then True else False
Prelude> royal 9001
True
Lets also define a related function peasant
Prelude> let peasant p = not (royal p)
Prelude> peasant 9001
False
Reusing code is easy
The most common data type in Haskell
Prelude> [3,1,5,3]
[3,1,5,3]
Prelude> ["list","of","strings"]
["list","of","strings"]
The elements must be of the same type
Prelude> [1,2,3,"a","bb","ccc"]
ERROR!
Start at 1, end at 10
Prelude> [1..10]
[1,2,3,4,5,6,7,8,9,10]
Start at 1, count up by 0.25, end at 4
Prelude> [1, 1.25 .. 4.0]
[1.0,1.25,1.5,1.75,2.0,2.25,2.5,2.75,3.0,3.25,3.5,3.75,4.0]
Prelude> let l1 = [1,2,3]
Prelude> let l2 = [4,5,6]
Prelude> l1 ++ l2
[1,2,3,4,5,6]
Prelude> 0 : [1,2,3]
[0,1,2,3]
Prelude> let l1 = [1,2,3]
Prelude> let l2 = [4,5,6]
Prelude> (++) l1 l2
[1,2,3,4,5,6]
Prelude> (:) 0 [1,2,3]
[0,1,2,3]
In fact, a list is formally defined like this
Prelude> [1,2,3] == (:) 1 ((:) 2 ((:) 3 []))
True
Gross! We'll put that away. I'm so sorry.
A String is just a list of characters.
Prelude> "wahoo" == ['w', 'a', 'h', 'o', 'o']
True
So (++) and (:) work on strings too.
Prelude> "" == []
True
Prelude> 'a':"bc" == ['a', 'b', 'c']
True
Prelude> 6:"789" == "6789"
ERROR!
head returns the first element of a list
tail returns the remaining elements of a list
last returns the last element of a list
init returns everything but the last element
image © Miran Lipovača: Learn You a Haskell
Create a file myfunction.hs
and write a function to reverse a list
rev [] = []
rev [] = []
rev xs = last xs : rev (init xs)
rev [] = []
rev xs = rev (tail xs) ++ [head xs]
rev [] = []
rev (x:xs) = reverse xs ++ [x]
(clear answer)
(show answer 1)
(show answer 2)
(show answer 3)
run ghci and load the file
Prelude> :load myfunction.hs
[1 of 1] Compiling Main ( myfunction.hs, interpreted )
Ok, modules loaded: Main.
*Main> rev "hello"
"olleh"
The Prelude package comes with many functions.
Reuse, reuse, reuse!
reverse is already implemented for you!
GG, Haskell
Let S be a set, and p(x) be a predicate.
Mathematically, we can write the following statement.
∀x∈S, p(x)
Haskell allows a similar notation
[ x | x ← S, p x ]
← is pronounced "drawn from"
p x is called a guard
Prelude> [ x+5 | x <- [1,2,3] ]
Prelude> [ x+5 | x <- [1,2,3] ]
[6,7,8]
(clear answer)
(show answer)
Prelude> [ x | x <- [2..10], 10 `mod` x == 0]
Prelude> [ x | x <- [2..10], 10 `mod` x == 0]
[2,5,10]
(clear answer)
(show answer)
Prelude> [ team ++ " " ++ player |
team <- ["red", "blue"],
player <- ["soldier", "pyro", "scout"] ]
Prelude> [ team ++ " " ++ player |
team <- ["red", "blue"],
player <- ["soldier", "pyro", "scout"] ]
["red soldier","red pyro","red scout","blue soldier","blue pyro","blue scout"]
(clear answer)
(show answer)
A tuple is a binding of data types
It's comma-separated, just like lists
A tuple is surrounded by ( ... )
(1, 2)("lol", "wut")(40, "forty")("Coins", [1, 5, 10, 25])fst retrieves the first element
Prelude> fst (1,2)
1
snd retrieves the second element
Prelude> snd (1,2)
2
Prelude> [ (n, even n) | n <- [1..4] ]
[(1,False),(2,True),(3,False),(4,True)]
Prelude> [(a,b,c) | c<-[1..10], b<-[1..c], a<-[1..b]
, a^2 + b^2 == c^2]
[(3,4,5),(6,8,10)]
Grab one of these editors, and start hacking!
Write 2 different implementations of factorial
facA n = ...
facB n = ...
Save the file as fac.hs and run it using GHCi