Pattern matching: Difference between revisions

Content deleted Content added
Undid revision 969916214 by 72.188.118.58 (talk)
mNo edit summary
Line 23:
 
<syntaxhighlight lang="haskell">
f 0 = 1
</syntaxhighlight>
 
Line 29:
 
<syntaxhighlight lang="haskell">
f n = n * f (n-1)
</syntaxhighlight>
 
Line 44:
 
<syntaxhighlight lang="haskell">
data Color = ColorConstructor Integer String
</syntaxhighlight>
 
Line 54:
 
<syntaxhighlight lang="haskell">
integerPart (ColorConstructor theInteger _) = theInteger
</syntaxhighlight>
 
As well:
<syntaxhighlight lang="haskell">
stringPart (ColorConstructor _ theString) = theString
</syntaxhighlight>
 
Line 68:
 
<syntaxhighlight lang="haskell">
[A x|A x <- [A 1, B 1, A 2, B 2]]
</syntaxhighlight>
 
Line 77:
In [[Mathematica]], the only structure that exists is the [[Tree (data structure)|tree]], which is populated by symbols. In the [[Haskell (programming language)|Haskell]] syntax used thus far, this could be defined as
<syntaxhighlight lang="mathematica">
data SymbolTree = Symbol String [SymbolTree]
 
data SymbolTree = Symbol String [SymbolTree]
</syntaxhighlight>
An example tree could then look like
<syntaxhighlight lang="mathematica">
Symbol "a" [Symbol "b" [], Symbol "c" [] ]
 
Symbol "a" [Symbol "b" [], Symbol "c" [] ]
</syntaxhighlight>
In the traditional, more suitable syntax, the symbols are written as they are and the levels of the tree are represented using [], so that for instance <code>a[b,c]</code> is a tree with a as the parent, and b and c as the children.
Line 95 ⟶ 93:
The Mathematica function <code>Cases</code> filters elements of the first argument that match the pattern in the second argument:
<syntaxhighlight lang="mathematica">
Cases[{a[1], b[1], a[2], b[2]}, a[_] ]
</syntaxhighlight>
evaluates to
<syntaxhighlight lang="mathematica">
{a[1], a[2]}
</syntaxhighlight>
Pattern matching applies to the ''structure'' of expressions. In the example below,
<syntaxhighlight lang="mathematica">
Cases[ {a[b], a[b, c], a[b[c], d], a[b[c], d[e]], a[b[c], d, e]}, a[b[_], _] ]
</syntaxhighlight>
returns
<syntaxhighlight lang="mathematica">
{a[b[c],d], a[b[c],d[e]]}
</syntaxhighlight>
because only these elements will match the pattern <code>a[b[_],_]</code> above.
Line 113 ⟶ 111:
In Mathematica, it is also possible to extract structures as they are created in the course of computation, regardless of how or where they appear. The function <code>Trace</code> can be used to monitor a computation, and return the elements that arise which match a pattern. For example, we can define the [[Fibonacci number|Fibonacci sequence]] as
<syntaxhighlight lang="mathematica">
fib[0|1]:=1
fib[n_]:= fib[n-1] + fib[n-2]
</syntaxhighlight>
Then, we can ask the question: Given fib[3], what is the sequence of recursive Fibonacci calls?
<syntaxhighlight lang="mathematica">
Trace[fib[3], fib[_]]
</syntaxhighlight>
returns a structure that represents the occurrences of the pattern <code>fib[_]</code> in the computational structure:
<syntaxhighlight lang="mathematica">
{fib[3],{fib[2],{fib[1]},{fib[0]}},{fib[1]}}
</syntaxhighlight>
 
Line 131 ⟶ 129:
 
<syntaxhighlight lang="mathematica">
com[i_] := Binomial[2i, i]
Compile[{x, {i, _Integer}}, x^com[i], {{com[_], Integer}}]
</syntaxhighlight>
 
Line 150 ⟶ 148:
 
<syntaxhighlight lang="haskell">
[] -- an empty list
x:xs -- an element x constructed on a list xs
</syntaxhighlight>
 
Line 157 ⟶ 155:
 
<syntaxhighlight lang="haskell">
head (element:list) = element
</syntaxhighlight>
 
Line 165 ⟶ 163:
 
<syntaxhighlight lang="haskell">
head (element:_) = element
</syntaxhighlight>
 
Line 182 ⟶ 180:
 
<syntaxhighlight lang="haskell">
['a', _]
</syntaxhighlight>
 
Line 194 ⟶ 192:
 
<syntaxhighlight lang="haskell">
[letter, digit] | isAlpha letter && isDigit digit
</syntaxhighlight>