Pattern matching: Difference between revisions

Content deleted Content added
Introduce section on terminology relating to pattern matching.
Bender the Bot (talk | contribs)
 
(4 intermediate revisions by 4 users not shown)
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
: Often written as a single underscore, <code>_</code>, the wildcard pattern accepts all values without examining them further, ignoring their structure. Also known as '''discard''', the '''wild pattern''', the '''catch-all pattern''', or as a '''hole'''.
 
; [[Guard (computer science)|Guard]]
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
Line 314:
*[https://archive.today/19990225161739/http://www.haskell.org/development/views.html Views: An Extension to Haskell Pattern Matching]
* Nikolaas N. Oosterhof, Philip K. F. Hölzenspies, and Jan Kuper. [https://web.archive.org/web/20060304053330/http://wwwhome.cs.utwente.nl/~tina/apm/applPatts.pdf Application patterns]. A presentation at Trends in Functional Programming, 2005
*[httphttps://www.cs.cornell.edu/Projects/jmatch JMatch]: the [[Java (programming language)|Java]] language extended with pattern matching
*[https://archive.today/20130630081135/http://www.showtrend.com/ ShowTrend]: Online pattern matching for stock prices
*[https://web.archive.org/web/20060211020429/http://cm.bell-labs.com/cm/cs/who/dmr/qed.html An incomplete history of the QED Text Editor] by [[Dennis Ritchie]] - provides the history of regular expressions in computer programs