Title: Patterns
1ML Again ( Chapter 7)
- Patterns
- Local variable definitions
- A sorting example
2- fun f n nn
- fun f (a, b) ab
Both n and (a, b) are patterns.
n is a pattern of integers (a, b) is a pattern
of 2-tuple of integers.
For match and bind (most of the time)
3Underscore is a Pattern
- fun f _ "yes" val f fn 'a -gt string - f
34.5 val it "yes" string - f val it
"yes" string
- The underscore matches anything,
- but does not bind it to a variable
fun f x "yes"
4Constants are Patterns
- fun f 0 "yes" Warning match nonexhaustive
0 gt ... val f fn int -gt string - f
0 val it "yes" string
- Any constant of an equality type can be used as a
pattern - But not fun f 0.0 "yes"
5Non-Exhaustive Match
- match non-exhaustive warning
- Meaning f was defined using a pattern that
didnt cover all the domain type (int) - So you may get runtime errors like this
- f 0 val it "yes" string - f 1 uncaught
exception nonexhaustive match failure
6Lists Of Patterns As Patterns
- fun f a,_ a Warning match nonexhaustive
a _ nil gt ... val f fn 'a list
-gt 'a - f "f","g" val it "f" char
- You can use a list of patterns as a pattern
- This example matches any list of length 2
- It treats a and _ as sub-patterns, binding a to
the first list element
Why not 1?
7Cons Of Patterns As A Pattern
- fun f (xxs) x Warning match
nonexhaustive x xs gt ... val f fn
'a list -gt 'a - f 1,2,3 val it 1 int
- You can use a cons of patterns as a pattern
- xxs matches any non-empty list, and binds x to
the head and xs to the tail - Parentheses around xxs are for precedence
8Little Summary
- A variable is a pattern that matches anything,
and binds to it - A _ is a pattern that matches anything
- A constant (of an equality type) is a pattern
that matches only that constant - A tuple of patterns is a pattern that matches any
tuple of the right size, whose contents match the
sub-patterns - A list of patterns is a pattern that matches any
list of the right size, whose contents match the
sub-patterns - A cons () of patterns is a pattern that matches
any non-empty list whose head and tail match the
sub-patterns
9Multiple Patterns for Functions
- fun f 0 "zero" f 1 "one" Warning
match nonexhaustive 0 gt ...
1 gt ... val f fn int -gt string - f 1 val
it "one" string
- You can define a function by listing alternate
patterns
10Syntax
ltfun-defgt fun ltfun-bodiesgt ltfun-bodiesgt
ltfun-bodygt ltfun-bodiesgt ltfun-bodygt
ltfun-bodiesgt ltfun-bodygt ltfun-namegt ltpatterngt
ltexpressiongt
- To list alternate patterns for a function
- You must repeat the function name in each
alternative
We just hit the limitation of CFG
11Overlapping Patterns
- fun f 0 "zero" f _ "non-zero" val f
fn int -gt string - f 0 val it "zero"
string - f 34val it "non-zero" string
- Patterns may overlap
- ML uses the first match for a given argument
12Pattern-Matching Style
- Two alternatives
- fun f 0 "zero" f _ "non-zero"
- fun f n if n 0
- then "zero" else "non-zero"
- Pattern-matching style is preferred in ML
- shorter and more legible
13Pattern-Matching Examples
fun fact n if n 0 then 1 else n
fact(n-1)
fun fact 0 1 fact n n fact(n-1)
fun reverse L if null L then nil else
reverse(tl L) _at_ hd L
fun reverse nil nil reverse (firstrest)
reverse rest_at_first
14A Restriction
How to explain this?
fun f (a,a) f (a,b)
- This is not legal, but why?
fun f (a,b) if (ab) then else
15Patterns in val
- val (a,b) (1,2.3) val a 1 int val b
2.3 real - val ab 1,2,3,4,5 Warning
binding not exhaustive a b ... val
a 1 int val b 2,3,4,5 int list
16Local Variable Definitions
ltlet-expgt let ltdefinitionsgt in ltexpressiongt
end
- let val x 1 val y 2 in xy end val it 3
int - x Error unbound variable or
constructor x
let val x 1 val y 2 in xy End
This alone is not very useful! We can put it
into a function definition
17Long Expressions with let
fun days2ms days days24.060.060.01000.0
fun days2ms days let val hours days
24.0 val minutes hours 60.0 val
seconds minutes 60.0 in seconds
1000.0 end
18Merge two sorted lists
1
2
6
3
5
7
8
19Merge Sort
20How to split
In ML, its a bit difficult to do this
We have hd, tl and
7
3,8,2,1,6
5
Recursively
21halve
fun halve nil (nil, nil) halve a (a,
nil) halve (abcs) let val
(x, y) halve cs in (ax, by)
end
- -halve 1
- val it (1,) int list int list
- halve 1,2
- val it (1,2) int list int list
- - halve 1,2,3,4,5,6
- val it (1,3,5,2,4,6) int list int list
22merge
Recursively
7
xs
5
ys
5
fun merge (nil, ys) ys merge (xs, nil)
xs merge (xxs, yys) if (x lt y)
then x merge(xs, yys) else y
merge(xxs, ys)
23Merge Sort
fun mergeSort nil nil mergeSort a a
mergeSort theList let val (x,
y) halve theList in
merge(mergeSort x, mergeSort y) end
24Merge Sort At Work
These two functions can be defined locally.
- fun mergeSort nil nil mergeSort a
a mergeSort theList let
val (x, y) halve theList in
merge(mergeSort x, mergeSort y) end val
mergeSort fn int list -gt int list - mergeSort
4,3,2,1 val it 1,2,3,4 int list -
mergeSort 4,2,3,1,5,3,6 val it
1,2,3,3,4,5,6 int list
25fun mergeSort nil nil mergeSort e e
mergeSort theList let ( fun and var
defined locally ) fun halve nil (nil,
nil) halve a (a, nil)
halve (abcs) let val (x,
y) halve cs in (ax, by)
end fun merge (nil, ys) ys
merge (xs, nil) xs merge
(xxs, yys) if (x lt y) then x
merge(xs, yys) else y
merge(xxs, ys) val (x, y) halve theList
in merge(mergeSort x, mergeSort y)
end
26Quick Sort
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
lt
27Quick Sort
4
8
14
3
16
2
28 private void SwapTwo(int A, int i, int j)
int temp Ai AiAj
Ajtemp public void QuickSort(int
A, int h, int t) if (h t) return
int ih1, jt if (ij)
if (Ah gt Ai) SwapTwo(A,h,i)
return while (i lt j)
while (Ah gt Ai i lt t) i
while (Ah lt Aj j gt h) j--
if (i lt j) SwapTwo(A,i,j--) if
(ij AhgtAi) SwapTwo(A,h,i)
QuickSort(A,h,i-1)
QuickSort(A,i,t)
Quick Sort in Java
29 fun split nil (nil,nil) split a
(a,nil) split (abc) let
val (x,y) split (ac) in
if (a lt b) then (x, by)
else (bx, y) end fun quicksort
nil nil quicksort a a quicksort a
let val (x,y) split a
in quicksort(x)_at_quicksort(y)
end
split a,b if (a lt b)
then (a,b) else (b,a)
Quick Sort in ML
30Homework 5
Homework 5 30 points