Pattern matching: Difference between revisions

Content deleted Content added
External links: Added navbar
No edit summary
Line 6:
Sequence patterns (e.g., a text string) are often described using [[regular expression]]s and matched using techniques such as [[backtracking]].
 
Tree patterns are used in some [[programming language]]s as a general tool to process data based on its structure, e.g. [[C Sharp (programming language)|C#]],<ref>{{cite web|url=https://docs.microsoft.com/en-us/dotnet/csharp/pattern-matching|title=Pattern Matching - C# Guide}}</ref>, [[F Sharp (programming language)|F#]],<ref>{{cite web|url=https://docs.microsoft.com/en-us/dotnet/fsharp/language-reference/pattern-matching|title=Pattern Matching - F# Guide}}</ref>, [[Haskell (programming language)|Haskell]], [[ML programming language|ML]], [[Rust (programming language)|Rust]] ,<ref>{{cite web |url=https://doc.rust-lang.org/book/ch18-03-pattern-syntax.html |title=Pattern Syntax - The Rust Programming language}}</ref>, [[Scala programming language|Scala]], [[Swift (programming language)|Swift]] <ref>{{cite web|url=https://docs.swift.org/swift-book/ReferenceManual/Patterns.html|title=Patterns — The Swift Programming Language (Swift 5.1)}}</ref> and the symbolic mathematics language [[Mathematica]] have special syntax for expressing tree patterns and a [[language construct]] for [[Conditional (programming)|conditional execution]] and value retrieval based on it. For simplicity and efficiency reasons, these tree patterns lack some features that are available in regular expressions.{{citation needed|date=June 2019}}
 
Often it is possible to give alternative patterns that are tried one by one, which yields a powerful [[Conditional (programming)|conditional programming construct]]. Pattern matching sometimes includes support for [[guard (computing)|guards]].{{citation needed|date=January 2019}}
Line 124:
{fib[3],{fib[2],{fib[1]},{fib[0]}},{fib[1]}}
</source>
 
===Declarative programming===
In symbolic programming languages, it is easy to have patterns as arguments to functions or as elements of data structures. A consequence of this is the ability to use patterns to declaratively make statements about pieces of data and to flexibly instruct functions how to operate.
Line 147 ⟶ 148:
 
In Haskell and functional programming languages in general, strings are represented as functional [[List (computing)|lists]] of characters. A functional list is defined as an empty list, or an element constructed on an existing list. In Haskell syntax:
 
<source lang="haskell">
[] -- an empty list
Line 153 ⟶ 155:
 
The structure for a list with some elements is thus <code>element:list</code>. When pattern matching, we assert that a certain piece of data is equal to a certain pattern. For example, in the function:
 
<source lang="haskell">
head (element:list) = element
</source>
 
weWe assert that the first element of <code>head</code>'s argument is called element, and the function returns this. We know that this is the first element because of the way lists are defined, a single element constructed onto a list. This single element must be the first. The empty list would not match the pattern at all, as an empty list does not have a head (the first element that is constructed).
 
In the example, we have no use for <code>list</code>, so we can disregard it, and thus write the function:
 
<source lang="haskell">
head (element:_) = element
Line 176 ⟶ 180:
 
The same pattern in Haskell:
 
<source lang="haskell">
['a', _]
Line 187 ⟶ 192:
 
In Haskell, [[guard (computing)|guards]] could be used to achieve the same matches:
 
<source lang="haskell">
[letter, digit] | isAlpha letter && isDigit digit
Line 205 ⟶ 211:
 
==See also==
 
* [[AIML]] for an AI language based on matching patterns in speech
* [[AWK|AWK language]]