Notice that patterns are recursive (case (iv) from the previous page). In determining whether two patterns "match", we can therefore use a recursive algorithm, in pseudocode:
fun matches(left, right) = if left is a constant then if left = right then true else false else if left is a variable then true else if left is a wildcard then true else if left is a compound constructor then if right has the same top-level constructor then if each component of left matches each component of right then true else false else false else (* left may not appear in a pattern! *) raise some kind of error
Trace the execution of the pattern-matching algorithm over the following value bindings:
val 3 = 3; val 3 = 4; val _ = "walrus"; val (3+5, x) = (8, 9); val x :: _ :: xs = [2,3,4]; val ({baz="hi", foo=seal}, fish::nil) = ({foo="bar", baz="hi"}, [827]); val [2,3,x] @ nil = 2::3::4::nil;