Introduction to Programming in ATS: | ||
---|---|---|
<<< Previous | Datatypes | Next >>> |
Pattern matching means matching values against patterns. In the case where a value matches a pattern, a collection of bindings are generated between the variables in the pattern and certain components in the value. Pattern-matching is performed according to the following set of rules:
A value that matches a constant pattern must be the same constant, and this matching generates no bindings.
The void value () only matches the pattern (), and this matching generates no bindings.
Any value can match the wildcard pattern, and this matching generates no bindings.
Any value can match a variable pattern, and this matching generates a binding between the variable and the value.
A tuple value matches a tuple pattern if they are of the same length and each value component in the former matches the corresponding pattern component in the latter, and this matching generates a collection of bindings that is the union of the bindings generated from matching the value components in the tuple value against the pattern components in the tuple pattern.
A record value matches a record pattern if they have the same field names and each value component in the former matches the corresponding pattern component in the latter, and this matching generates a collection of bindings that is the union of the bindings generated from matching the value components in the record value against the pattern components in the record pattern.
Given a pattern formed by applying a constructor C to some pattern arguments, a value matches this pattern if the value is formed by applying C to some value arguments matching the pattern arguments, and this matching generates a collection of bindings that is the union of the bindings generated from matching the value arguments against the pattern arguments.
Given a referenced pattern (x as pat), a value matches the pattern if it matches pat, and this matching generates a collection of bindings that extends the bindings generated from matching the value against pat with a binding from x to the value.
<<< Previous | Home | Next >>> |
Datatypes | Up | Matching Clauses and Case-Expressions |