<s>
In	O
computer	B-General_Concept
science	I-General_Concept
,	O
pattern	B-Language
matching	I-Language
is	O
the	O
act	O
of	O
checking	O
a	O
given	O
sequence	O
of	O
tokens	O
for	O
the	O
presence	O
of	O
the	O
constituents	O
of	O
some	O
pattern	O
.	O
</s>
<s>
The	O
patterns	O
generally	O
have	O
the	O
form	O
of	O
either	O
sequences	O
or	O
tree	B-Data_Structure
structures	I-Data_Structure
.	O
</s>
<s>
Uses	O
of	O
pattern	B-Language
matching	I-Language
include	O
outputting	O
the	O
locations	O
(	O
if	O
any	O
)	O
of	O
a	O
pattern	O
within	O
a	O
token	O
sequence	O
,	O
to	O
output	O
some	O
component	O
of	O
the	O
matched	O
pattern	O
,	O
and	O
to	O
substitute	O
the	O
matching	O
pattern	O
with	O
some	O
other	O
token	O
sequence	O
(	O
i.e.	O
,	O
search	B-Language
and	I-Language
replace	I-Language
)	O
.	O
</s>
<s>
Sequence	O
patterns	O
(	O
e.g.	O
,	O
a	O
text	O
string	O
)	O
are	O
often	O
described	O
using	O
regular	B-Language
expressions	I-Language
and	O
matched	O
using	O
techniques	O
such	O
as	O
backtracking	B-Algorithm
.	O
</s>
<s>
Tree	B-Application
patterns	O
are	O
used	O
in	O
some	O
programming	O
languages	O
as	O
a	O
general	O
tool	O
to	O
process	O
data	O
based	O
on	O
its	O
structure	O
,	O
e.g.	O
</s>
<s>
C#	B-Application
,	O
F#	B-Operating_System
,	O
Haskell	B-Language
,	O
ML	B-Language
,	O
Python	B-Language
,	O
Ruby	B-Language
,	O
Rust	B-Application
,	O
Scala	B-Application
,	O
Swift	B-Application
and	O
the	O
symbolic	O
mathematics	O
language	O
Mathematica	B-Language
have	O
special	O
syntax	O
for	O
expressing	O
tree	B-Application
patterns	O
and	O
a	O
language	B-Language
construct	I-Language
for	O
conditional	B-Language
execution	I-Language
and	O
value	O
retrieval	O
based	O
on	O
it	O
.	O
</s>
<s>
Often	O
it	O
is	O
possible	O
to	O
give	O
alternative	O
patterns	O
that	O
are	O
tried	O
one	O
by	O
one	O
,	O
which	O
yields	O
a	O
powerful	O
conditional	B-Language
programming	I-Language
construct	I-Language
.	O
</s>
<s>
Pattern	B-Language
matching	I-Language
sometimes	O
includes	O
support	O
for	O
guards	B-Language
.	O
</s>
<s>
Early	O
programming	O
languages	O
with	O
pattern	B-Language
matching	I-Language
constructs	O
include	O
COMIT	B-Language
(	O
1957	O
)	O
,	O
SNOBOL	B-Application
(	O
1962	O
)	O
,	O
Refal	B-Language
(	O
1968	O
)	O
with	O
tree-based	O
pattern	B-Language
matching	I-Language
,	O
Prolog	B-Language
(	O
1972	O
)	O
,	O
SASL	B-Language
(	O
1976	O
)	O
,	O
NPL	B-Language
(	O
1977	O
)	O
,	O
and	O
KRC	B-Language
(	O
1981	O
)	O
.	O
</s>
<s>
Many	O
text	B-Application
editors	I-Application
support	O
pattern	B-Language
matching	I-Language
of	O
various	O
kinds	O
:	O
the	O
QED	B-Application
editor	I-Application
supports	O
regular	B-Language
expression	I-Language
search	O
,	O
and	O
some	O
versions	O
of	O
TECO	B-Application
support	O
the	O
OR	O
operator	O
in	O
searches	O
.	O
</s>
<s>
Computer	B-General_Concept
algebra	I-General_Concept
systems	I-General_Concept
generally	O
support	O
pattern	B-Language
matching	I-Language
on	O
algebraic	O
expressions	O
.	O
</s>
<s>
The	O
simplest	O
pattern	O
in	O
pattern	B-Language
matching	I-Language
is	O
an	O
explicit	O
value	O
or	O
a	O
variable	O
.	O
</s>
<s>
For	O
an	O
example	O
,	O
consider	O
a	O
simple	O
function	O
definition	O
in	O
Haskell	B-Language
syntax	O
(	O
function	O
parameters	O
are	O
not	O
in	O
parentheses	O
but	O
are	O
separated	O
by	O
spaces	O
,	O
=	O
is	O
not	O
assignment	O
but	O
definition	O
)	O
:	O
</s>
<s>
Now	O
,	O
whenever	O
f	O
is	O
given	O
0	O
as	O
argument	O
the	O
pattern	B-Language
matches	I-Language
and	O
the	O
function	O
returns	O
1	O
.	O
</s>
<s>
In	O
Haskell	B-Language
(	O
unlike	O
at	O
least	O
Hope	B-Language
)	O
,	O
patterns	O
are	O
tried	O
in	O
order	O
so	O
the	O
first	O
definition	O
still	O
applies	O
in	O
the	O
very	O
specific	O
case	O
of	O
the	O
input	O
being	O
0	O
,	O
while	O
for	O
any	O
other	O
argument	O
the	O
function	O
returns	O
n	O
*	O
f	O
(	O
n-1	O
)	O
with	O
n	O
being	O
the	O
argument	O
.	O
</s>
<s>
Algorithms	O
for	O
matching	B-Algorithm
wildcards	I-Algorithm
in	O
simple	O
string-matching	O
situations	O
have	O
been	O
developed	O
in	O
a	O
number	O
of	O
recursive	O
and	O
non-recursive	O
varieties	O
.	O
</s>
<s>
A	O
tree	B-Application
pattern	O
describes	O
a	O
part	O
of	O
a	O
tree	B-Application
by	O
starting	O
with	O
a	O
node	O
and	O
specifying	O
some	O
branches	O
and	O
nodes	O
and	O
leaving	O
some	O
unspecified	O
with	O
a	O
variable	O
or	O
wildcard	O
pattern	O
.	O
</s>
<s>
It	O
may	O
help	O
to	O
think	O
of	O
the	O
abstract	B-Data_Structure
syntax	I-Data_Structure
tree	I-Data_Structure
of	O
a	O
programming	O
language	O
and	O
algebraic	O
data	O
types	O
.	O
</s>
<s>
In	O
Haskell	B-Language
,	O
the	O
following	O
line	O
defines	O
an	O
algebraic	O
data	O
type	O
Color	O
that	O
has	O
a	O
single	O
data	O
constructor	O
ColorConstructor	O
that	O
wraps	O
an	O
integer	O
and	O
a	O
string	O
.	O
</s>
<s>
The	O
constructor	O
is	O
a	O
node	O
in	O
a	O
tree	B-Application
and	O
the	O
integer	O
and	O
string	O
are	O
leaves	O
in	O
branches	O
.	O
</s>
<s>
When	O
we	O
want	O
to	O
write	O
functions	O
to	O
make	O
Color	O
an	O
abstract	O
data	O
type	O
,	O
we	O
wish	O
to	O
write	O
functions	O
to	O
interface	B-Application
with	O
the	O
data	O
type	O
,	O
and	O
thus	O
we	O
want	O
to	O
extract	O
some	O
data	O
from	O
the	O
data	O
type	O
,	O
for	O
example	O
,	O
just	O
the	O
string	O
or	O
just	O
the	O
integer	O
part	O
of	O
Color	O
.	O
</s>
<s>
For	O
example	O
,	O
for	O
a	O
function	O
to	O
get	O
the	O
integer	O
part	O
of	O
Color	O
,	O
we	O
can	O
use	O
a	O
simple	O
tree	B-Application
pattern	O
and	O
write	O
:	O
</s>
<s>
The	O
creations	O
of	O
these	O
functions	O
can	O
be	O
automated	O
by	O
Haskell	B-Language
's	O
data	O
record	O
syntax	O
.	O
</s>
<s>
This	O
Ocaml	B-Language
example	O
which	O
defines	O
a	O
red-black	O
tree	B-Application
and	O
a	O
function	O
to	O
re-balance	O
it	O
after	O
element	O
insertion	O
shows	O
how	O
to	O
match	O
on	O
a	O
more	O
complex	O
structure	O
generated	O
by	O
a	O
recursive	O
data	O
type	O
.	O
</s>
<s>
Pattern	B-Language
matching	I-Language
can	O
be	O
used	O
to	O
filter	O
data	O
of	O
a	O
certain	O
structure	O
.	O
</s>
<s>
For	O
instance	O
,	O
in	O
Haskell	B-Language
a	O
list	B-Language
comprehension	I-Language
could	O
be	O
used	O
for	O
this	O
kind	O
of	O
filtering	O
:	O
</s>
<s>
In	O
Mathematica	B-Language
,	O
the	O
only	O
structure	O
that	O
exists	O
is	O
the	O
tree	B-Application
,	O
which	O
is	O
populated	O
by	O
symbols	O
.	O
</s>
<s>
In	O
the	O
traditional	O
,	O
more	O
suitable	O
syntax	O
,	O
the	O
symbols	O
are	O
written	O
as	O
they	O
are	O
and	O
the	O
levels	O
of	O
the	O
tree	B-Application
are	O
represented	O
using	O
[],	O
so	O
that	O
for	O
instance	O
a[b,c]	O
is	O
a	O
tree	B-Application
with	O
a	O
as	O
the	O
parent	O
,	O
and	O
b	O
and	O
c	O
as	O
the	O
children	O
.	O
</s>
<s>
A	O
pattern	O
in	O
Mathematica	B-Language
involves	O
putting	O
"	O
_	O
"	O
at	O
positions	O
in	O
that	O
tree	B-Application
.	O
</s>
<s>
In	O
this	O
case	O
,	O
A	O
is	O
the	O
concrete	O
element	O
,	O
while	O
_	O
denotes	O
the	O
piece	O
of	O
tree	B-Application
that	O
can	O
be	O
varied	O
.	O
</s>
<s>
The	O
Mathematica	B-Language
function	O
Cases	O
filters	O
elements	O
of	O
the	O
first	O
argument	O
that	O
match	O
the	O
pattern	O
in	O
the	O
second	O
argument	O
:	O
</s>
<s>
Pattern	B-Language
matching	I-Language
applies	O
to	O
the	O
structure	O
of	O
expressions	O
.	O
</s>
<s>
In	O
Mathematica	B-Language
,	O
it	O
is	O
also	O
possible	O
to	O
extract	O
structures	O
as	O
they	O
are	O
created	O
in	O
the	O
course	O
of	O
computation	O
,	O
regardless	O
of	O
how	O
or	O
where	O
they	O
appear	O
.	O
</s>
<s>
For	O
instance	O
,	O
the	O
Mathematica	B-Language
function	O
Compile	O
can	O
be	O
used	O
to	O
make	O
more	O
efficient	O
versions	O
of	O
the	O
code	O
.	O
</s>
<s>
Mailboxes	O
in	O
Erlang	B-Operating_System
also	O
work	O
this	O
way	O
.	O
</s>
<s>
The	O
Curry	O
–	O
Howard	O
correspondence	O
between	O
proofs	O
and	O
programs	O
relates	O
ML-style	O
pattern	B-Language
matching	I-Language
to	O
case	O
analysis	O
and	O
proof	O
by	O
exhaustion	O
.	O
</s>
<s>
By	O
far	O
the	O
most	O
common	O
form	O
of	O
pattern	B-Language
matching	I-Language
involves	O
strings	O
of	O
characters	O
.	O
</s>
<s>
In	O
many	O
programming	O
languages	O
,	O
a	O
particular	O
syntax	O
of	O
strings	O
is	O
used	O
to	O
represent	O
regular	B-Language
expressions	I-Language
,	O
which	O
are	O
patterns	O
describing	O
string	O
characters	O
.	O
</s>
<s>
However	O
,	O
it	O
is	O
possible	O
to	O
perform	O
some	O
string	O
pattern	B-Language
matching	I-Language
within	O
the	O
same	O
framework	O
that	O
has	O
been	O
discussed	O
throughout	O
this	O
article	O
.	O
</s>
<s>
In	O
Mathematica	B-Language
,	O
strings	O
are	O
represented	O
as	O
trees	O
of	O
root	O
StringExpression	O
and	O
all	O
the	O
characters	O
in	O
order	O
as	O
children	O
of	O
the	O
root	O
.	O
</s>
<s>
In	O
Haskell	B-Language
and	O
functional	B-Language
programming	I-Language
languages	I-Language
in	O
general	O
,	O
strings	O
are	O
represented	O
as	O
functional	O
lists	O
of	O
characters	O
.	O
</s>
<s>
In	O
Haskell	B-Language
syntax	O
:	O
</s>
<s>
When	O
pattern	B-Language
matching	I-Language
,	O
we	O
assert	O
that	O
a	O
certain	O
piece	O
of	O
data	O
is	O
equal	O
to	O
a	O
certain	O
pattern	O
.	O
</s>
<s>
In	O
Mathematica	B-Language
,	O
for	O
instance	O
,	O
</s>
<s>
The	O
same	O
pattern	O
in	O
Haskell	B-Language
:	O
</s>
<s>
In	O
Haskell	B-Language
,	O
guards	B-Language
could	O
be	O
used	O
to	O
achieve	O
the	O
same	O
matches	O
:	O
</s>
<s>
SNOBOL	B-Application
(	O
StriNg	O
Oriented	O
and	O
symBOlic	O
Language	O
)	O
is	O
a	O
computer	O
programming	O
language	O
developed	O
between	O
1962	O
and	O
1967	O
at	O
AT&T	O
Bell	O
Laboratories	O
by	O
David	O
J	O
.	O
Farber	O
,	O
Ralph	O
E	O
.	O
Griswold	O
and	O
Ivan	O
P	O
.	O
Polonsky	O
.	O
</s>
<s>
SNOBOL4	B-Application
stands	O
apart	O
from	O
most	O
programming	O
languages	O
by	O
having	O
patterns	O
as	O
a	O
first-class	O
data	O
type	O
(	O
i.e.	O
</s>
<s>
SNOBOL	B-Application
was	O
quite	O
widely	O
taught	O
in	O
larger	O
US	O
universities	O
in	O
the	O
late	O
1960s	O
and	O
early	O
1970s	O
and	O
was	O
widely	O
used	O
in	O
the	O
1970s	O
and	O
1980s	O
as	O
a	O
text	O
manipulation	O
language	O
in	O
the	O
humanities	O
.	O
</s>
<s>
Since	O
SNOBOL	B-Application
's	O
creation	O
,	O
newer	O
languages	O
such	O
as	O
Awk	B-Application
and	O
Perl	B-Language
have	O
made	O
string	O
manipulation	O
by	O
means	O
of	O
regular	B-Language
expressions	I-Language
fashionable	O
.	O
</s>
<s>
SNOBOL4	B-Application
patterns	O
,	O
however	O
,	O
subsume	O
BNF	O
grammars	O
,	O
which	O
are	O
equivalent	O
to	O
context-free	O
grammars	O
and	O
more	O
powerful	O
than	O
regular	B-Language
expressions	I-Language
.	O
</s>
