Programming Languages and Paradigms COMP 302
Prof. Errington
School of Computer Science Mc
Copyright By PowCoder代写 加微信 powcoder
Fall 2022 – Week 3, lesson 1
Announcements
Elections: 3 October. Classes cancelled. (No effect to us.)
Advance polling available. (See #announcements.)
HW1 will be released on Thursday and will be due next Thursday.
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
type height = int
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
type height = int type name = string
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
type height = int
type name = string
type person = name * height
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
type height = int
type name = string
type person = name * height
Defining enumerations:
type color = Red | Green | Blue | Yellow
Last time…
Tracing recursive functions.
Tail call optimization and tail-recursive functions. World of types:
Tuples: (“jake”, 180) : string * int Defining type synonyms
type height = int
type name = string
type person = name * height
Defining enumerations:
type color = Red | Green | Blue | Yellow
Cases of enumerated types can hold values, too:
type value = Numeric of int | Skip | Reverse | Plus2
This time…
Recap and semantics of pattern matching. Recursive types.
Polymorphic types.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable. Each pi is a pattern, i.e.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable. Each pi is a pattern, i.e.
A variable such as x or a wildcard _.
These variables are bound in the corresponding expression ei.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable. Each pi is a pattern, i.e.
A variable such as x or a wildcard _.
These variables are bound in the corresponding expression ei.
A tuple of other patterns: (p1, …, pN)
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable. Each pi is a pattern, i.e.
A variable such as x or a wildcard _.
These variables are bound in the corresponding expression ei.
A tuple of other patterns: (p1, …, pN)
A constructor applied to patterns: Cnstr (p1, …, pN)
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Syntax
e is an expression, usually a variable. Each pi is a pattern, i.e.
A variable such as x or a wildcard _.
These variables are bound in the corresponding expression ei.
A tuple of other patterns: (p1, …, pN)
A constructor applied to patterns: Cnstr (p1, …, pN)
Each ei is an expression to evaluate, contingent on pi matching, together with variable bindings coming from pi.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Operational Semantics
1. Evaluate e to a value v.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Operational Semantics
1. Evaluate e to a value v.
2. Line up v against each pattern pi until we find one that matches.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Operational Semantics
1. Evaluate e to a value v.
2. Line up v against each pattern pi until we find one that
3. Patterns can contain variables; these become bound to the corresponding part of v.
Recap: pattern matching
match e with p1 -> e1 | … | pN -> eN Operational Semantics
1. Evaluate e to a value v.
2. Line up v against each pattern pi until we find one that
3. Patterns can contain variables; these become bound to the
corresponding part of v.
4. The associated expression of the first matching pattern is evaluated with any new bindings and gives the result of the whole match-expression.
Pattern matching recap & recursive types
程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com