Lecture 2

(["lists", "tuples"], "and data types")

Using These Slides

Every slide has a secret note.

• On Chrome: press F12, then click Console
• On IE: press F12, then click Console
• On Firefox: Ctrl+Shift+k

Shortcut Keys:

 ↓, PgDn, n, j next slide ↑, PgUp, p, k prev slide Esc enables ctrl+f globally

Jumping into GHCi

Make Haskell your calculator for the next few months

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

Functions are Everywhere

Prelude> (+) 2 3
5

f(2, 3) = 2 + 3 = 5

Prelude> (^) 3 4
81

g(3, 4) = 3^4 = 81

Bool

Prelude> (True && False) || False

False

Prelude> True && 1

ERROR!

Prelude> 30 < 31
True

if ... then ... else ...

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

Lists

The most common data type in Haskell

[☺, ☹, ☺]

• elements are comma-separated
• surrounded by square brackets [ ... ]
• an empty list is simply []

Try it out

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!

Enumeration

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]

List Operators

Concatenation (++)

Prelude> let l1 = [1,2,3]
Prelude> let l2 = [4,5,6]
Prelude> l1 ++ l2
[1,2,3,4,5,6]

Construct (:)

Prelude> 0 : [1,2,3]
[0,1,2,3]

Almost Everything is a function

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.

String

A String is just a list of characters.

Prelude> "wahoo" == ['w', 'a', 'h', 'o', 'o']
True

So (++) and (:) work on strings too.

True or False?

Prelude> "" == []

True

Prelude> 'a':"bc" == ['a', 'b', 'c']

True

Prelude> 6:"789" == "6789"

ERROR!

Useful List Functions

• 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

Reverse a List

Create a file myfunction.hs

and write a function to reverse a list

rev [] = []

run ghci and load the file

[1 of 1] Compiling Main             ( myfunction.hs, interpreted )
Ok, modules loaded: Main.

*Main> rev "hello"
"olleh"

Hoogle

The Prelude package comes with many functions.

Reuse, reuse, reuse!

reverse is already implemented for you!

List Comprehension

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

What's the output?

Prelude> [ x+5 | x <- [1,2,3] ]

Prelude> [ x | x <- [2..10], 10 `mod` x  == 0]

Prelude> [ team ++ " " ++ player |
team   <- ["red", "blue"],
player <- ["soldier", "pyro", "scout"] ]

Tuples

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])

Accessing Tuple Elements

• fst retrieves the first element

Prelude> fst (1,2)
1

• snd retrieves the second element

Prelude> snd (1,2)
2

Tuple Examples

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)]

Editors

Grab one of these editors, and start hacking!

Homework

Think Differently

1. Write 2 different implementations of factorial

facA n = ...

facB n = ...

2. Save the file as fac.hs and run it using GHCi

3. Fill out this week's form