Pattern matching: Difference between revisions

Content deleted Content added
m Reverted edit by RedactedUser15 (talk) to last version by Leastfixedpoint
Line 46:
 
; [[Name binding|Binding]]
: A way of associating a ''name'' with a portion of the discriminant, so that the name is [[Name binding|bound to]] that portion when the continuation executes. For example, in Rust, <{{code>|2=rust|1=match v { (a, b) => ... }</code> }} expects <code>v</code> to be a pair, and <code>a</code> and <code>b</code> are bindings bringing variables of the same name into scope in the continuation ("<code>...</code>").
 
; Wildcard
Line 55:
 
; Predicate
: Some pattern languages allow user-defined ''predicate'' functions to be embedded in a pattern. The predicate is applied to the portion of the discriminant corresponding to the position of the predicate in the pattern; if the predicate responds with boolean false, the pattern is considered to have failed. For example, in Racket, the pattern <{{code>|(list (? even?) ...)</code>|rkt}} first expects a list, and then applies the predicate <code>even?</code> to each element; the overall pattern thus succeeds only when the discriminant is a list of even numbers.
 
; View pattern
Line 61:
 
; Constraint
: Some pattern languages allow direct comparison of portions of the discriminant with previously-computed (or constant) data structures. For example, the pattern <{{code>|1=(== expr)</code>|2=rkt}} in Racket compares the value against the result of evaluating <code>expr</code>. In Erlang, mention of any variable already in scope in a pattern causes it to act as a constraint in this way (instead of as a binding).
 
; Literal pattern; atomic pattern