| Pattern Matching |
Article Index for Pattern |
Website Links For Pattern |
Information AboutPattern Matching |
| CATEGORIES ABOUT PATTERN MATCHING | |
| conditional constructs | |
| pattern matching | |
| articles with example haskell codeconditional constructs | |
| pattern matching | |
| articles with example haskell code | |
| formal languages | |
|
Sequence (or specifically text string) patterns are often described using Regular Expression s (i.e. Backtracking ) and matched using respective algorithms. Sequences can also be seen as trees branching for each element into the respective element and the rest of the sequence, or as trees that immediately branch into all elements. Tree patterns can be used in Programming Languages as a general tool to process data based on its structure. Some Functional Programming Language s such as Haskell , ML and the symbolic mathematics language Mathematica have a special syntax for expressing tree patterns and a language construct for 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. Depending on the languages, pattern matching can be used for function arguments, in case expressions, whenever new variables are bound, or in very limited situations such as only for sequences in assignment (in Python ). Often it is possible to give alternative patterns that are tried one by one, which yields a powerful Conditional Programming Construct . Pattern matching can benefit from Guard s. Term Rewriting languages rely on pattern matching for the fundamental way a program evaluates into a result. Pattern matching benefits most when the underlying datastructures are as simple and flexible as possible. This is especially the case in languages with a strong symbolic flavor. In Symbolic Programming Language s, patterns are the same kind of datatype as everything else, and can therefore be fed in as arguments to functions. PRIMITIVE PATTERNS The simplest pattern in pattern matching is an explicit value or a variable. For an example, consider a simple function definition in Haskell syntax (function parameters are not in parentheses but are separated by spaces, = is not assignment but definition): f 0 = 1 Here, 0 is a single value pattern. Now, whenever f is given 0 as argument the pattern matches and the function returns 1. With any other argument, the matching and thus the function fail. As the syntax supports alternative patterns in function definitions, we can continue the definition extending it to take more generic arguments:
The wildcard pattern (often written as _) is also simple: like a variable name, it matches any value, but does not bind the value to any name.TREE PATTERNS More complex patterns can be built from the primitive ones of the previous section, usually in the same way as values are built by combining other values. The difference then is that with variable and wildcard parts, a pattern doesn't build into single value, but matches a group of values that are the combination of the concrete elements and the elements that are allowed to vary within the structure of the pattern. A tree pattern describes a part of a tree by starting with a node and specifying some branches and nodes and leaving some unspecified with a variable or wildcard pattern. It may help to think of the Abstract Syntax Tree of a programming language and Algebraic Data Type s. In Haskell, the following line defines an algebraic data type Color that has a single data constructor ColorConstructor that wraps an integer and a string.data Color = ColorConstructor Integer String The constructor is a node in a tree and the integer and string are leaves in branches. When we want to write Function s to make Color an Abstract Data Type , we wish to write functions to Interface with the data type, and thus we want to extract some data from the data type, for example, just the string or just the integer part of Color.If we pass a variable that is of type Color, how can we get the data out of this variable? For example, for a function to get the integer part of Color, we can use a simple tree pattern and write:integerPart (ColorConstructor theInteger _) = theInteger As well: stringPart (ColorConstructor _ theString) = theString The creations of these functions can be automated by Haskell's data record syntax. FILTERING DATA WITH PATTERNS Pattern matching can be used to filter data of a certain structure. For instance, in Haskell a list comprehension could be used for this kind of filtering: | ||
|   | Fib | "01" class="copylinks" target="_blank">{Link without Title} :=1 |
Compile can be used to make more efficient versions of the code. In the following example the details do not particularly matter; what matters is that the subexpression instructs Compile that expressions of the form com {Link without Title} can be assumed to be Integer s for the purposes of compilation:element:list. When pattern matching, we assert that a certain piece of data is equal to a certain pattern. For example, in the function:head'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). list, so we can disregard it, and thus write the function:|
|