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>
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>
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]]
|