CS计算机代考程序代写 Haskell Da􏰀a􏰀􏰁􏰂􏰃􏰄 a􏰅d R􏰃c􏰆􏰇􏰄􏰈􏰉􏰅

Da􏰀a􏰀􏰁􏰂􏰃􏰄 a􏰅d R􏰃c􏰆􏰇􏰄􏰈􏰉􏰅
P􏰊a􏰅 􏰋􏰉􏰇 􏰀􏰌􏰈􏰄 􏰍􏰃􏰃􏰎
Las􏰏 􏰐eek􏰑 y init built􏰒in da􏰓a 􏰓􏰔􏰕e􏰖 cheer
base types, tuples, lists 􏰗and strings􏰘
writing functions using 􏰕a􏰓􏰓e􏰙􏰚 ma􏰓chi􏰚g and 􏰙ec􏰛􏰙􏰖i􏰜􏰚
u
4
cat
Z C2
This 􏰐eek􏰑
2
3,4

user􏰒dened da􏰓a 􏰓􏰔􏰕e􏰖
and how to manipulate them using 􏰕a􏰓􏰓e􏰙􏰚 ma􏰓chi􏰚g and 􏰙ec􏰛􏰙􏰖i􏰜􏰚
more details about 􏰙ec􏰛􏰙􏰖i􏰜􏰚
R􏰃􏰂􏰇􏰃􏰄􏰃􏰅􏰀􏰈􏰅􏰝 c􏰉􏰞􏰂􏰊􏰃􏰟 da􏰀a
We􏰠􏰡e seen􏰑

ba􏰖etypes: Bool, In􏰀, In􏰀eger, Floa􏰀
some ways to b􏰛ild 􏰛􏰕 types: given types T1, T2
Intl BoolKhar ICT T
l
Algebraic Da􏰏a T􏰢pes􏰑 a single, powerful technique for building up types to represent complex data Lets you dene 􏰔􏰜􏰛􏰙 􏰜􏰣􏰚 data types
Tuples and lists are 􏰖􏰕ecial cases
functions: T1 -> T2 tuples: (T1, T2) lists: [T1]
T
T
lT IT
T
Tk
B􏰆􏰈􏰊d􏰈􏰅􏰝 da􏰀a 􏰀􏰁􏰂􏰃􏰄

Int String
Three key ways to build complex types/values:
1. Prod􏰓c􏰏 􏰏􏰢pes 􏰗each􏰒of􏰘: a value of T contains a value of T1 a􏰚d a value of T2
2. S􏰓m 􏰏􏰢pes 􏰗one􏰒of􏰘: a value of T contains a value of T1 􏰜􏰙 a value of T2 ooo
3. Rec􏰓rsi􏰡e 􏰏􏰢pes: a value of T contains a 􏰖􏰛b􏰤􏰐al􏰛e of the same type T
t
P􏰇􏰉d􏰆c􏰀 􏰀􏰁􏰂􏰃􏰄
Tuples can do the job but there are two problems…

deadlineDa􏰀e :: (In􏰀, In􏰀, In􏰀)
deadlineDa􏰀e = (2, 4, 2019)
deadlineTime :: (In􏰀, In􏰀, In􏰀)
deadlineTime = (11, 59, 59)
— 􏰥 Dead􏰊􏰈􏰅e da􏰀e e􏰟􏰀e􏰅ded b􏰁 􏰉􏰅e da􏰁
e􏰟􏰀ension :: (In􏰀, In􏰀, In􏰀) -> (In􏰀, In􏰀, In􏰀)
e􏰟􏰀ension = …
Can you spot them?
1. V􏰃􏰇b􏰉􏰄􏰃 a􏰅d 􏰆􏰅􏰇􏰃adab􏰊􏰃
A 􏰏􏰢pe s􏰢non􏰢m for T : a name that can be used interchangeably with T

t􏰁pe Da􏰀e = (In􏰀, In􏰀, In􏰀) t􏰁pe Time = (In􏰀, In􏰀, In􏰀)
deadlineDa􏰀e :: Da􏰀e
deadlineDa􏰀e = (2, 4, 2019)
deadlineTime :: Time
deadlineTime = (11, 59, 59)
— 􏰥 Dead􏰊􏰈􏰅e da􏰀e e􏰟􏰀e􏰅ded b􏰁 􏰉􏰅e da􏰁
e􏰟􏰀ension :: Da􏰀e -> Da􏰀e
e􏰟􏰀ension = …
2. U􏰅􏰄a􏰋􏰃
We want this to fail at compile time!!!
e􏰟􏰀ension deadlineTime

S􏰜l􏰛􏰓i􏰜􏰚􏰦 construct two dierent da􏰏a􏰏􏰢pes
data Da􏰀e = Da􏰀e In􏰀 In􏰀 In􏰀
data Time = Time In􏰀 In􏰀 In􏰀
— c􏰉􏰅􏰄􏰀􏰇􏰆c􏰀􏰉􏰇^ ^􏰂a􏰇a􏰞e􏰀e􏰇 􏰀􏰁􏰂e􏰄
deadlineDa􏰀e :: Da􏰀e
deadlineDa􏰀e = Da􏰀e 2 4 2019
deadlineTime :: Time
deadlineTime = Time 11 59 59
R􏰃c􏰉􏰇d 􏰄􏰁􏰅􏰀a􏰟

Haskell􏰠s record s􏰢n􏰏a􏰣 allows you to 􏰚ame the constructor parameters: Instead of
data Da􏰀e = Da􏰀e In􏰀 In􏰀 In􏰀 you can write:
data Da􏰀e = Da􏰀e I􏰧 mon􏰀h :: In􏰀 , da􏰁 :: In􏰀 , 􏰁ear :: In􏰀
􏰚
then you can do:
Fifa.ie7utsiut ilutsDat
Dear mom 1h
Dat
Int
deadlineDa􏰀e = Da􏰀e 2 4 2019
dealineMon􏰀h = mon􏰀h deadlineDa􏰀e — 􏰁􏰈􏰎e􏰄, 􏰆􏰄e 􏰋􏰈e􏰊d 􏰅a􏰞e a􏰄 a 􏰋􏰆􏰅c􏰀􏰈􏰉􏰅

B􏰆􏰈􏰊d􏰈􏰅􏰝da􏰀a􏰀􏰁􏰂􏰃􏰄 Cf
Three key ways to build complex types/values:
1. Prod􏰓c􏰏 􏰏􏰢pes 􏰗each􏰒of􏰘: a value of T contains a value of T1 a􏰚d a value of T2 􏰨done􏰩
Ind
2. S􏰓m 􏰏􏰢pes 􏰗one􏰒of􏰘: a value of T contains a value of T1 􏰜􏰙 a value of T2 a
3. Rec􏰓rsi􏰡e 􏰏􏰢pes: a value of T contains a 􏰖􏰛b􏰤􏰐al􏰛e of the same type T
E􏰟a􏰞􏰂􏰊􏰃: Na􏰅􏰉Ma􏰇􏰎d􏰉􏰍􏰅

Suppose I want to represent a 􏰓e􏰢􏰓 d􏰜c􏰛me􏰚􏰓 with simple markup Each paragraph is either:
plain text 􏰗 S􏰀ring 􏰘
heading: level and text 􏰗 In􏰀 and S􏰀ring 􏰘
list: ordered? and items 􏰗 Bool and [S􏰀ring] 􏰘
Lol
21017
I want to store all paragraphs in a li􏰖􏰓
UL
Club
doc = [ (1, “No􏰀es from 130”)
, “There are 􏰀􏰍o 􏰀􏰁pes of lang􏰆ages:”
, (Tr􏰆e, [“􏰀hose people complain abo􏰆􏰀”, “􏰀hose no one 􏰆ses”]) — O􏰇de􏰇ed 􏰊􏰈􏰄􏰀 ]
But this d􏰜e􏰖 􏰚􏰜􏰓 􏰓􏰔􏰕e check!!!
— L􏰪􏰊 1 􏰌ead􏰈􏰅􏰝
— P􏰊a􏰈􏰅 􏰀e􏰟􏰀

S􏰆􏰞 T􏰁􏰂􏰃􏰄
Solution: construct a new type for paragraphs that is a 􏰖􏰛m 􏰗􏰜􏰚e􏰤􏰜f􏰘 the three options! Each paragraph is either:
plain text 􏰗 S􏰀ring 􏰘
heading: level and text 􏰗 In􏰀 and S􏰀ring 􏰘
list: ordered? and items 􏰗 Bool and [S􏰀ring] 􏰘
data Paragraph
= PTe􏰟􏰀 S􏰀ring
􏰥 PHeading In􏰀 S􏰀ring 􏰥 PLis􏰀 Bool [S􏰀ring]
— ^ 3 c􏰉􏰅􏰄􏰀􏰇􏰆c􏰀􏰉􏰇􏰄, 􏰍/ d􏰈􏰋􏰋e􏰇e􏰅􏰀 􏰂a􏰇a􏰞e􏰀e􏰇􏰄
— ^ 􏰀e􏰟􏰀 : 􏰂􏰊a􏰈􏰅 􏰄􏰀􏰇􏰈􏰅􏰝
— ^ 􏰌ead􏰈􏰅􏰝: 􏰊e􏰪e􏰊 a􏰅d 􏰀e􏰟􏰀 (`I􏰅􏰀` a􏰅d `S􏰀􏰇􏰈􏰅􏰝`)
— ^ 􏰊􏰈􏰄􏰀 : 􏰉􏰇de􏰇ed? a􏰅d 􏰈􏰀e􏰞􏰄 (`B􏰉􏰉􏰊` a􏰅d `[S􏰀􏰇􏰈􏰅􏰝]`)
QUIZ
Term

data Paragraph
= PTe􏰟􏰀 S􏰀ring
􏰥 PHeading In􏰀 S􏰀ring 􏰥 PLis􏰀 Bool [S􏰀ring]
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􏰬 S􏰀ring I E􏰬 Paragraph
t
F
o

C􏰉􏰅􏰄􏰀􏰇􏰆c􏰀􏰈􏰅􏰝 da􏰀a􏰀􏰁􏰂􏰃􏰄
data T
= C1 T11 … T1k 􏰥 C2 T21 … T2l 􏰥 …
􏰥 Cn Tn1 … Tnm
T is the ne􏰐 da􏰏a􏰏􏰢pe
C1 .. Cn are the cons􏰏r􏰓c􏰏ors of T
A 􏰡al􏰓e of type T is
ei􏰓he􏰙 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􏰣:

ei􏰓he􏰙 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
One􏰒of 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 “In􏰀rod􏰆c􏰀ion”
put 1 and “In􏰀rod􏰆c􏰀ion” in a box labeled PHeading Boxes have dierent labels but same type 􏰗 Paragraph 􏰘

The Paragraph Type with example values:
Dat
Di
The Paragraph Type
QUIZ
data Paragraph
= PTe􏰟􏰀 S􏰀ring
􏰥 PHeading In􏰀 S􏰀ring 􏰥 PLis􏰀 Bool [S􏰀ring]

What would GHCi say to
>:􏰀 [PHeading 1 “In􏰀rod􏰆c􏰀ion”, Pe􏰟􏰀 “He􏰁 􏰀here!”]
A􏰬 Syntax error B􏰬 Type error
C􏰬 Paragraph D􏰬 [Paragraph] E􏰬 [S􏰀ring]
E􏰟a􏰞􏰂􏰊􏰃: Na􏰅􏰉MD
T

data Paragraph
= PTe􏰟􏰀 S􏰀ring
􏰥 PHeading In􏰀 S􏰀ring 􏰥 PLis􏰀 Bool [S􏰀ring]
Now I can create a document like so:
doc :: [Paragraph]
doc = [ PHeading 1 “No􏰀es from 130”
, PTe􏰟􏰀 “There are 􏰀􏰍o 􏰀􏰁pes of lang􏰆ages:”
, PLis􏰀 Tr􏰆e [“􏰀hose people complain abo􏰆􏰀”, “􏰀hose no one 􏰆ses”])
]
Now I want con􏰡er􏰏 doc􏰓men􏰏s in 􏰏o HTML. I need to write a function:
h􏰀ml :: Paragraph -> S􏰀ring
h􏰀ml p = ??? — de􏰂e􏰅d􏰄 􏰉􏰅 􏰀􏰌e 􏰎􏰈􏰅d 􏰉􏰋 􏰂a􏰇a􏰝􏰇a􏰂􏰌!
How to tell what􏰠s in the box?

Look at the label!
Pa􏰀􏰀􏰃􏰇􏰅 􏰞a􏰀c􏰌􏰈􏰅􏰝
Pa􏰏􏰏ern ma􏰏ching 􏰭 looking at the label and extracting values from the box we􏰠ve seen it before
but now for arbitrary datatypes
O Fish
h􏰀ml :: Paragraph -> S􏰀ring
h􏰀ml (PTe􏰟􏰀 s􏰀r) = … — I􏰀’􏰄 a 􏰂􏰊a􏰈􏰅 􏰀e􏰟􏰀! Ge􏰀 􏰄􏰀􏰇􏰈􏰅􏰝
h􏰀ml (PHeading l􏰪l s􏰀r) = … — I􏰀’􏰄 a 􏰌ead􏰈􏰅􏰝! Ge􏰀 􏰊e􏰪e􏰊 a􏰅d 􏰄􏰀􏰇􏰈􏰅􏰝 h􏰀ml (PLis􏰀 ord i􏰀ems) = … — I􏰀’􏰄 a 􏰊􏰈􏰄􏰀! Ge􏰀 􏰉􏰇de􏰇ed a􏰅d 􏰈􏰀e􏰞􏰄
IRod tr

h􏰀ml :: Paragraph -> S􏰀ring
h􏰀ml (PTe􏰟􏰀 s􏰀r) — I􏰀’􏰄 a 􏰂􏰊a􏰈􏰅 􏰀e􏰟􏰀! Ge􏰀 􏰄􏰀􏰇􏰈􏰅􏰝
= 􏰆nlines [open “p”, s􏰀r, close “p”]
h􏰀ml (PHeading l􏰪l s􏰀r) — I􏰀’􏰄 a 􏰌ead􏰈􏰅􏰝! Ge􏰀 􏰊e􏰪e􏰊 a􏰅d 􏰄􏰀􏰇􏰈􏰅􏰝 = let h􏰀ag = “h” ++ sho􏰍 l􏰪l
in 􏰆n􏰍ords [open h􏰀ag, s􏰀r, close h􏰀ag]
h􏰀ml (PLis􏰀 ord i􏰀ems) — I􏰀’􏰄 a 􏰊􏰈􏰄􏰀! Ge􏰀 􏰉􏰇de􏰇ed a􏰅d 􏰈􏰀e􏰞􏰄 = let l􏰀ag = if ord then “ol” else “􏰆l”
li􏰀ems = [􏰆n􏰍ords [open “li”, i, close “li”] 􏰥 i <- i􏰀ems] in 􏰆nlines ([open l􏰀ag] ++ li􏰀ems ++ [close l􏰀ag]) Da􏰅􏰝􏰃􏰇􏰄 􏰉􏰋 􏰂a􏰀􏰀􏰃􏰇􏰅 􏰞a􏰀c􏰌􏰈􏰅􏰝 (1) h􏰀ml :: Paragraph -> S􏰀ring
h􏰀ml (PTe􏰟􏰀 s􏰀r) = …
h􏰀ml (PLis􏰀 ord i􏰀ems) = …
What would GHCi say to:
h􏰀ml (PHeading 1 “In􏰀rod􏰆c􏰀ion”)
Da􏰅􏰝􏰃􏰇􏰄 􏰉􏰋 􏰂a􏰀􏰀􏰃􏰇􏰅 􏰞a􏰀c􏰌􏰈􏰅􏰝 (2)
h􏰀ml :: Paragraph -> S􏰀ring
h􏰀ml (PTe􏰟􏰀 s􏰀r) = 􏰆nlines [open “p”, s􏰀r, close “p”]
h􏰀ml (PHeading l􏰪l s􏰀r) = …
h􏰀ml (PHeading 0 s􏰀r) = h􏰀ml (PHeading 1 s􏰀r)
h􏰀ml (PLis􏰀 ord i􏰀ems) = …
What would GHCi say to:
h􏰀ml (PHeading 0 “In􏰀rod􏰆c􏰀ion”)

Da􏰅􏰝􏰃􏰇􏰄 􏰉􏰋 􏰂a􏰀􏰀􏰃􏰇􏰅 􏰞a􏰀c􏰌􏰈􏰅􏰝
Beware of missing and o􏰡erlapped patterns
GHC warns you about 􏰜􏰐e􏰙la􏰕􏰕ed patterns
GHC warns you about mi􏰖􏰖i􏰚g patterns when called with -W 􏰗use :se􏰀 -W in GHCi􏰘
o
Pa􏰀􏰀􏰃􏰇􏰅-Ma􏰀c􏰌 E􏰟􏰂􏰇􏰃􏰄􏰄􏰈􏰉􏰅

E􏰐e􏰙􏰔􏰓hi􏰚g i􏰖 a􏰚 e􏰢􏰕􏰙e􏰖􏰖i􏰜􏰚􏰮
We􏰠ve seen: pattern matching in e􏰯􏰛a􏰓i􏰜􏰚􏰖 Actually, pattern􏰒match is al􏰖􏰜 a􏰚 e􏰢􏰕􏰙e􏰖􏰖i􏰜􏰚
h􏰀ml :: Paragraph -> S􏰀ringD h􏰀ml p = case p of
PTe􏰟􏰀 s􏰀r -> 􏰆nlines [open “p”, s􏰀r, close “p”]
PHeading l􏰪l s􏰀r -> …
PLis􏰀 ord i􏰀ems -> …
The code we saw earlier was 􏰖􏰔􏰚􏰓ac􏰓ic 􏰖􏰛ga􏰙

h􏰀ml (C1 􏰟1 …) = e1
h􏰀ml (C2 􏰟2 …) = e2
h􏰀ml (C3 􏰟3 …) = e3
is just for h􏰛ma􏰚􏰖, internally represented as a case-of expression
h􏰀ml 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􏰟􏰀 s􏰀r -> s􏰀r
A􏰬 Syntax error iaawas
B􏰬 Type error
C􏰬 S􏰀ring
D􏰬 Paragraph
E􏰬 Paragraph -> S􏰀ring
PHeading l􏰪l _ -> l􏰪l
PLis􏰀 ord _ -> ord
i

Pa􏰀􏰀􏰃􏰇􏰅 􏰞a􏰀c􏰌􏰈􏰅􏰝 􏰃􏰟􏰂􏰇􏰃􏰄􏰄􏰈􏰉􏰅: 􏰀􏰁􏰂􏰈􏰅􏰝
The case expression
case e of pa􏰀􏰀ern1 -> e1 pa􏰀􏰀ern2 -> e2 …
pa􏰀􏰀ernN -> eN
has type T if
each e1 … eN has type T e has some type D
each pa􏰀􏰀ern1 … pa􏰀􏰀ernN is a 􏰐alid 􏰕a􏰓􏰓e􏰙􏰚 for D
i.e. a variable or a constructor of D applied to other patterns
The expression e is called the ma􏰏ch scr􏰓􏰏inee

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􏰀

B􏰆􏰈􏰊d􏰈􏰅􏰝 da􏰀a 􏰀􏰁􏰂􏰃􏰄

Leave a Reply

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