Daa ad Rc
Pa
Las eek y init builtin daa e cheer
base types, tuples, lists and strings
writing functions using ae machig and eci
u
4
cat
Z C2
This eek
2
3,4
userde ned daa e
and how to manipulate them using ae machig and eci
more details about eci
R c daa
Wee seen
baetypes: Bool, In, Ineger, Floa
some ways to bild types: given types T1, T2
Intl BoolKhar ICT T
l
Algebraic Daa Tpes a single, powerful technique for building up types to represent complex data Lets you de ne data types
Tuples and lists are ecial cases
functions: T1 -> T2 tuples: (T1, T2) lists: [T1]
T
T
lT IT
T
Tk
Bd daa
Int String
Three key ways to build complex types/values:
1. Prodc pes eachof: a value of T contains a value of T1 ad a value of T2
2. Sm pes oneof: a value of T contains a value of T1 a value of T2 ooo
3. Recrsie pes: a value of T contains a bale of the same type T
t
Pdc
Tuples can do the job but there are two problems…
deadlineDae :: (In, In, In)
deadlineDae = (2, 4, 2019)
deadlineTime :: (In, In, In)
deadlineTime = (11, 59, 59)
— Deade dae eeded b e da
eension :: (In, In, In) -> (In, In, In)
eension = …
Can you spot them?
1. Vb ad adab
A pe snonm for T : a name that can be used interchangeably with T
tpe Dae = (In, In, In) tpe Time = (In, In, In)
deadlineDae :: Dae
deadlineDae = (2, 4, 2019)
deadlineTime :: Time
deadlineTime = (11, 59, 59)
— Deade dae eeded b e da
eension :: Dae -> Dae
eension = …
2. Ua
We want this to fail at compile time!!!
eension deadlineTime
Sli construct two di erent daapes
data Dae = Dae In In In
data Time = Time In In In
— cc^ ^aaee e
deadlineDae :: Dae
deadlineDae = Dae 2 4 2019
deadlineTime :: Time
deadlineTime = Time 11 59 59
Rcd a
Haskells record sna allows you to ame the constructor parameters: Instead of
data Dae = Dae In In In you can write:
data Dae = Dae I monh :: In , da :: In , ear :: In
then you can do:
Fifa.ie7utsiut ilutsDat
Dear mom 1h
Dat
Int
deadlineDae = Dae 2 4 2019
dealineMonh = monh deadlineDae — e, e ed ae a a c
Bddaa Cf
Three key ways to build complex types/values:
1. Prodc pes eachof: a value of T contains a value of T1 ad a value of T2 done
Ind
2. Sm pes oneof: a value of T contains a value of T1 a value of T2 a
3. Recrsie pes: a value of T contains a bale of the same type T
Ea: NaMad
Suppose I want to represent a e dcme with simple markup Each paragraph is either:
plain text Sring
heading: level and text In and Sring
list: ordered? and items Bool and [Sring]
Lol
21017
I want to store all paragraphs in a li
UL
Club
doc = [ (1, “Noes from 130”)
, “There are o pes of langages:”
, (Tre, [“hose people complain abo”, “hose no one ses”]) — Odeed ]
But this de e check!!!
— L 1 ead
— Pa e
S T
Solution: construct a new type for paragraphs that is a m ef the three options! Each paragraph is either:
plain text Sring
heading: level and text In and Sring
list: ordered? and items Bool and [Sring]
data Paragraph
= PTe Sring
PHeading In Sring PLis Bool [Sring]
— ^ 3 cc, / dee aaee
— ^ e : a
— ^ ead: ee ad e (`I` ad `S`)
— ^ : deed? ad e (`B` ad `[S]`)
QUIZ
Term
data Paragraph
= PTe Sring
PHeading In Sring PLis Bool [Sring]
PText
hey
I lTi
What is the type of Te “He here!” ? i.e. How would GHCi reply to:
>: (PTe “He here!”)
A Syntax error
B Type error
C PTe
D Sring I E Paragraph
t
F
o
Cc daa
data T
= C1 T11 … T1k C2 T21 … T2l …
Cn Tn1 … Tnm
T is the ne daape
C1 .. Cn are the consrcors of T
A ale of type T is
eihe C1 1 .. k with i :: T1i C2 1 .. l with i :: T2i
…
Cn 1 .. m with i :: Tni
You can think of a T value as a bo:
eihe a box labeled C1 with values of types T11 .. T1k inside a box labeled C2 with values of types T21 .. T2l inside
…
a box labeled Cn with values of types Tn1 .. Tnm inside
Oneof Types
Apply a constructor pack some values into a box and label it
PTe “He here!”
put “He here!” in a box labeled PTe PHeading 1 “Inrodcion”
put 1 and “Inrodcion” in a box labeled PHeading Boxes have di erent labels but same type Paragraph
The Paragraph Type with example values:
Dat
Di
The Paragraph Type
QUIZ
data Paragraph
= PTe Sring
PHeading In Sring PLis Bool [Sring]
What would GHCi say to
>: [PHeading 1 “Inrodcion”, Pe “He here!”]
A Syntax error B Type error
C Paragraph D [Paragraph] E [Sring]
Ea: NaMD
T
data Paragraph
= PTe Sring
PHeading In Sring PLis Bool [Sring]
Now I can create a document like so:
doc :: [Paragraph]
doc = [ PHeading 1 “Noes from 130”
, PTe “There are o pes of langages:”
, PLis Tre [“hose people complain abo”, “hose no one ses”])
]
Now I want coner docmens in o HTML. I need to write a function:
hml :: Paragraph -> Sring
hml p = ??? — deed e d aaa!
How to tell whats in the box?
Look at the label!
Pa ac
Paern maching looking at the label and extracting values from the box weve seen it before
but now for arbitrary datatypes
O Fish
hml :: Paragraph -> Sring
hml (PTe sr) = … — I’ a a e! Ge
hml (PHeading ll sr) = … — I’ a ead! Ge ee ad hml (PLis ord iems) = … — I’ a ! Ge deed ad e
IRod tr
hml :: Paragraph -> Sring
hml (PTe sr) — I’ a a e! Ge
= nlines [open “p”, sr, close “p”]
hml (PHeading ll sr) — I’ a ead! Ge ee ad = let hag = “h” ++ sho ll
in nords [open hag, sr, close hag]
hml (PLis ord iems) — I’ a ! Ge deed ad e = let lag = if ord then “ol” else “l”
liems = [nords [open “li”, i, close “li”] i <- iems] in nlines ([open lag] ++ liems ++ [close lag])
Da a ac (1)
hml :: Paragraph -> Sring
hml (PTe sr) = …
hml (PLis ord iems) = …
What would GHCi say to:
hml (PHeading 1 “Inrodcion”)
Da a ac (2)
hml :: Paragraph -> Sring
hml (PTe sr) = nlines [open “p”, sr, close “p”]
hml (PHeading ll sr) = …
hml (PHeading 0 sr) = hml (PHeading 1 sr)
hml (PLis ord iems) = …
What would GHCi say to:
hml (PHeading 0 “Inrodcion”)
Da a ac
Beware of missing and oerlapped patterns
GHC warns you about elaed patterns
GHC warns you about miig patterns when called with -W use :se -W in GHCi
o
Pa-Mac E
Eehig i a eei
Weve seen: pattern matching in eai Actually, patternmatch is al a eei
hml :: Paragraph -> SringD hml p = case p of
PTe sr -> nlines [open “p”, sr, close “p”]
PHeading ll sr -> …
PLis ord iems -> …
The code we saw earlier was acic ga
hml (C1 1 …) = e1
hml (C2 2 …) = e2
hml (C3 3 …) = e3
is just for hma, internally represented as a case-of expression
hml p = case p of
(C1 1 …) -> e1
(C2 2 …) -> e2
(C3 3 …) -> e3
QUIZ
What is the pe of
let p = Te “He here!” in case p of
PTe sr -> sr
A Syntax error iaawas
B Type error
C Sring
D Paragraph
E Paragraph -> Sring
PHeading ll _ -> ll
PLis ord _ -> ord
i
Pa ac :
The case expression
case e of paern1 -> e1 paern2 -> e2 …
paernN -> eN
has type T if
each e1 … eN has type T e has some type D
each paern1 … paernN is a alid ae for D
i.e. a variable or a constructor of D applied to other patterns
The expression e is called the mach scrinee
QUIZ
What is the type of
let p = Te “He here!” in case p of
PTe _ -> 1
PHeading _ _ -> 2
PLis _ _ -> 3
A Syntax error B Type error C Paragraph D In
E Paragraph -> In
Bd daa