Download Understanding Function Parameters & Pattern Matching in Modern Programming Languages and more Slides Advanced Computer Programming in PDF only on Docsity!
A Second Look At ML
Chapter Seven Modern Programming Languages, 2nd ed. (^) Docsity.com 1
Outline
Patterns
Local variable definitions
A sorting example
Chapter Seven Modern Programming Languages, 2nd ed. (^) Docsity.com 2
Underscore As A Pattern
The underscore can be used as a pattern
It matches anything, but does not bind it to
a variable
Preferred to:
fun f x = "yes";
Chapter Seven Modern Programming Languages, 2nd ed. 4
- fun f _ = "yes"; val f = fn : 'a -> string
- f 34.5; val it = "yes" : string
- f []; val it = "yes" : string
Constants As Patterns
Any constant of an equality type can be
used as a pattern
But not:
fun f 0.0 = "yes";
Chapter Seven Modern Programming Languages, 2nd ed. 5
- fun f 0 = "yes"; Warning: match nonexhaustive 0 => ... val f = fn : int -> string
- f 0; val it = "yes" : string
Lists Of Patterns As Patterns
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
Chapter Seven Modern Programming Languages, 2nd ed. 7
- fun f [a,_] = a; Warning: match nonexhaustive a :: _ :: nil => ... val f = fn : 'a list -> 'a
- f [#"f",#"g"]; val it = #"f" : char
Cons Of Patterns As A Pattern
You can use a cons of patterns as a pattern
x::xs matches any non-empty list, and
binds x to the head and xs to the tail
Parens around x::xs are for precedence
Chapter Seven Modern Programming Languages, 2nd ed. 8
- fun f (x::xs) = x; Warning: match nonexhaustive x :: xs => ... val f = fn : 'a list -> 'a
- f [1,2,3]; val it = 1 : int
Multiple Patterns for Functions
You can define a function by listing
alternate patterns
Chapter Seven Modern Programming Languages, 2nd ed. 10
- fun f 0 = "zero" = | f 1 = "one"; Warning: match nonexhaustive 0 => ... 1 => ... val f = fn : int -> string;
- f 1; val it = "one" : string
Syntax
To list alternate patterns for a function
You must repeat the function name in each
alternative
Chapter Seven Modern Programming Languages, 2nd ed. 11
< fun-def > ::= fun < fun-bodies > ; < fun-bodies > ::= < fun-body > | < fun-body > '|' < fun-bodies > < fun-body > ::= < fun-name > < pattern > = < expression >
Pattern-Matching Style
These definitions are equivalent:
fun f 0 = "zero"
| f _ = "non-zero";
fun f n =
if n = 0 then "zero"
else "non-zero";
But the pattern-matching style usually
preferred in ML
It often gives shorter and more legible
functions
Chapter Seven Modern Programming Languages, 2nd ed. (^) Docsity.com 13
Pattern-Matching Example
Chapter Seven Modern Programming Languages, 2nd ed. 14
fun fact n = if n = 0 then 1 else n * fact(n-1);
Original (from Chapter 5):
Rewritten using patterns:
fun fact 0 = 1 | fact n = n * fact(n-1);
More Examples
Chapter Seven Modern Programming Languages, 2nd ed. 16
This structure occurs frequently in recursive functions that operate on lists: one alternative for the base case ( nil ) and one alternative for the recursive case ( first::rest ).
Adding up all the elements of a list: fun f nil = 0 | f (first::rest) = first + f rest;
Counting the true values in a list: fun f nil = 0 | f (true::rest) = 1 + f rest | f (false::rest) = f rest;
More Examples
Chapter Seven Modern Programming Languages, 2nd ed. 17
Making a new list of integers in which each is one greater than in the original list:
fun f nil = nil | f (first::rest) = first+1 :: f rest;
The polyEqual Warning
Warning for an equality comparison, when
the runtime type cannot be resolved
OK to ignore: this kind of equality test is
inefficient, but can’t always be avoided
Chapter Seven Modern Programming Languages, 2nd ed. 19
- fun eq (a,b) = if a=b then 1 else 0; Warning: calling polyEqual val eq = fn : ''a * ''a -> int
- eq (1,3); val it = 0 : int
- eq ("abc","abc"); val it = 1 : int
Patterns Everywhere
Patterns are not just for function definition
Here we see that you can use them in a val
More ways to use patterns, later
Chapter Seven Modern Programming Languages, 2nd ed. 20
- val (a,b) = (1,2.3); val a = 1 : int val b = 2.3 : real
- val a::b = [1,2,3,4,5]; Warning: binding not exhaustive a :: b = ... val a = 1 : int val b = [2,3,4,5] : int list