Artifact Content
Not logged in

Artifact c4d2e35bf279b1e2b070c4135cc29aa84ed01c41:


<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<!-- DO NOT EDIT THIS FILE-->
<!-- Edit the .tex version instead-->
<html><head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>Using Match</title>
<link href="match_files/base.css" rel="stylesheet" type="text/css">
<script>(function n(){!function(){function e(e,t,n){t=t||{};var i=e.ownerDocument||e,a=i.createEvent?i.createEvent("CustomEvent"):i.createEventObject();a.initCustomEvent&&a.initCustomEvent(t.type,!!t.bubbles,!!t.cancelable,t.detail);for(var r in t)a[r]=t[r];return setTimeout(function(){try{e.dispatchEvent?e.dispatchEvent(a):e.fireEvent("on"+t.type,i.createEventObject())}catch(r){var o=e["listen"+t.type];if(o)for(var s=0;s<o.length;++s)try{o[s].call(e,a)}catch(r){}}n()},0),this}function t(e,t,n){function i(e,t){try{var n=e.ownerDocument;if(n.createEventObject){var i=n.createEventObject();e.fireEvent("on"+t,i)}else i=n.createEvent("HTMLEvents"),i.initEvent(t,!0,!0),e.dispatchEvent(i)}catch(a){}}var a=!0,r=e.className&&-1!=e.className.indexOf("fancified");if(window.jQuery){var o=window.jQuery(e);try{if(o.selectBoxIt)o.selectBoxIt("selectOption",o.val());else if(o.data("chosen")||o.chosen)o.trigger("chosen:updated").trigger("liszt:updated");else if(o.data("chooserElement"))o.trigger("change");else if(o.fancySelect)o.get("fancySelect").select("value",o.val());else if(o.selectBox)o.selectBox("value",o.val());else if(o.selectric)o.selectric("refresh");else if(o.coreUISelect){var s=o.data("coreUISelect");s.isSelectShow=!0,s.changeDropdownData(),s.isSelectShow=!1}else if(o.data("myJSPulldownObject")){var l=o.data("myJSPulldownObject");l.setToValue(o.val())}else if(o.fancyfields)o.setVal(o.val());else if(o.data("select2"));else if(o.data("selectize"))a=!1,o.data("selectize").setValue(o.val());else if(o.hasClass("fancified"))o.trigger("update");else if(o.selectmenu){var c=o.val();try{o.selectmenu("value",o[0].options[0].value)}catch(d){}o.selectmenu("value",c)}o.trigger("change")}catch(d){}}a&&(r&&i(e,"update"),i(e,"change"),i(e,"blur")),n()}function n(t,n,i,a){var r=t.value;e(t,{type:"keydown",keyCode:n,which:n,charCode:n,bubbles:!0},function(){e(t,{type:"keypress",keyCode:n,which:n,charCode:n,bubbles:!0},function(){setTimeout(function(){var o=t.value;r==o&&(t.value=i),e(t,{type:"input",keyCode:n,which:n,charCode:n,bubbles:!0},function(){e(t,{type:"keyup",keyCode:n,which:n,charCode:n,bubbles:!0},function(){a()})})},1)})})}function i(e,t,a,r){if(!t||""==t)return void r();var o=t.charCodeAt(0);a+=t.charAt(0),n(e,o,a,function(){i(e,t.substring(1),a,r)})}function a(t,n,a){e(t,{type:"focus"},function(){e(t,{type:"click"},function(){i(t,n,"",function(){e(t,{type:"change"},function(){e(t,{type:"blur"},function(){e(document,{type:"abineFilled"},function(){a()})})})})})})}function r(n,i,a,r){var o=(i||"").toLowerCase(),s=function(){e(document,{type:"abineFilled"},function(){r()})},l=!1,c=!1,d=n.getElementsByTagName("option");if(d&&d.length>0){for(var u=-1,m=0;m<d.length;m++){var h=(d[m].text||"").toLowerCase();if(d[m].getAttribute("value")==i||h==o){d[m].selected||(l=!0),c=!0,d[m].selected=!0;break}-1==u&&-1!=h.indexOf(o)&&(u=m)}c||-1==u||a||d[u].selected||(l=!0,d[u].selected=!0)}n.setAttribute("abineFillResponse",c),l?t(n,i,s):s()}function o(){var e=document.getElementsByClassName("abineFillTarget");if(e.length>0)return e[0];for(var t=0;t<frames.length;t++)try{var e=frames[t].document.getElementsByClassName("abineFillTarget");if(e.length>0)return e[0]}catch(n){}return null}function s(){var n=document.createElement("div");n.id="abineFillElement","undefined"!=typeof paypal&&n.setAttribute("data-paypal","1"),"undefined"!=typeof MasterPass&&n.setAttribute("data-masterpass","1"),document.documentElement.appendChild(n),n.addEventListener("fill",function(){var t=o();if(t){var i=n.getAttribute("value");a(t,i,function(){})}else e(document,{type:"abineFilled"},function(){})},!1),n.addEventListener("fillSelect",function(){var t=o();if(t){var i=n.getAttribute("value"),a=!!n.getAttribute("skipPartial");r(t,i,a,function(){})}else e(document,{type:"abineFilled"},function(){})}),n.addEventListener("triggerChange",function(){var i=o();if(i){var a=n.getAttribute("value");i.nodeName.match(/select/i)?t(i,a,function(){}):e(i,{type:"change"},function(){e(i,{type:"blur"},function(){})})}})}s()}()})()</script></head>
<body data-link64watched="1">




<h1>Using Match</h1>
<h2>R. Kent Dybvig</h2>
<h3>January 17, 2010</h3>


<p>

</p><table cellpadding="0" cellspacing="0">

  
  
 
</table>


<p>
This document describes a pattern matcher, unimaginatively called
<tt>match</tt>, the source code for which is available at:

</p><p>
<a class="ref" href="http://www.cs.indiana.edu/chezscheme/match/match.ss">http://www.cs.indiana.edu/chezscheme/match/match.ss</a>

</p><p>
A <tt>match</tt> expression looks a lot like a <tt>case</tt> expression,
except that the keys are replaced with a pattern to be matched against
the input.
A <tt>match</tt> expression has the general form below.

</p><p>

</p><p><tt>(match&nbsp;<i>input-expr</i>&nbsp;<i>clause</i>)</tt>
</p><p>Each clause consists of an input pattern, an optional guard, and a
set of expressions.

</p><p>

</p><p><tt>[<i>input-pattern</i>&nbsp;<i>expr1</i>&nbsp;<i>expr2</i>&nbsp;...]</tt>
</p><p>or

</p><p>

</p><p><tt>[<i>input-pattern</i>&nbsp;(guard&nbsp;<i>guard-expr</i>&nbsp;...)&nbsp;<i>expr1</i>&nbsp;<i>expr2</i>&nbsp;...]</tt>
</p><p>As with <tt>case</tt>, the input expression is evaluated to produce
the input value, and the first clause the input value matches,
if any, is selected.
The output expressions of the selected clause are evaluated in sequence,
and the value of the last expression is returned.

</p><p>
An input value matches a clause if it fits the clause's pattern and
passes the clause's guards, if any.
Patterns may contain symbolic constants, which must match exactly, and
pattern variables, which match any input.  
Pattern variables are prefixed by commas; symbolic constants are not.

</p><p>

</p><p><tt>(match&nbsp;'(a&nbsp;17&nbsp;37)<br>

&nbsp;&nbsp;[(a&nbsp;,x)&nbsp;1]<br>

&nbsp;&nbsp;[(b&nbsp;,x&nbsp;,y)&nbsp;2]<br>

&nbsp;&nbsp;[(a&nbsp;,x&nbsp;,y)&nbsp;3])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;3</tt>
</p><p>The first clause fails to match because there are three items in the
input list, and the pattern has only two.
The second clause fails because <tt>b</tt> does not match <tt>a</tt>.

</p><p>
In the output expression, the values of the pattern variables are
bound to the corresponding pieces of input.

</p><p>

</p><p><tt>(match&nbsp;'(a&nbsp;17&nbsp;37)<br>

&nbsp;&nbsp;[(a&nbsp;,x)&nbsp;(-&nbsp;x)]<br>

&nbsp;&nbsp;[(b&nbsp;,x&nbsp;,y)&nbsp;(+&nbsp;x&nbsp;y)]<br>

&nbsp;&nbsp;[(a&nbsp;,x&nbsp;,y)&nbsp;(*&nbsp;x&nbsp;y)])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;629</tt>
</p><p>When followed by an ellipsis (<tt>...</tt>), a pattern variable
represents a sequence of input values.

</p><p>

</p><p><tt>(match&nbsp;'(a&nbsp;17&nbsp;37)&nbsp;[(a&nbsp;,x*&nbsp;...)&nbsp;x*])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;(17&nbsp;37)</tt>
</p><p>By convention, we place a <tt>*</tt> suffix on each pattern variable
that matches a sequence of input expressions.
This is just a convention, however, and not part of the syntax of
<tt>match</tt>.

</p><p>
Ellipses can follow a structured pattern containing one or more
pattern variables.

</p><p>

</p><p><tt>(match&nbsp;'(say&nbsp;(a&nbsp;time)&nbsp;(stitch&nbsp;saves)&nbsp;(in&nbsp;nine))<br>

&nbsp;&nbsp;[(say&nbsp;(,x*&nbsp;,y*)&nbsp;...)&nbsp;(append&nbsp;x*&nbsp;y*)])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;(a&nbsp;stitch&nbsp;in&nbsp;time&nbsp;saves&nbsp;nine)</tt>
</p><p>Ellipses can be nested, producing sequences of sequences of values.

</p><p>

</p><p><tt>(match&nbsp;'((a&nbsp;b&nbsp;c&nbsp;d)&nbsp;(e&nbsp;f&nbsp;g)&nbsp;(h&nbsp;i)&nbsp;(j))<br>

&nbsp;&nbsp;[((,x*&nbsp;,y**&nbsp;...)&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;(list&nbsp;x*&nbsp;y**)])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;((a&nbsp;e&nbsp;h&nbsp;j)&nbsp;((b&nbsp;c&nbsp;d)&nbsp;(f&nbsp;g)&nbsp;(i)&nbsp;()))</tt>
</p><p>Recursion is frequently required while processing an input expression
with <tt>match</tt>.
Here is a simple definition of <tt>length</tt> using <tt>match</tt>.

</p><p>

</p><p><tt>(define&nbsp;length<br>

&nbsp;&nbsp;(lambda&nbsp;(ls)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;ls<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[()&nbsp;0]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,x&nbsp;.&nbsp;,x*)&nbsp;(add1&nbsp;(length&nbsp;x*))])))</tt>
</p><p>Using ellipses may make this more clear.

</p><p>

</p><p><tt>(define&nbsp;length<br>

&nbsp;&nbsp;(lambda&nbsp;(ls)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;ls<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[()&nbsp;0]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,x&nbsp;,x*&nbsp;...)&nbsp;(add1&nbsp;(length&nbsp;x*))])))</tt>
</p><p>Here is a more realistic example of recursion.
It also illustrates the use of guards and the use of
<tt>error</tt> to signal match errors.

</p><p>

</p><p><tt>(define&nbsp;simple-eval<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,i&nbsp;(guard&nbsp;(integer?&nbsp;i))&nbsp;i]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(+&nbsp;,x*&nbsp;...)&nbsp;(apply&nbsp;+&nbsp;(map&nbsp;simple-eval&nbsp;x*))]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(*&nbsp;,x*&nbsp;...)&nbsp;(apply&nbsp;*&nbsp;(map&nbsp;simple-eval&nbsp;x*))]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(-&nbsp;,x&nbsp;,y)&nbsp;(-&nbsp;(simple-eval&nbsp;x)&nbsp;(simple-eval&nbsp;y))]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(/&nbsp;,x&nbsp;,y)&nbsp;(/&nbsp;(simple-eval&nbsp;x)&nbsp;(simple-eval&nbsp;y))]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'simple-eval&nbsp;"invalid&nbsp;expression&nbsp;~s"&nbsp;x)])))</tt>
</p><p>Try out <tt>simple-eval</tt> using Chez Scheme's <tt>new-cafe</tt>
with <tt>simple-eval</tt> as the evaluation procedure:

</p><p>

</p><p><tt>&gt;&nbsp;(new-cafe&nbsp;simple-eval)<br>

&gt;&gt;&nbsp;(+&nbsp;1&nbsp;2&nbsp;3)&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>

6<br>

&gt;&gt;&nbsp;(+&nbsp;(-&nbsp;0&nbsp;1)&nbsp;(+&nbsp;2&nbsp;3))<br>

4<br>

&gt;&gt;&nbsp;(-&nbsp;1&nbsp;2&nbsp;3)
<br>
<br>
Error&nbsp;in&nbsp;simple-eval:&nbsp;invalid&nbsp;expression&nbsp;(-&nbsp;1&nbsp;2&nbsp;3).<br>

Type&nbsp;(debug)&nbsp;to&nbsp;enter&nbsp;the&nbsp;debugger.<br>

&gt;&gt;&nbsp;</tt>
</p><p>Unlike <tt>case</tt> and <tt>cond</tt>, <tt>match</tt> does not have
an explicit <tt>else</tt> clause.
In fact, if a clause of the form

</p><p>

</p><p><tt>&nbsp;&nbsp;[else&nbsp;<i>expr</i>]</tt>
</p><p>is used, it matches (only) the symbol <tt>else</tt>.
To achieve the effect of a catch-all clause, <tt>simple-eval</tt>
uses the pattern <tt>,x</tt> which matches any input.

</p><p>
An even simpler version of the above uses <tt>match</tt>'s
"catamorphism" feature to perform the recursion
automatically.

</p><p>

</p><p><tt>(define&nbsp;simple-eval<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,i&nbsp;(guard&nbsp;(integer?&nbsp;i))&nbsp;i]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(+&nbsp;,[x*]&nbsp;...)&nbsp;(apply&nbsp;+&nbsp;x*)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(*&nbsp;,[x*]&nbsp;...)&nbsp;(apply&nbsp;*&nbsp;x*)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(-&nbsp;,[x]&nbsp;,[y])&nbsp;(-&nbsp;x&nbsp;y)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(/&nbsp;,[x]&nbsp;,[y])&nbsp;(/&nbsp;x&nbsp;y)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'simple-eval&nbsp;"invalid&nbsp;expression&nbsp;~s"&nbsp;x)])))</tt>
</p><p>In the second version of <tt>simple-eval</tt>, the explicit
recursive calls are gone.
Instead, the pattern variables have been written as
<tt>,[<i>var</i>]</tt>; this tells <tt>match</tt> to recur on the
matching subpart of the input before evaluating the
output expressions of the clause.
Parentheses may be used in place of brackets, i.e.,
<tt>,(<i>var</i>)</tt>; we use brackets for readability.

</p><p>
Here is another definition of <tt>length</tt>, this time using the
catamorphism feature.

</p><p>

</p><p><tt>(define&nbsp;length<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[()&nbsp;0]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,x&nbsp;.&nbsp;,[y])&nbsp;(+&nbsp;y&nbsp;1)])))</tt>
</p><p>Since we usually use <tt>match</tt> to write translators from one
language to another, we usually need to build an output expression,
rather than return an output value.
For example, the following converts <tt>let</tt> expressions into
equivalent <tt>lambda</tt> applications.

</p><p>

</p><p><tt>(define&nbsp;translate<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(let&nbsp;((,var*&nbsp;,expr*)&nbsp;...)&nbsp;,body&nbsp;,body*&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`((lambda&nbsp;,var*&nbsp;,body&nbsp;,@body*)&nbsp;,@expr*)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'translate&nbsp;"invalid&nbsp;expression:&nbsp;~s"&nbsp;x)])))
<br>
<br>
(translate&nbsp;'(let&nbsp;((x&nbsp;3)&nbsp;(y&nbsp;4))&nbsp;(+&nbsp;x&nbsp;y)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;((lambda&nbsp;(x&nbsp;y)&nbsp;(+&nbsp;x&nbsp;y))&nbsp;3&nbsp;4)</tt>
</p><p>This procedure uses Scheme's <tt>quasiquote</tt> (backquote),
<tt>unquote</tt> (comma), and <tt>unquote-splicing</tt> (comma-at)
to piece together the output form.
These are not described here, but are described in
<a class="ref" href="http://www.scheme.com/tspl4/objects.html#SECTQUOTING"><i>The
Scheme Programming Language</i></a>.

</p><p>
One useful feature of <tt>quasiquote</tt> not described in these documents
is a <tt>match</tt> extension that allows ellipses to be used in place
of <tt>unquote-splicing</tt>, which often leads to more readable code.

</p><p>

</p><p><tt>(define&nbsp;translate<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(let&nbsp;((,var*&nbsp;,expr*)&nbsp;...)&nbsp;,body&nbsp;,body*&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`((lambda&nbsp;,var*&nbsp;,body&nbsp;,body*&nbsp;...)&nbsp;,expr*&nbsp;...)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'translate&nbsp;"invalid&nbsp;expression:&nbsp;~s"&nbsp;x)])))</tt>
</p><p>Within each subform followed by an ellipsis, each
comma-prefixed item must be a list and all such items within the same
subform must have the same length.
The following procedure extracts two lists from its input and merges
them to form a list of pairs.

</p><p>

</p><p><tt>(define&nbsp;(f&nbsp;x)<br>

&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;[((,a&nbsp;...)&nbsp;(,b&nbsp;...))&nbsp;`((,a&nbsp;.&nbsp;,b)&nbsp;...)]))<br>

(f&nbsp;'((1&nbsp;2&nbsp;3)&nbsp;(a&nbsp;b&nbsp;c)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;((1&nbsp;.&nbsp;a)&nbsp;(2&nbsp;.&nbsp;b)&nbsp;(3&nbsp;.&nbsp;c))</tt>
</p><p>It fails if the two lists have different lengths.

</p><p>

</p><p><tt>(f&nbsp;'((1&nbsp;2&nbsp;3)&nbsp;(a&nbsp;b)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;<i>error</i></tt>
</p><p>The following also fails because one of the items is not even a list.

</p><p>

</p><p><tt>(define&nbsp;(f&nbsp;x)<br>

&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;[(,a&nbsp;,b&nbsp;...)&nbsp;`((,a&nbsp;,b)&nbsp;...)]))<br>

(f&nbsp;'(1&nbsp;2&nbsp;3&nbsp;4))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;<i>error</i></tt>
</p><p>A subform followed by an ellipses may be contained with a larger subform
that is also followed by an ellipsis.
In this case, each comma-prefixed item must be a list of lists, each such
item must have the same length, and the corresponding sublists of each
such item must have the same lengths.
This requirement generalizes to comma-prefixed items nested within
more than two levels of ellispis-followed subforms.

</p><p>
The following procedure illustrates two levels of nested ellipses in both
the input and output.
It converts unnamed <tt>let</tt> expressions into
direct <tt>lambda</tt> applications, where the <tt>let</tt> has been
generalized to allow an implicit <tt>begin</tt> in each right-hand-side
expression.

</p><p>

</p><p><tt>(define&nbsp;(f&nbsp;x)<br>

&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;[(let&nbsp;([,x&nbsp;,e1&nbsp;...]&nbsp;...)&nbsp;,b1&nbsp;,b2&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`((lambda&nbsp;(,x&nbsp;...)&nbsp;,b1&nbsp;,b2&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(begin&nbsp;,e1&nbsp;...)&nbsp;...)]))<br>

(f&nbsp;'(let&nbsp;((x&nbsp;(write&nbsp;'one)&nbsp;3)&nbsp;(y&nbsp;(write&nbsp;'two)&nbsp;4))&nbsp;(list&nbsp;x&nbsp;y)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;"><br>

&nbsp;&nbsp;((lambda&nbsp;(x&nbsp;y)&nbsp;(list&nbsp;x&nbsp;y))<br>

&nbsp;&nbsp;&nbsp;(begin&nbsp;(write&nbsp;'one)&nbsp;3)<br>

&nbsp;&nbsp;&nbsp;(begin&nbsp;(write&nbsp;'two)&nbsp;4))</tt>
</p><p>In the output, a subform may be followed directly by two or more
ellipses; the requirements are the same as for nested ellipses, but
the result is flattened rather than nested, as illustrated by the
following example.

</p><p>

</p><p><tt>(define&nbsp;(f&nbsp;x)<br>

&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;[(foo&nbsp;(,x&nbsp;...)&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(list&nbsp;(car&nbsp;,x)&nbsp;...&nbsp;...)]))<br>

(f&nbsp;'(foo&nbsp;(a)&nbsp;(b&nbsp;c&nbsp;d&nbsp;e)&nbsp;()&nbsp;(f&nbsp;g)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;"><br>

&nbsp;&nbsp;(list&nbsp;(car&nbsp;a)&nbsp;(car&nbsp;b)&nbsp;(car&nbsp;c)&nbsp;(car&nbsp;d)&nbsp;(car&nbsp;e)&nbsp;(car&nbsp;f)&nbsp;(car&nbsp;g))</tt>
</p><p>Sometimes it is useful to explicitly name the operator in a
"cata" subpattern.
Whereas <tt>,[<i>id</i>&nbsp;...]</tt> recurs to the top of the current
<tt>match</tt>, <tt>,[<i>cata</i>&nbsp;-&gt;&nbsp;<i>id</i>&nbsp;...]</tt> recurs
to <tt><i>cata</i></tt>.
<tt><i>cata</i></tt> must evaluate to a procedure that accepts
one argument, the input expression, and returns as many values
as there are identifiers following the <tt>-&gt;</tt>.

</p><p>
This allows processing to be split into several
mutually recursive procedures, as in the following parser for the simple
language defined by the grammar below.

</p><p>

  
  
  
  </p><table><tbody><tr><td nowrap="nowrap">
&lt;Prog&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap"><tt>(program</tt> &lt;Stmt&gt;* &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
&lt;Stmt&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap"><tt>(if</tt> &lt;Expr&gt; &lt;Stmt&gt; &lt;Stmt&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(set!</tt> &lt;var&gt; &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
&lt;Expr&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap">&lt;var&gt;</td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap">&lt;integer&gt;</td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(if</tt> &lt;Expr&gt; &lt;Expr&gt; &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(</tt>&lt;Expr&gt; &lt;Expr&gt;*<tt>)</tt>
</td></tr></tbody></table>

<p>

</p><p><tt>(define&nbsp;parse<br>

&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(define&nbsp;Prog<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(program&nbsp;,[Stmt&nbsp;-&gt;&nbsp;s*]&nbsp;...&nbsp;,[Expr&nbsp;-&gt;&nbsp;e])<br>

&nbsp;&nbsp;&nbsp;&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<br>

&nbsp;&nbsp;&nbsp;&nbsp;(define&nbsp;Expr<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(lambda&nbsp;(x)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;x<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,v&nbsp;(guard&nbsp;(symbol?&nbsp;v))&nbsp;v]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,n&nbsp;(guard&nbsp;(integer?&nbsp;n))&nbsp;n]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(if&nbsp;,[e1]&nbsp;,[e2]&nbsp;,[e3])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(if&nbsp;,e1&nbsp;,e2&nbsp;,e3)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,[rator]&nbsp;,[rand*]&nbsp;...)&nbsp;`(,rator&nbsp;,rand*&nbsp;...)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'parse&nbsp;"invalid&nbsp;expression&nbsp;~s"&nbsp;x)])))<br>

&nbsp;&nbsp;&nbsp;&nbsp;(Prog&nbsp;x)))
<br>
<br>
(parse&nbsp;'(program&nbsp;(set!&nbsp;x&nbsp;3)&nbsp;(+&nbsp;x&nbsp;4)))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;(begin&nbsp;(set!&nbsp;x&nbsp;3)&nbsp;(+&nbsp;x&nbsp;4))</tt>
</p><p>A mentioned above, the operator specified in the cata syntax can be any
expression.
We can make use of this to pass along an environment, if needed, say
to handle a version of the language above extended with a
<tt>let</tt> expression that creates bindings that might shadow keywords.

</p><p>

  
  
  
  </p><table><tbody><tr><td nowrap="nowrap">
&lt;Prog&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap"><tt>(program</tt> &lt;Stmt&gt;* &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
&lt;Stmt&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap"><tt>(if</tt> &lt;Expr&gt; &lt;Stmt&gt; &lt;Stmt&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(set!</tt> &lt;var&gt; &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
&lt;Expr&gt;</td><td align="center"><img src="match_files/1.gif" alt="&lt;graphic&gt;"></td><td nowrap="nowrap">&lt;var&gt;</td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap">&lt;integer&gt;</td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(if</tt> &lt;Expr&gt; &lt;Expr&gt; &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(let</tt> <tt>((</tt><tt><i>var</i></tt> &lt;Expr&gt;<tt>))</tt> &lt;Expr&gt;<tt>)</tt></td></tr><tr><td nowrap="nowrap">
          </td><td align="center">|</td><td nowrap="nowrap"><tt>(</tt>&lt;Expr&gt; &lt;Expr&gt;*<tt>)</tt>
</td></tr></tbody></table>

<p>
In the following version of <tt>parse</tt>, the output is more structured,
with an explicit <tt>call</tt> keyword so that there will not be any
confusion in the output between a call to the value of an <tt>if</tt>
variable and a <tt>if</tt> expression keyed by the <tt>if</tt> keyword.

</p><p>

</p><p><tt>(define&nbs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;(guard&nbsp;(not&nbsp;(memq&nbsp;'if&nbsp;env)))<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(if&nbsp;,e1&nbsp;,e2&nbsp;,e3)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(let&nbsp;([,v&nbsp;,[e]])&nbsp;,[(Expr&nbsp;(cons&nbsp;v&nbsp;env))&nbsp;-&gt;&nbsp;body])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(guard&nbsp;(not&nbsp;(memq&nbsp;'let&nbsp;env))&nbsp;(symbol?&nbsp;v))<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(let&nbsp;([,v&nbsp;,e])&nbsp;,body)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,[rator]&nbsp;,[rand*]&nbsp;...)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;`(call&nbsp;,rator&nbsp;,rand*&nbsp;...)]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,x&nbsp;(error&nbsp;'parse&nbsp;"invalid&nbsp;expression&nbsp;~s"&nbsp;x)]))))<br>

&nbsp;&nbsp;&nbsp;&nbsp;(Prog&nbsp;x)))
<br>
<br>
(parse<br>

&nbsp;&nbsp;'(program&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="match_files/ghostRightarrow.gif">&nbsp;(begin<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let&nbsp;([if&nbsp;(if&nbsp;x&nbsp;list&nbsp;values)])&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;&nbsp;&nbsp;(let&nbsp;([if&nbsp;(if&nbsp;x&nbsp;list&nbsp;values)])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(if&nbsp;1&nbsp;2&nbsp;3))))&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="match_files/ghostRightarrow.gif">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(call&nbsp;if&nbsp;1&nbsp;2&nbsp;3)))</tt>
</p><p>When recurring via a cata call without an explicit operator, e.g.,
within the <tt>Expr</tt> <tt>if</tt> and application cases, the value of
<tt>Expr</tt>'s <tt>env</tt> argument does not change, which is good,
since nothing needs to be added to the environment at those call sites.
The value of <tt>Expr</tt>'s <tt>x</tt> argument does not
change either, so the clause that reads

</p><p>

</p><p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,v&nbsp;(guard&nbsp;(symbol?&nbsp;v))&nbsp;v]</tt>
</p><p>should not be replaced by one that reads

</p><p>

</p><p><tt>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[,v&nbsp;(guard&nbsp;(symbol?&nbsp;v))&nbsp;x]</tt>
</p><p>or nested expressions will not be handled properly.
In general, the values of any variables bound outside a <tt>match</tt>
expression do not change when a <tt>cata</tt> call is used to recur
directly to the <tt>match</tt> itself.

</p><p>
<a name="mrvs"></a>In some cases, a procedure that uses <tt>match</tt> will need to return
multiple values.
Programs return multiple values using the <tt>values</tt>
procedure and receive them using the <tt>let-values</tt>
syntactic form.
These features are described in
<a class="ref" href="http://www.scheme.com/tspl4/control.html#SECTMRVS"><i>The
Scheme Programming Language</i></a>.
Here is a simple example adapted from <i>The Scheme Programming Language</i>.

</p><p>

</p><p><tt>(define&nbsp;split<br>

&nbsp;&nbsp;(lambda&nbsp;(ls)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(if&nbsp;(or&nbsp;(null?&nbsp;ls)&nbsp;(null?&nbsp;(cdr&nbsp;ls)))<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(values&nbsp;ls&nbsp;'())<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(let-values&nbsp;([(odds&nbsp;evens)&nbsp;(split&nbsp;(cddr&nbsp;ls))])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(values&nbsp;(cons&nbsp;(car&nbsp;ls)&nbsp;odds)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(cons&nbsp;(cadr&nbsp;ls)&nbsp;evens))))))
<br>
<br>
(split&nbsp;'(a&nbsp;b&nbsp;c&nbsp;d&nbsp;e&nbsp;f))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;(a&nbsp;c&nbsp;e)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="match_files/ghostRightarrow.gif">&nbsp;(b&nbsp;d&nbsp;f)</tt>
</p><p>The <tt>values</tt> procedure is used to return multiple values; it
takes an arbitrary number of arguments and returns them as the values.
If passed one value, <tt>values</tt> behaves like the identity.
<tt>let-values</tt> is like <tt>let</tt>, except that each binding binds
zero or more variables to zero or more return values.

</p><p>
The pattern matcher cata syntax can also be used to receive multiple
values.
When making implicit recursive calls using the cata (<tt>,[]</tt>) syntax,
one can include zero or more variables between the brackets
(after the <tt>-&gt;</tt>, if one is present),
each representing one of the expected return values.
<tt>split</tt> may thus be defined using <tt>match</tt> as follows.

</p><p>

</p><p><tt>(define&nbsp;split<br>

&nbsp;&nbsp;(lambda&nbsp;(ls)<br>

&nbsp;&nbsp;&nbsp;&nbsp;(match&nbsp;ls<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[()&nbsp;(values&nbsp;'()&nbsp;'())]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,x)&nbsp;(values&nbsp;`(,x)&nbsp;'())]<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;[(,x&nbsp;,y&nbsp;.&nbsp;,[odds&nbsp;evens])<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(values&nbsp;(cons&nbsp;x&nbsp;odds)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;(cons&nbsp;y&nbsp;evens))])))
<br>
<br>
(split&nbsp;'(a&nbsp;b&nbsp;c&nbsp;d&nbsp;e&nbsp;f))&nbsp;<img src="match_files/0.gif" alt="&lt;graphic&gt;">&nbsp;(a&nbsp;c&nbsp;e)<br>

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<img src="match_files/ghostRightarrow.gif">&nbsp;(b&nbsp;d&nbsp;f)</tt>
</p><p>
 
 
</p><p></p><hr>©&nbsp;2013&nbsp;R. Kent Dybvig
<p>

</p><div id="link64_vdp_tabid" style="display:none;">1865</div></body><div id="abineFillElement"></div></html>