Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show...

29
Pattern Matching Prof. Clarkson Fall 2018 Today’s music: Blank Space by Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonna be forever // Or it's gonna go down in flames // You can tell me when it's over // If the high was worth the pain

Transcript of Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show...

Page 1: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Pattern Matching

Prof. ClarksonFall 2018

Today’s music: Blank Space by Taylor SwiftI could show you incredible things // Magic, madness, heaven, sin

So it's gonna be forever // Or it's gonna go down in flames // You can tell me when it's over // If the high was worth the pain

Page 2: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Attendance question

The AI Quiz is due tonight. Have you submitted?

A. YesB. NoC. No, but only because I plan to drop the class in

the next 12 hours

You will be unable to submit A0 if you do not submit the AI quiz by the deadline.

Page 3: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or
Page 4: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

PRIMARY summary

CS 3110

Textbook vs. lecture

Page 5: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Search

• Website• Textbook

• Piazza

Page 6: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Section & lecture attendance

about1% each: incentive not penalty

Page 7: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Review

Previously in 3110:• Expressions, definitions• Scope• Functions

Today:• Data types for collections: lists, records, tuples• Pattern matching

Page 8: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Collections

Homogenous elements

Heterogenous elements

Fixed number of elements

Tuples and records

Unbounded number of elements

Lists

Page 9: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

LISTS

Demo

Page 10: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

List implementation

• Immutable: can't change elements• Singly-linked:– Good for sequential access of short-to-medium

length lists (say, up to 10k)– Data structures are tools: none is perfect

• Terminology: nil and cons (from Lisp)

• Next lecture: we'll see the implementation

Page 11: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Question

What is the type of 31::[10]?

A. int

B. int list

C. int int listD. int list list

E. Not well-typed

Page 12: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Question

What is the type of 31::[10]?

A. int

B. int list

C. int int listD. int list list

E. Not well-typed

Page 13: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

RECORDS

Demo

Page 14: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

TUPLES

Demo

Page 15: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Records

Tuples

by name

by position

vs. vs.

Page 16: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Records and tuples

• New kind of definition: type definition• New kinds of types: record types, tuple types

Page 17: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Collections

Homogenous elements

Heterogenous elements

Fixed number of elements

Tuples and records

Unbounded number of elements

Lists

Syntax and semantics: all in the textbook

Page 18: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Question

You want to represent a camel, which has some number of humps and some number of riders. What do you use?

A. TupleB. RecordC. List

Page 19: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

PATTERN MATCHING

Page 20: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Deconstructing data

gpa

"Andrew" "CS"

3.0 false

gpagpa

Data:

Patterns:

Page 21: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Deconstructing data

"Andrew" "CS"

3.0 falsegpa

Pattern does not match data

Page 22: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Deconstructing data

"Andrew" "CS"

3.0 falsegpa

Pattern does match dataExtracts gpa

Page 23: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

PATTERN MATCHING ON LISTS

Demo

Page 24: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Pattern matching

• Match shape of data• Extract part(s) of data

Syntax:match e with | p1 -> e1 | p2 -> e2 | … | pn -> en

p1..pn: pattern expressions

Page 25: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Semantics of pattern matching

• [] matches [] and nothing else

• h :: t– matches 2::[], binding h to 2 and t to []– matches 1::3::[], binding h to 1 and t to 3::[]

• _ matches everything underscore character, called wildcard(it’s like a blank space)

Full details in textbook

Page 26: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Question

match ["taylor"; "swift"] with| [] -> "1989"| h :: t -> h

To what value does the above expression evaluate?A. “taylor”B. “swift”C. “1989”D. []E. h

Page 27: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Question

match ["taylor"; "swift"] with| [] -> "1989"| h :: t -> h

To what value does the above expression evaluate?A. “taylor”B. “swift”C. “1989”D. []E. h

Page 28: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Why pattern matching is INCREDIBLE

1. You can’t forget a case (inexhaustive pattern-match warning)

2. You can’t duplicate a case (unused match case warning)

3. You can’t get an exception(e.g., hd [])

4. Pattern matching leads to elegant, concise, beautiful code

Page 29: Pattern Matching - Cornell University · Today’s music: Blank Spaceby Taylor Swift I could show you incredible things // Magic, madness, heaven, sin So it's gonnabe forever // Or

Upcoming events

• [Wed] A0 dueAdvice: you'll be very sad later in the semester if you use any late days on A0

• [Thu] A1 out

This is incredible.

THIS IS 3110