Content deleted Content added
Undid revision 969916214 by 72.188.118.58 (talk) |
mNo edit summary |
||
Line 23:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 29:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 44:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 54:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
As well:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 68:
<syntaxhighlight lang="haskell">
</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]
</syntaxhighlight>
An example tree could then look like
<syntaxhighlight lang="mathematica">
▲ 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">
</syntaxhighlight>
evaluates to
<syntaxhighlight lang="mathematica">
</syntaxhighlight>
Pattern matching applies to the ''structure'' of expressions. In the example below,
<syntaxhighlight lang="mathematica">
</syntaxhighlight>
returns
<syntaxhighlight lang="mathematica">
</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">
</syntaxhighlight>
Then, we can ask the question: Given fib[3], what is the sequence of recursive Fibonacci calls?
<syntaxhighlight lang="mathematica">
</syntaxhighlight>
returns a structure that represents the occurrences of the pattern <code>fib[_]</code> in the computational structure:
<syntaxhighlight lang="mathematica">
</syntaxhighlight>
Line 131 ⟶ 129:
<syntaxhighlight lang="mathematica">
</syntaxhighlight>
Line 150 ⟶ 148:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 157 ⟶ 155:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 165 ⟶ 163:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 182 ⟶ 180:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
Line 194 ⟶ 192:
<syntaxhighlight lang="haskell">
</syntaxhighlight>
|