COMP302: Programming Languages and Paradigms
Prof. (Sec 01) Francisco Ferreira (Sec 02)
School of Computer Science Mc
Copyright By PowCoder代写 加微信 powcoder
Week 2-1, Fall 2017
Functional Tidbit: Evolution of
“ Pattern matching is so powerful and elegant! […] it’s hard for me to return to languages without pattern-matching capabilities.” ( )
COMP302: Programming Languages and Paradigms 2 / 17
Data Types and Pattern Matching – Continued –
COMP302: Programming Languages and Paradigms 3 / 17
Recap: User-Defined (Non-Recursive Data Types
How can we model a collection of cards?
COMP302: Programming Languages and Paradigms 4 / 17
Recap: User-Defined (Non-Recursive Data Types
How can we model a collection of cards?
Declare a new type together with its elements
1 type suit = Clubs | Spades | Hearts | Diamonds
COMP302: Programming Languages and Paradigms 4 / 17
Recap: How Do We Work with User-Defined Data?
1 type suit = Clubs | Spades | Hearts | Diamonds
COMP302: Programming Languages and Paradigms 5 / 17
Recap: How Do We Work with User-Defined Data?
type suit = Clubs | Spades | Hearts | Diamonds
1 2 3 4 5 6 7 8 9
Pattern Matching
COMP302: Programming Languages and Paradigms
(* dom : suit*suit -> bool
dom(s1,s2) = true
iff suit s1 beats or is equal to suit s2 relative to the ordering S > H > D > C
let rec dom (s1, s2) = match (s1, s2) with
| (Spades ,
| (Hearts ,
| (Hearts ,
| (Diamonds , Clubs)
| (s1, s2)
_) Diamonds) Clubs)
What’s in your hand?
COMP302: Programming Languages and Paradigms 6 / 17
What’s in your hand?
Describe the collection of cards in a hand inductively.
COMP302: Programming Languages and Paradigms 6 / 17
What’s in your hand?
Describe the collection of cards in a hand inductively.
• Empty is of type hand
• Ifc isacard andh isoftypehand,thenHand(c, h) isoftypehand. • Nothing else is of type hand.
COMP302: Programming Languages and Paradigms 6 / 17
What’s in your hand?
Describe the collection of cards in a hand inductively.
• Empty is of type hand
• Ifc isacard andh isoftypehand,thenHand(c, h) isoftypehand. • Nothing else is of type hand.
1 type hand = Empty | Hand of card * hand
COMP302: Programming Languages and Paradigms 6 / 17
Sample Hands
1 2 3 4 5 6 7 8
let hand0:hand = Empty
let hand1:hand = Hand((Ace, Hearts), Empty)
let hand2:hand = Hand((Queen, Diamonds), hand1) let hand5:hand = Hand((Ace, Spades),
Hand((Ten, Diamonds), Hand((Seven, Clubs),
Hand((Queen, Spades), Hand((Eight, Clubs), Empty)))))
COMP302: Programming Languages and Paradigms
Task: Extract it!
Write a function extract: suit -> hand -> hand
extract s h returns a hand containing all cards from h of suit s.
COMP302: Programming Languages and Paradigms 8 / 17
Task: Find it!
Write a function find which when given a rank and a hand, finds the first card in hand of the specified rank and returns its corresponding suit.
COMP302: Programming Languages and Paradigms 9 / 17
Task: Find it!
Write a function find which when given a rank and a hand, finds the first card in hand of the specified rank and returns its corresponding suit.
=⇒ Problem: what to do if no such card exists?
COMP302: Programming Languages and Paradigms 9 / 17
Task: Find it!
Write a function find which when given a rank and a hand, finds the first card in hand of the specified rank and returns its corresponding suit.
=⇒ Problem: what to do if no such card exists? Optional Data Type (predefined)
1 type ’a option = None | Some of ’a
COMP302: Programming Languages and Paradigms 9 / 17
Task: Find it!
Write a function find: rank * hand -> suit option. Given a rank r and a hand h, extract r h
• finds the first card with rank r in h and return its corresponding suit s as Some s.
• returns None, if there is no card with rank r.
COMP302: Programming Languages and Paradigms 10 / 17
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com