Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
Pattern matching
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
===Pattern matching in Mathematica=== In [[Mathematica]], the only structure that exists is the [[Tree (data structure)|tree]], which is populated by symbols. In the [[Haskell]] syntax used thus far, this could be defined as <syntaxhighlight lang="haskell"> 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 <code>[]</code>, so that for instance <code>a[b,c]</code> is a tree with a as the parent, and b and c as the children. A pattern in Mathematica involves putting "_" at positions in that tree. For instance, the pattern A[_] will match elements such as A[1], A[2], or more generally A[''x''] where ''x'' is any entity. In this case, <code>A</code> is the concrete element, while <code>_</code> denotes the piece of tree that can be varied. A symbol prepended to <code>_</code> binds the match to that variable name while a symbol appended to <code>_</code> restricts the matches to nodes of that symbol. Note that even blanks themselves are internally represented as <code>Blank[]</code> for <code>_</code> and <code>Blank[x]</code> for <code>_x</code>. The Mathematica function <code>Cases</code> filters elements of the first argument that match the pattern in the second argument:<ref>{{Cite web|title=Cases—Wolfram Language Documentation|url=https://reference.wolfram.com/language/ref/Cases.html.en|access-date=2020-11-17|website=reference.wolfram.com}}</ref> <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. 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> ====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. For instance, the [[Mathematica]] function <code>Compile</code> 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 <code>{{com[_], Integer}}</code> instructs <code>Compile</code> that expressions of the form <code>com[_]</code> can be assumed to be [[integer]]s for the purposes of compilation: <syntaxhighlight lang="mathematica"> com[i_] := Binomial[2i, i] Compile[{x, {i, _Integer}}, x^com[i], {{com[_], Integer}}] </syntaxhighlight> Mailboxes in [[Erlang (programming language)|Erlang]] also work this way. The [[Curry–Howard correspondence]] between proofs and programs relates [[ML (programming language)|ML]]-style pattern matching to [[Proof by cases|case analysis]] and [[proof by exhaustion]].
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)