<html> <head> <title>Teach Yourself Scheme in Fixnum Days: Just the Code Samples</title> <style > body { color: black; /* background-color: #e5e5e5;*/ background-color: #ffffff; /*background-color: beige;*/ margin-top: 2em; margin-right: 8%; } h1,h2,h3,h4,h5,h6 { margin-top: .5em; } .title { font-size: 200%; font-weight: normal; } .partheading { font-size: 100%; } .chapterheading { font-size: 100%; } .beginsection { font-size: 110%; } .tiny { font-size: 40%; } .scriptsize { font-size: 60%; } .footnotesize { font-size: 75%; } .small { font-size: 90%; } .normalsize { font-size: 100%; } .large { font-size: 120%; } .largecap { font-size: 150%; } .largeup { font-size: 200%; } .huge { font-size: 300%; } .hugecap { font-size: 350%; } pre { margin-left: 2em; } blockquote { margin-left: 2em; } ol { list-style-type: decimal; } ol ol { list-style-type: lower-alpha; } ol ol ol { list-style-type: lower-roman; } ol ol ol ol { list-style-type: upper-alpha; } /* .verbatim { color: #4d0000; } */ tt i { font-family: serif; } .verbatim em { font-family: serif; } .scheme em { font-family: serif; color: black; } .scheme { color: brown; } .scheme .keyword { color: #990000; font-weight: bold; } .scheme .builtin { color: #990000; } .scheme .variable { color: navy; } .scheme .global { color: purple; } .scheme .selfeval { color: green; } .scheme .comment { color: teal; } .schemeresponse { color: green; } .navigation { color: red; text-align: right; font-size: medium; font-style: italic; } .disable { /* color: #e5e5e5; */ color: gray; } .smallcaps { font-size: 75%; } .smallprint { color: gray; font-size: 75%; text-align: right; } /* .smallprint hr { text-align: left; width: 40%; } */ .footnoterule { text-align: left; width: 40%; } .colophon { color: gray; font-size: 80%; text-align: right; } .colophon a { color: gray; } pre { border:1px solid #CCC; padding:5px; } </style> </head> <body> <p><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html'>Teach Yourself Scheme in <code>Fixnum</code> Days</a> -- Just The Code Samples</p> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-3.html'>Chapter 1</a></h2> <pre class=scheme><span class=comment>;The first program</span> (<span class=keyword>begin</span> (<span class=variable>display</span> <span class=selfeval>&quot;Hello, World!&quot;</span>) (<span class=variable>newline</span>)) </pre> <pre class=verbatim>mzscheme&nbsp; </pre> <pre class=scheme>(<span class=variable>load</span> <span class=selfeval>&quot;hello.scm&quot;</span>) </pre> <pre class=scheme>(<span class=keyword>begin</span> (<span class=variable>display</span> <span class=selfeval>&quot;Hello, World!&quot;</span>) (<span class=variable>newline</span>)) </pre> <pre class=verbatim>Hello, World!&nbsp; </pre> <pre class=scheme><span class=selfeval>&quot;Hello, World!&quot;</span> </pre> <pre class=scheme><span class=variable>E</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>v</span> </pre> <pre class=scheme>(<span class=keyword>begin</span> (<span class=variable>display</span> <span class=selfeval>&quot;Hello, World!&quot;</span>) (<span class=variable>newline</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> </pre> <pre class=verbatim>Hello, World!&nbsp; </pre> <pre class=scheme><span class=selfeval>&quot;Hello, World!&quot;</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;Hello, World!&quot;</span> </pre> <pre class=scheme>(<span class=variable>exit</span>) </pre> <pre class=verbatim>mzscheme -r hello.scm&nbsp; </pre> <pre class=verbatim>echo Hello, World!&nbsp; </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-4.html'>Chapter 2</a></h2> <pre class=scheme>(<span class=variable>boolean?</span> <span class=selfeval>#t</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>boolean?</span> <span class=selfeval>&quot;Hello, World!&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>not</span> <span class=selfeval>#f</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>not</span> <span class=selfeval>#t</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>not</span> <span class=selfeval>&quot;Hello, World!&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>number?</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>number?</span> <span class=selfeval>#t</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>complex?</span> <span class=selfeval>2+3i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>real?</span> <span class=selfeval>2+3i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>real?</span> <span class=selfeval>3.1416</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>real?</span> <span class=selfeval>22/7</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>real?</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>rational?</span> <span class=selfeval>2+3i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>rational?</span> <span class=selfeval>3.1416</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>rational?</span> <span class=selfeval>22/7</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>integer?</span> <span class=selfeval>22/7</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>integer?</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>eqv?</span> <span class=selfeval>42</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>eqv?</span> <span class=selfeval>42</span> <span class=selfeval>#f</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>eqv?</span> <span class=selfeval>42</span> <span class=selfeval>42.0</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>=</span> <span class=selfeval>42</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>=</span> <span class=selfeval>42</span> <span class=selfeval>#f</span>) <font color="red"><tt><strong><i>--&gt;ERROR!!!</i></strong></tt></font> (<span class=variable>=</span> <span class=selfeval>42</span> <span class=selfeval>42.0</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>&lt;</span> <span class=selfeval>3</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>&gt;=</span> <span class=selfeval>4.5</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> (<span class=variable>-</span> <span class=selfeval>5.3</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3.3</span> (<span class=variable>-</span> <span class=selfeval>5</span> <span class=selfeval>2</span> <span class=selfeval>1</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> (<span class=variable>*</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> (<span class=variable>/</span> <span class=selfeval>6</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> (<span class=variable>/</span> <span class=selfeval>22</span> <span class=selfeval>7</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>22/7</span> (<span class=variable>expt</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>8</span> (<span class=variable>expt</span> <span class=selfeval>4</span> <span class=selfeval>1/2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2.0</span> </pre> <pre class=scheme>(<span class=variable>-</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>-4</span> (<span class=variable>/</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1/4</span> </pre> <pre class=scheme>(<span class=variable>max</span> <span class=selfeval>1</span> <span class=selfeval>3</span> <span class=selfeval>4</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>4</span> (<span class=variable>min</span> <span class=selfeval>1</span> <span class=selfeval>3</span> <span class=selfeval>4</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> </pre> <pre class=scheme>(<span class=variable>abs</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3</span> (<span class=variable>abs</span> <span class=selfeval>-4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>4</span> </pre> <pre class=scheme>(<span class=variable>char?</span> <span class=selfeval>#\c</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>char?</span> <span class=selfeval>1</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>char?</span> <span class=selfeval>#\;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>char=?</span> <span class=selfeval>#\a</span> <span class=selfeval>#\a</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>char&lt;?</span> <span class=selfeval>#\a</span> <span class=selfeval>#\b</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>char&gt;=?</span> <span class=selfeval>#\a</span> <span class=selfeval>#\b</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>char-ci=?</span> <span class=selfeval>#\a</span> <span class=selfeval>#\A</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>char-ci&lt;?</span> <span class=selfeval>#\a</span> <span class=selfeval>#\B</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>char-downcase</span> <span class=selfeval>#\A</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\a</span> (<span class=variable>char-upcase</span> <span class=selfeval>#\a</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\A</span> </pre> <pre class=scheme><span class=selfeval>#t</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> <span class=selfeval>42</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>42</span> <span class=selfeval>#\c</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\c</span> </pre> <pre class=scheme>(<span class=keyword>quote</span> <span class=variable>xyz</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>xyz</span> </pre> <pre class=scheme><span class=keyword>'</span><span class=variable>E</span> </pre> <pre class=scheme>(<span class=keyword>quote</span> <span class=variable>E</span>) </pre> <pre class=scheme>(<span class=variable>symbol?</span> <span class=keyword>'</span><span class=variable>xyz</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>symbol?</span> <span class=selfeval>42</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>eqv?</span> <span class=keyword>'</span><span class=variable>Calorie</span> <span class=keyword>'</span><span class=variable>calorie</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>xyz</span> <span class=selfeval>9</span>) </pre> <pre class=scheme><span class=variable>xyz</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>9</span> </pre> <pre class=scheme>(<span class=keyword>set!</span> <span class=variable>xyz</span> <span class=selfeval>#\c</span>) </pre> <pre class=scheme><span class=variable>xyz</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\c</span> </pre> <pre class=scheme><span class=selfeval>&quot;Hello, World!&quot;</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;Hello, World!&quot;</span> </pre> <pre class=scheme>(<span class=variable>string</span> <span class=selfeval>#\h</span> <span class=selfeval>#\e</span> <span class=selfeval>#\l</span> <span class=selfeval>#\l</span> <span class=selfeval>#\o</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;hello&quot;</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>greeting</span> <span class=selfeval>&quot;Hello; Hello!&quot;</span>) </pre> <pre class=scheme>(<span class=variable>string-ref</span> <span class=variable>greeting</span> <span class=selfeval>0</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\H</span> </pre> <pre class=scheme>(<span class=variable>string-append</span> <span class=selfeval>&quot;E &quot;</span> <span class=selfeval>&quot;Pluribus &quot;</span> <span class=selfeval>&quot;Unum&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;E Pluribus Unum&quot;</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>a-3-char-long-string</span> (<span class=variable>make-string</span> <span class=selfeval>3</span>)) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>hello</span> (<span class=variable>string</span> <span class=selfeval>#\H</span> <span class=selfeval>#\e</span> <span class=selfeval>#\l</span> <span class=selfeval>#\l</span> <span class=selfeval>#\o</span>)) <span class=variable>hello</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;Hello&quot;</span> (<span class=variable>string-set!</span> <span class=variable>hello</span> <span class=selfeval>1</span> <span class=selfeval>#\a</span>) <span class=variable>hello</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;Hallo&quot;</span> </pre> <pre class=scheme>(<span class=variable>vector</span> <span class=selfeval>0</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#</span>(<span class=selfeval>0</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>v</span> (<span class=variable>make-vector</span> <span class=selfeval>5</span>)) </pre> <pre class=scheme>(<span class=variable>cons</span> <span class=selfeval>1</span> <span class=selfeval>#t</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> . <span class=selfeval>#t</span>) </pre> <pre class=scheme><span class=keyword>'</span>(<span class=selfeval>1</span> . <span class=selfeval>#t</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> . <span class=selfeval>#t</span>) (<span class=selfeval>1</span> . <span class=selfeval>#t</span>) <font color="red"><tt><strong><i>--&gt;ERROR!!!</i></strong></tt></font> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>x</span> (<span class=variable>cons</span> <span class=selfeval>1</span> <span class=selfeval>#t</span>)) (<span class=variable>car</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=variable>cdr</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> </pre> <pre class=scheme>(<span class=variable>set-car!</span> <span class=variable>x</span> <span class=selfeval>2</span>) (<span class=variable>set-cdr!</span> <span class=variable>x</span> <span class=selfeval>#f</span>) <span class=variable>x</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>2</span> . <span class=selfeval>#f</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>y</span> (<span class=variable>cons</span> (<span class=variable>cons</span> <span class=selfeval>1</span> <span class=selfeval>2</span>) <span class=selfeval>3</span>)) <span class=variable>y</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> ((<span class=selfeval>1</span> . <span class=selfeval>2</span>) . <span class=selfeval>3</span>) </pre> <pre class=scheme>(<span class=variable>car</span> (<span class=variable>car</span> <span class=variable>y</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=variable>cdr</span> (<span class=variable>car</span> <span class=variable>y</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> </pre> <pre class=scheme>(<span class=variable>caar</span> <span class=variable>y</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=variable>cdar</span> <span class=variable>y</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> </pre> <pre class=scheme>(<span class=variable>cons</span> <span class=selfeval>1</span> (<span class=variable>cons</span> <span class=selfeval>2</span> (<span class=variable>cons</span> <span class=selfeval>3</span> (<span class=variable>cons</span> <span class=selfeval>4</span> <span class=selfeval>5</span>)))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span> . <span class=selfeval>5</span>) </pre> <pre class=scheme><span class=keyword>'</span>() <font color="teal"><tt><strong>=&gt;</strong></tt></font> () </pre> <pre class=scheme>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) </pre> <pre class=scheme>(<span class=variable>cons</span> <span class=selfeval>1</span> (<span class=variable>cons</span> <span class=selfeval>2</span> (<span class=variable>cons</span> <span class=selfeval>3</span> (<span class=variable>cons</span> <span class=selfeval>4</span> <span class=keyword>'</span>())))) </pre> <pre class=scheme>(<span class=variable>list</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) </pre> <pre class=scheme><span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>y</span> (<span class=variable>list</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>)) (<span class=variable>list-ref</span> <span class=variable>y</span> <span class=selfeval>0</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=variable>list-ref</span> <span class=variable>y</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>4</span> (<span class=variable>list-tail</span> <span class=variable>y</span> <span class=selfeval>1</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span>) (<span class=variable>list-tail</span> <span class=variable>y</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>4</span>) </pre> <pre class=scheme>(<span class=variable>pair?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> . <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>pair?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>pair?</span> <span class=keyword>'</span>()) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>list?</span> <span class=keyword>'</span>()) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>null?</span> <span class=keyword>'</span>()) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>list?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>list?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> . <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>null?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=variable>null?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> . <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>char-&gt;integer</span> <span class=selfeval>#\d</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>100</span> (<span class=variable>integer-&gt;char</span> <span class=selfeval>50</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\2</span> </pre> <pre class=scheme>(<span class=variable>string-&gt;list</span> <span class=selfeval>&quot;hello&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>#\h</span> <span class=selfeval>#\e</span> <span class=selfeval>#\l</span> <span class=selfeval>#\l</span> <span class=selfeval>#\o</span>) </pre> <pre class=scheme>(<span class=variable>number-&gt;string</span> <span class=selfeval>16</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;16&quot;</span> </pre> <pre class=scheme>(<span class=variable>string-&gt;number</span> <span class=selfeval>&quot;16&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>16</span> (<span class=variable>string-&gt;number</span> <span class=selfeval>&quot;Am I a hot number?&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=variable>string-&gt;number</span> <span class=selfeval>&quot;16&quot;</span> <span class=selfeval>8</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>14</span> </pre> <pre class=scheme>(<span class=variable>symbol-&gt;string</span> <span class=keyword>'</span><span class=variable>symbol</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;symbol&quot;</span> (<span class=variable>string-&gt;symbol</span> <span class=selfeval>&quot;string&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>string</span> </pre> <pre class=scheme><span class=variable>cons</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>&lt;procedure&gt;</span> </pre> <pre class=scheme>(<span class=variable>display</span> <span class=selfeval>&quot;Hello, World!&quot;</span> (<span class=variable>current-output-port</span>)) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-5.html'>Chapter 3</a></h2> <pre class=scheme>(<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>+</span> <span class=variable>x</span> <span class=selfeval>2</span>)) </pre> <pre class=scheme>((<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>+</span> <span class=variable>x</span> <span class=selfeval>2</span>)) <span class=selfeval>5</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>7</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>add2</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>+</span> <span class=variable>x</span> <span class=selfeval>2</span>))) </pre> <pre class=scheme>(<span class=variable>add2</span> <span class=selfeval>4</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> (<span class=variable>add2</span> <span class=selfeval>9</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>11</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>area</span> (<span class=keyword>lambda</span> (<span class=variable>length</span> <span class=variable>breadth</span>) (<span class=variable>*</span> <span class=variable>length</span> <span class=variable>breadth</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>area</span> <span class=variable>*</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>x</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>)) (<span class=variable>apply</span> <span class=variable>+</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> </pre> <pre class=scheme>(<span class=variable>apply</span> <span class=variable>+</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>12</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>display3</span> (<span class=keyword>lambda</span> (<span class=variable>arg1</span> <span class=variable>arg2</span> <span class=variable>arg3</span>) (<span class=keyword>begin</span> (<span class=variable>display</span> <span class=variable>arg1</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>display</span> <span class=variable>arg2</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>display</span> <span class=variable>arg3</span>) (<span class=variable>newline</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>display3</span> (<span class=keyword>lambda</span> (<span class=variable>arg1</span> <span class=variable>arg2</span> <span class=variable>arg3</span>) (<span class=variable>display</span> <span class=variable>arg1</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>display</span> <span class=variable>arg2</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>display</span> <span class=variable>arg3</span>) (<span class=variable>newline</span>))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-6.html'>Chapter 4</a></h2> <pre class=scheme>(<span class=keyword>if</span> <span class=variable>test-expression</span> <span class=variable>then-branch</span> <span class=variable>else-branch</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>p</span> <span class=selfeval>80</span>) (<span class=keyword>if</span> (<span class=variable>&gt;</span> <span class=variable>p</span> <span class=selfeval>70</span>) <span class=keyword>'</span><span class=variable>safe</span> <span class=keyword>'</span><span class=variable>unsafe</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>safe</span> (<span class=keyword>if</span> (<span class=variable>&lt;</span> <span class=variable>p</span> <span class=selfeval>90</span>) <span class=keyword>'</span><span class=variable>low-pressure</span>) <span class=comment>;no ``else'' branch</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>low-pressure</span> </pre> <pre class=scheme>(<span class=keyword>when</span> (<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>)) </pre> <pre class=scheme>(<span class=keyword>if</span> (<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=keyword>begin</span> (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>))) </pre> <pre class=scheme>(<span class=keyword>unless</span> (<span class=variable>&gt;=</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>)) </pre> <pre class=scheme>(<span class=keyword>if</span> (<span class=variable>char&lt;?</span> <span class=variable>c</span> <span class=selfeval>#\c</span>) <span class=selfeval>-1</span> (<span class=keyword>if</span> (<span class=variable>char=?</span> <span class=variable>c</span> <span class=selfeval>#\c</span>) <span class=selfeval>0</span> <span class=selfeval>1</span>)) </pre> <pre class=scheme>(<span class=keyword>cond</span> ((<span class=variable>char&lt;?</span> <span class=variable>c</span> <span class=selfeval>#\c</span>) <span class=selfeval>-1</span>) ((<span class=variable>char=?</span> <span class=variable>c</span> <span class=selfeval>#\c</span>) <span class=selfeval>0</span>) (<span class=keyword>else</span> <span class=selfeval>1</span>)) </pre> <pre class=scheme>(<span class=keyword>case</span> <span class=variable>c</span> ((<span class=selfeval>#\a</span>) <span class=selfeval>1</span>) ((<span class=selfeval>#\b</span>) <span class=selfeval>2</span>) ((<span class=selfeval>#\c</span>) <span class=selfeval>3</span>) (<span class=keyword>else</span> <span class=selfeval>4</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3</span> </pre> <pre class=scheme>(<span class=keyword>and</span> <span class=selfeval>1</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> (<span class=keyword>and</span> <span class=selfeval>#f</span> <span class=selfeval>1</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=keyword>or</span> <span class=selfeval>1</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=keyword>or</span> <span class=selfeval>#f</span> <span class=selfeval>1</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> </pre> <pre class=scheme>(<span class=keyword>and</span> <span class=selfeval>1</span> <span class=selfeval>#f</span> <span class=variable>expression-guaranteed-to-cause-error</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> (<span class=keyword>or</span> <span class=selfeval>1</span> <span class=selfeval>#f</span> <span class=variable>expression-guaranteed-to-cause-error</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-7.html'>Chapter 5</a></h2> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>x</span> <span class=selfeval>9</span>) (<span class=keyword>define</span> <span class=variable>add2</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>+</span> <span class=variable>x</span> <span class=selfeval>2</span>))) <span class=variable>x</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>9</span> (<span class=variable>add2</span> <span class=selfeval>3</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>5</span> (<span class=variable>add2</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>11</span> <span class=variable>x</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>9</span> </pre> <pre class=scheme>(<span class=keyword>set!</span> <span class=variable>x</span> <span class=selfeval>20</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>add2</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=keyword>set!</span> <span class=variable>x</span> (<span class=variable>+</span> <span class=variable>x</span> <span class=selfeval>2</span>)) <span class=variable>x</span>)) </pre> <pre class=scheme>(<span class=variable>add2</span> <span class=variable>x</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>22</span> </pre> <pre class=scheme><span class=variable>x</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>20</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>counter</span> <span class=selfeval>0</span>) (<span class=keyword>define</span> <span class=variable>bump-counter</span> (<span class=keyword>lambda</span> () (<span class=keyword>set!</span> <span class=variable>counter</span> (<span class=variable>+</span> <span class=variable>counter</span> <span class=selfeval>1</span>)) <span class=variable>counter</span>)) </pre> <pre class=scheme>(<span class=variable>bump-counter</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=variable>bump-counter</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> (<span class=variable>bump-counter</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3</span> </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>x</span> <span class=selfeval>1</span>) (<span class=variable>y</span> <span class=selfeval>2</span>) (<span class=variable>z</span> <span class=selfeval>3</span>)) (<span class=variable>list</span> <span class=variable>x</span> <span class=variable>y</span> <span class=variable>z</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>x</span> <span class=selfeval>1</span>) (<span class=variable>y</span> <span class=variable>x</span>)) (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>21</span> </pre> <pre class=scheme>(<span class=keyword>let*</span> ((<span class=variable>x</span> <span class=selfeval>1</span>) (<span class=variable>y</span> <span class=variable>x</span>)) (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>x</span> <span class=selfeval>1</span>)) (<span class=keyword>let</span> ((<span class=variable>y</span> <span class=variable>x</span>)) (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>cons</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)))) (<span class=variable>cons</span> <span class=selfeval>1</span> <span class=selfeval>2</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3</span> </pre> <pre class=scheme>(<span class=keyword>fluid-let</span> ((<span class=variable>counter</span> <span class=selfeval>99</span>)) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>)) </pre> <pre class=verbatim>100&nbsp; 101&nbsp; 102&nbsp; </pre> <pre class=scheme><span class=variable>counter</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>3</span> </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>counter</span> <span class=selfeval>99</span>)) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>) (<span class=variable>display</span> (<span class=variable>bump-counter</span>)) (<span class=variable>newline</span>)) </pre> <pre class=scheme><span class=selfeval>4</span> <span class=selfeval>5</span> <span class=selfeval>6</span> </pre> <pre class=scheme><span class=variable>counter</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-8.html'>Chapter 6</a></h2> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>factorial</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>1</span> (<span class=variable>*</span> <span class=variable>n</span> (<span class=variable>factorial</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>is-even?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#t</span> (<span class=variable>is-odd?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))))) (<span class=keyword>define</span> <span class=variable>is-odd?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#f</span> (<span class=variable>is-even?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))))) </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>local-even?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#t</span> (<span class=variable>local-odd?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))))) (<span class=variable>local-odd?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#f</span> (<span class=variable>local-even?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>)))))) (<span class=variable>list</span> (<span class=variable>local-even?</span> <span class=selfeval>23</span>) (<span class=variable>local-odd?</span> <span class=selfeval>23</span>))) </pre> <pre class=scheme>(<span class=keyword>letrec</span> ((<span class=variable>local-even?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#t</span> (<span class=variable>local-odd?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))))) (<span class=variable>local-odd?</span> (<span class=keyword>lambda</span> (<span class=variable>n</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>0</span>) <span class=selfeval>#f</span> (<span class=variable>local-even?</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>)))))) (<span class=variable>list</span> (<span class=variable>local-even?</span> <span class=selfeval>23</span>) (<span class=variable>local-odd?</span> <span class=selfeval>23</span>))) </pre> <pre class=scheme>(<span class=keyword>letrec</span> ((<span class=variable>countdown</span> (<span class=keyword>lambda</span> (<span class=variable>i</span>) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>i</span> <span class=selfeval>0</span>) <span class=keyword>'</span><span class=variable>liftoff</span> (<span class=keyword>begin</span> (<span class=variable>display</span> <span class=variable>i</span>) (<span class=variable>newline</span>) (<span class=variable>countdown</span> (<span class=variable>-</span> <span class=variable>i</span> <span class=selfeval>1</span>))))))) (<span class=variable>countdown</span> <span class=selfeval>10</span>)) </pre> <pre class=scheme>(<span class=keyword>let</span> <span class=variable>countdown</span> ((<span class=variable>i</span> <span class=selfeval>10</span>)) (<span class=keyword>if</span> (<span class=variable>=</span> <span class=variable>i</span> <span class=selfeval>0</span>) <span class=keyword>'</span><span class=variable>liftoff</span> (<span class=keyword>begin</span> (<span class=variable>display</span> <span class=variable>i</span>) (<span class=variable>newline</span>) (<span class=variable>countdown</span> (<span class=variable>-</span> <span class=variable>i</span> <span class=selfeval>1</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>list-position</span> (<span class=keyword>lambda</span> (<span class=variable>o</span> <span class=variable>l</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>0</span>) (<span class=variable>l</span> <span class=variable>l</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>l</span>) <span class=selfeval>#f</span> (<span class=keyword>if</span> (<span class=variable>eqv?</span> (<span class=variable>car</span> <span class=variable>l</span>) <span class=variable>o</span>) <span class=variable>i</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>cdr</span> <span class=variable>l</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>reverse!</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>s</span> <span class=variable>s</span>) (<span class=variable>r</span> <span class=keyword>'</span>())) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=variable>r</span> (<span class=keyword>let</span> ((<span class=variable>d</span> (<span class=variable>cdr</span> <span class=variable>s</span>))) (<span class=variable>set-cdr!</span> <span class=variable>s</span> <span class=variable>r</span>) (<span class=variable>loop</span> <span class=variable>d</span> <span class=variable>s</span>)))))) </pre> <pre class=scheme>(<span class=variable>map</span> <span class=variable>add2</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>3</span> <span class=selfeval>4</span> <span class=selfeval>5</span>) </pre> <pre class=scheme>(<span class=variable>for-each</span> <span class=variable>display</span> (<span class=variable>list</span> <span class=selfeval>&quot;one &quot;</span> <span class=selfeval>&quot;two &quot;</span> <span class=selfeval>&quot;buckle my shoe&quot;</span>)) </pre> <pre class=scheme>(<span class=variable>map</span> <span class=variable>cons</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <span class=keyword>'</span>(<span class=selfeval>10</span> <span class=selfeval>20</span> <span class=selfeval>30</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> ((<span class=selfeval>1</span> . <span class=selfeval>10</span>) (<span class=selfeval>2</span> . <span class=selfeval>20</span>) (<span class=selfeval>3</span> . <span class=selfeval>30</span>)) (<span class=variable>map</span> <span class=variable>+</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <span class=keyword>'</span>(<span class=selfeval>10</span> <span class=selfeval>20</span> <span class=selfeval>30</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>11</span> <span class=selfeval>22</span> <span class=selfeval>33</span>) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-9.html'>Chapter 7</a></h2> <pre class=scheme>(<span class=variable>display</span> <span class=selfeval>9</span>) (<span class=variable>display</span> <span class=selfeval>9</span> (<span class=variable>current-output-port</span>)) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>i</span> (<span class=variable>open-input-file</span> <span class=selfeval>&quot;hello.txt&quot;</span>)) (<span class=variable>read-char</span> <span class=variable>i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\h</span> (<span class=keyword>define</span> <span class=variable>j</span> (<span class=variable>read</span> <span class=variable>i</span>)) <span class=variable>j</span> <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>ello</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>o</span> (<span class=variable>open-output-file</span> <span class=selfeval>&quot;greeting.txt&quot;</span>)) (<span class=variable>display</span> <span class=selfeval>&quot;hello&quot;</span> <span class=variable>o</span>) (<span class=variable>write-char</span> <span class=selfeval>#\space</span> <span class=variable>o</span>) (<span class=variable>display</span> <span class=keyword>'</span><span class=variable>world</span> <span class=variable>o</span>) (<span class=variable>newline</span> <span class=variable>o</span>) (<span class=variable>close-output-port</span> <span class=variable>o</span>) </pre> <pre class=verbatim>hello world&nbsp; </pre> <pre class=scheme>(<span class=variable>call-with-input-file</span> <span class=selfeval>&quot;hello.txt&quot;</span> (<span class=keyword>lambda</span> (<span class=variable>i</span>) (<span class=keyword>let*</span> ((<span class=variable>a</span> (<span class=variable>read-char</span> <span class=variable>i</span>)) (<span class=variable>b</span> (<span class=variable>read-char</span> <span class=variable>i</span>)) (<span class=variable>c</span> (<span class=variable>read-char</span> <span class=variable>i</span>))) (<span class=variable>list</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>c</span>)))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> (<span class=selfeval>#\h</span> <span class=selfeval>#\e</span> <span class=selfeval>#\l</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>i</span> (<span class=variable>open-input-string</span> <span class=selfeval>&quot;hello world&quot;</span>)) (<span class=variable>read-char</span> <span class=variable>i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#\h</span> (<span class=variable>read</span> <span class=variable>i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>ello</span> (<span class=variable>read</span> <span class=variable>i</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>world</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>o</span> (<span class=variable>open-output-string</span>)) (<span class=variable>write</span> <span class=keyword>'</span><span class=variable>hello</span> <span class=variable>o</span>) (<span class=variable>write-char</span> <span class=selfeval>#\,</span> <span class=variable>o</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span> <span class=variable>o</span>) (<span class=variable>display</span> <span class=selfeval>&quot;world&quot;</span> <span class=variable>o</span>) </pre> <pre class=scheme>(<span class=variable>get-output-string</span> <span class=variable>o</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;hello, world&quot;</span> </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-10.html'>Chapter 8</a></h2> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>when</span> (<span class=keyword>lambda</span> (<span class=variable>test</span> . <span class=variable>branch</span>) (<span class=variable>list</span> <span class=keyword>'</span><span class=keyword>if</span> <span class=variable>test</span> (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>begin</span> <span class=variable>branch</span>)))) </pre> <pre class=scheme>(<span class=keyword>when</span> (<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>)) </pre> <pre class=scheme>(<span class=variable>apply</span> (<span class=keyword>lambda</span> (<span class=variable>test</span> . <span class=variable>branch</span>) (<span class=variable>list</span> <span class=keyword>'</span><span class=keyword>if</span> <span class=variable>test</span> (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>begin</span> <span class=variable>branch</span>))) <span class=keyword>'</span>((<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>))) </pre> <pre class=scheme>(<span class=keyword>if</span> (<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=keyword>begin</span> (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>unless</span> (<span class=keyword>lambda</span> (<span class=variable>test</span> . <span class=variable>branch</span>) (<span class=variable>list</span> <span class=keyword>'</span><span class=keyword>if</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>not</span> <span class=variable>test</span>) (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>begin</span> <span class=variable>branch</span>)))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>unless</span> (<span class=keyword>lambda</span> (<span class=variable>test</span> . <span class=variable>branch</span>) (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>when</span> (<span class=variable>cons</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>not</span> <span class=variable>test</span>) <span class=variable>branch</span>)))) </pre> <pre class=scheme>(<span class=variable>list</span> <span class=keyword>'</span><span class=keyword>if</span> <span class=variable>test</span> (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>begin</span> <span class=variable>branch</span>)) </pre> <pre class=scheme>(<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) </pre> <pre class=scheme>((<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>)) </pre> <pre class=scheme>(<span class=variable>list</span> <span class=keyword>'</span><span class=keyword>IF</span> <span class=variable>test</span> (<span class=variable>cons</span> <span class=keyword>'</span><span class=keyword>BEGIN</span> <span class=variable>branch</span>)) </pre> <pre class=scheme><span class=keyword>`</span>(<span class=keyword>IF</span> <span class=keyword>,</span><span class=variable>test</span> (<span class=keyword>BEGIN</span> <span class=keyword>,@</span><span class=variable>branch</span>)) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>when</span> (<span class=keyword>lambda</span> (<span class=variable>test</span> . <span class=variable>branch</span>) <span class=keyword>`</span>(<span class=keyword>IF</span> <span class=keyword>,</span><span class=variable>test</span> (<span class=keyword>BEGIN</span> <span class=keyword>,@</span><span class=variable>branch</span>)))) </pre> <pre class=scheme>(<span class=keyword>IF</span> (<span class=variable>&lt;</span> (<span class=variable>pressure</span> <span class=variable>tube</span>) <span class=selfeval>60</span>) (<span class=keyword>BEGIN</span> (<span class=variable>open-valve</span> <span class=variable>tube</span>) (<span class=variable>attach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>depress</span> <span class=variable>floor-pump</span> <span class=selfeval>5</span>) (<span class=variable>detach</span> <span class=variable>floor-pump</span> <span class=variable>tube</span>) (<span class=variable>close-valve</span> <span class=variable>tube</span>))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>my-or</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) <span class=keyword>`</span>(<span class=keyword>if</span> <span class=keyword>,</span><span class=variable>x</span> <span class=keyword>,</span><span class=variable>x</span> <span class=keyword>,</span><span class=variable>y</span>))) </pre> <pre class=scheme>(<span class=keyword>my-or</span> <span class=selfeval>1</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>1</span> (<span class=keyword>my-or</span> <span class=selfeval>#f</span> <span class=selfeval>2</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>2</span> </pre> <pre class=scheme>(<span class=keyword>my-or</span> (<span class=keyword>begin</span> (<span class=variable>display</span> <span class=selfeval>&quot;doing first argument&quot;</span>) (<span class=variable>newline</span>) <span class=selfeval>#t</span>) <span class=selfeval>2</span>) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>my-or</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) <span class=keyword>`</span>(<span class=keyword>let</span> ((<span class=variable>temp</span> <span class=keyword>,</span><span class=variable>x</span>)) (<span class=keyword>if</span> <span class=variable>temp</span> <span class=variable>temp</span> <span class=keyword>,</span><span class=variable>y</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>temp</span> <span class=selfeval>3</span>) (<span class=keyword>my-or</span> <span class=selfeval>#f</span> <span class=variable>temp</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>my-or</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) <span class=keyword>`</span>(<span class=keyword>let</span> ((<span class=variable>+temp</span> <span class=keyword>,</span><span class=variable>x</span>)) (<span class=keyword>if</span> <span class=variable>+temp</span> <span class=variable>+temp</span> <span class=keyword>,</span><span class=variable>y</span>)))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>my-or</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) (<span class=keyword>let</span> ((<span class=variable>temp</span> (<span class=variable>gensym</span>))) <span class=keyword>`</span>(<span class=keyword>let</span> ((<span class=keyword>,</span><span class=variable>temp</span> <span class=keyword>,</span><span class=variable>x</span>)) (<span class=keyword>if</span> <span class=keyword>,</span><span class=variable>temp</span> <span class=keyword>,</span><span class=variable>temp</span> <span class=keyword>,</span><span class=variable>y</span>))))) </pre> <pre class=scheme>(<span class=keyword>fluid-let</span> ((<span class=variable>x</span> <span class=selfeval>9</span>) (<span class=variable>y</span> (<span class=variable>+</span> <span class=variable>y</span> <span class=selfeval>1</span>))) (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)) </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>OLD-X</span> <span class=variable>x</span>) (<span class=variable>OLD-Y</span> <span class=variable>y</span>)) (<span class=keyword>set!</span> <span class=variable>x</span> <span class=selfeval>9</span>) (<span class=keyword>set!</span> <span class=variable>y</span> (<span class=variable>+</span> <span class=variable>y</span> <span class=selfeval>1</span>)) (<span class=keyword>let</span> ((<span class=variable>RESULT</span> (<span class=keyword>begin</span> (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)))) (<span class=keyword>set!</span> <span class=variable>x</span> <span class=variable>OLD-X</span>) (<span class=keyword>set!</span> <span class=variable>y</span> <span class=variable>OLD-Y</span>) <span class=variable>RESULT</span>)) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>fluid-let</span> (<span class=keyword>lambda</span> (<span class=variable>xexe</span> . <span class=variable>body</span>) (<span class=keyword>let</span> ((<span class=variable>xx</span> (<span class=variable>map</span> <span class=variable>car</span> <span class=variable>xexe</span>)) (<span class=variable>ee</span> (<span class=variable>map</span> <span class=variable>cadr</span> <span class=variable>xexe</span>)) (<span class=variable>old-xx</span> (<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>ig</span>) (<span class=variable>gensym</span>)) <span class=variable>xexe</span>)) (<span class=variable>result</span> (<span class=variable>gensym</span>))) <span class=keyword>`</span>(<span class=keyword>let</span> <span class=keyword>,</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>old-x</span> <span class=variable>x</span>) <span class=keyword>`</span>(<span class=keyword>,</span><span class=variable>old-x</span> <span class=keyword>,</span><span class=variable>x</span>)) <span class=variable>old-xx</span> <span class=variable>xx</span>) <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>e</span>) <span class=keyword>`</span>(<span class=keyword>set!</span> <span class=keyword>,</span><span class=variable>x</span> <span class=keyword>,</span><span class=variable>e</span>)) <span class=variable>xx</span> <span class=variable>ee</span>) (<span class=keyword>let</span> ((<span class=keyword>,</span><span class=variable>result</span> (<span class=keyword>begin</span> <span class=keyword>,@</span><span class=variable>body</span>))) <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>old-x</span>) <span class=keyword>`</span>(<span class=keyword>set!</span> <span class=keyword>,</span><span class=variable>x</span> <span class=keyword>,</span><span class=variable>old-x</span>)) <span class=variable>xx</span> <span class=variable>old-xx</span>) <span class=keyword>,</span><span class=variable>result</span>))))) </pre> <pre class=scheme>(<span class=keyword>let</span> ((<span class=variable>GEN-63</span> <span class=variable>x</span>) (<span class=variable>GEN-64</span> <span class=variable>y</span>)) (<span class=keyword>set!</span> <span class=variable>x</span> <span class=selfeval>9</span>) (<span class=keyword>set!</span> <span class=variable>y</span> (<span class=variable>+</span> <span class=variable>y</span> <span class=selfeval>1</span>)) (<span class=keyword>let</span> ((<span class=variable>GEN-65</span> (<span class=keyword>begin</span> (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>y</span>)))) (<span class=keyword>set!</span> <span class=variable>x</span> <span class=variable>GEN-63</span>) (<span class=keyword>set!</span> <span class=variable>y</span> <span class=variable>GEN-64</span>) <span class=variable>GEN-65</span>)) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-11.html'>Chapter 9</a></h2> <pre class=scheme>(<span class=keyword>defstruct</span> <span class=variable>tree</span> <span class=variable>height</span> <span class=variable>girth</span> <span class=variable>age</span> <span class=variable>leaf-shape</span> <span class=variable>leaf-color</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>coconut</span> (<span class=variable>make-tree</span> <span class=keyword>'</span><span class=variable>height</span> <span class=selfeval>30</span> <span class=keyword>'</span><span class=variable>leaf-shape</span> <span class=keyword>'</span><span class=variable>frond</span> <span class=keyword>'</span><span class=variable>age</span> <span class=selfeval>5</span>)) </pre> <pre class=scheme>(<span class=variable>tree.height</span> <span class=variable>coconut</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>30</span> (<span class=variable>tree.leaf-shape</span> <span class=variable>coconut</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>frond</span> (<span class=variable>tree.girth</span> <span class=variable>coconut</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>&lt;undefined&gt;</span> </pre> <pre class=scheme>(<span class=variable>set!tree.height</span> <span class=variable>coconut</span> <span class=selfeval>40</span>) (<span class=variable>set!tree.girth</span> <span class=variable>coconut</span> <span class=selfeval>10</span>) </pre> <pre class=scheme>(<span class=variable>tree.height</span> <span class=variable>coconut</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>40</span> (<span class=variable>tree.girth</span> <span class=variable>coconut</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>10</span> </pre> <pre class=scheme>(<span class=keyword>defstruct</span> <span class=variable>tree</span> <span class=variable>height</span> <span class=variable>girth</span> <span class=variable>age</span> (<span class=variable>leaf-shape</span> <span class=keyword>'</span><span class=variable>frond</span>) (<span class=variable>leaf-color</span> <span class=keyword>'</span><span class=variable>green</span>)) (<span class=keyword>define</span> <span class=variable>palm</span> (<span class=variable>make-tree</span> <span class=keyword>'</span><span class=variable>height</span> <span class=selfeval>60</span>)) (<span class=variable>tree.height</span> <span class=variable>palm</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>60</span> (<span class=variable>tree.leaf-shape</span> <span class=variable>palm</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>frond</span> (<span class=keyword>define</span> <span class=variable>plantain</span> (<span class=variable>make-tree</span> <span class=keyword>'</span><span class=variable>height</span> <span class=selfeval>7</span> <span class=keyword>'</span><span class=variable>leaf-shape</span> <span class=keyword>'</span><span class=variable>sheet</span>)) (<span class=variable>tree.height</span> <span class=variable>plantain</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>7</span> (<span class=variable>tree.leaf-shape</span> <span class=variable>plantain</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>sheet</span> (<span class=variable>tree.leaf-color</span> <span class=variable>plantain</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=variable>green</span> </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>defstruct</span> (<span class=keyword>lambda</span> (<span class=variable>s</span> . <span class=variable>ff</span>) (<span class=keyword>let</span> ((<span class=variable>s-s</span> (<span class=variable>symbol-&gt;string</span> <span class=variable>s</span>)) (<span class=variable>n</span> (<span class=variable>length</span> <span class=variable>ff</span>))) (<span class=keyword>let*</span> ((<span class=variable>n+1</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>1</span>)) (<span class=variable>vv</span> (<span class=variable>make-vector</span> <span class=variable>n+1</span>))) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>ff</span> <span class=variable>ff</span>)) (<span class=keyword>if</span> (<span class=variable>&lt;=</span> <span class=variable>i</span> <span class=variable>n</span>) (<span class=keyword>let</span> ((<span class=variable>f</span> (<span class=variable>car</span> <span class=variable>ff</span>))) (<span class=variable>vector-set!</span> <span class=variable>vv</span> <span class=variable>i</span> (<span class=keyword>if</span> (<span class=variable>pair?</span> <span class=variable>f</span>) (<span class=variable>cadr</span> <span class=variable>f</span>) <span class=keyword>'</span>(<span class=keyword>if</span> <span class=selfeval>#f</span> <span class=selfeval>#f</span>))) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>cdr</span> <span class=variable>ff</span>))))) (<span class=keyword>let</span> ((<span class=variable>ff</span> (<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=keyword>if</span> (<span class=variable>pair?</span> <span class=variable>f</span>) (<span class=variable>car</span> <span class=variable>f</span>) <span class=variable>f</span>)) <span class=variable>ff</span>))) <span class=keyword>`</span>(<span class=keyword>begin</span> (<span class=keyword>define</span> <span class=keyword>,</span>(<span class=variable>string-&gt;symbol</span> (<span class=variable>string-append</span> <span class=selfeval>&quot;make-&quot;</span> <span class=variable>s-s</span>)) (<span class=keyword>lambda</span> <span class=variable>fvfv</span> (<span class=keyword>let</span> ((<span class=variable>st</span> (<span class=variable>make-vector</span> <span class=keyword>,</span><span class=variable>n+1</span>)) (<span class=variable>ff</span> <span class=keyword>'</span><span class=keyword>,</span><span class=variable>ff</span>)) (<span class=variable>vector-set!</span> <span class=variable>st</span> <span class=selfeval>0</span> <span class=keyword>'</span><span class=keyword>,</span><span class=variable>s</span>) <span class=keyword>,@</span>(<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>r</span> <span class=keyword>'</span>())) (<span class=keyword>if</span> (<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>n+1</span>) <span class=variable>r</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>cons</span> <span class=keyword>`</span>(<span class=variable>vector-set!</span> <span class=variable>st</span> <span class=keyword>,</span><span class=variable>i</span> <span class=keyword>,</span>(<span class=variable>vector-ref</span> <span class=variable>vv</span> <span class=variable>i</span>)) <span class=variable>r</span>)))) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>fvfv</span> <span class=variable>fvfv</span>)) (<span class=keyword>if</span> (<span class=variable>not</span> (<span class=variable>null?</span> <span class=variable>fvfv</span>)) (<span class=keyword>begin</span> (<span class=variable>vector-set!</span> <span class=variable>st</span> (<span class=variable>+</span> (<span class=variable>list-position</span> (<span class=variable>car</span> <span class=variable>fvfv</span>) <span class=variable>ff</span>) <span class=selfeval>1</span>) (<span class=variable>cadr</span> <span class=variable>fvfv</span>)) (<span class=variable>loop</span> (<span class=variable>cddr</span> <span class=variable>fvfv</span>))))) <span class=variable>st</span>))) <span class=keyword>,@</span>(<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>procs</span> <span class=keyword>'</span>())) (<span class=keyword>if</span> (<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>n+1</span>) <span class=variable>procs</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=keyword>let</span> ((<span class=variable>f</span> (<span class=variable>symbol-&gt;string</span> (<span class=variable>list-ref</span> <span class=variable>ff</span> (<span class=variable>-</span> <span class=variable>i</span> <span class=selfeval>1</span>))))) (<span class=variable>cons</span> <span class=keyword>`</span>(<span class=keyword>define</span> <span class=keyword>,</span>(<span class=variable>string-&gt;symbol</span> (<span class=variable>string-append</span> <span class=variable>s-s</span> <span class=selfeval>&quot;.&quot;</span> <span class=variable>f</span>)) (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>vector-ref</span> <span class=variable>x</span> <span class=keyword>,</span><span class=variable>i</span>))) (<span class=variable>cons</span> <span class=keyword>`</span>(<span class=keyword>define</span> <span class=keyword>,</span>(<span class=variable>string-&gt;symbol</span> (<span class=variable>string-append</span> <span class=selfeval>&quot;set!&quot;</span> <span class=variable>s-s</span> <span class=selfeval>&quot;.&quot;</span> <span class=variable>f</span>)) (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>v</span>) (<span class=variable>vector-set!</span> <span class=variable>x</span> <span class=keyword>,</span><span class=variable>i</span> <span class=variable>v</span>))) <span class=variable>procs</span>)))))) (<span class=keyword>define</span> <span class=keyword>,</span>(<span class=variable>string-&gt;symbol</span> (<span class=variable>string-append</span> <span class=variable>s-s</span> <span class=selfeval>&quot;?&quot;</span>)) (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=keyword>and</span> (<span class=variable>vector?</span> <span class=variable>x</span>) (<span class=variable>eqv?</span> (<span class=variable>vector-ref</span> <span class=variable>x</span> <span class=selfeval>0</span>) <span class=keyword>'</span><span class=keyword>,</span><span class=variable>s</span>)))))))))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-12.html'>Chapter 10</a></h2> <pre class=scheme>((<span class=variable>a</span> . <span class=selfeval>1</span>) (<span class=variable>b</span> . <span class=selfeval>2</span>) (<span class=variable>c</span> . <span class=selfeval>3</span>)) </pre> <pre class=scheme>(<span class=keyword>defstruct</span> <span class=variable>table</span> (<span class=variable>equ</span> <span class=variable>eqv?</span>) (<span class=variable>alist</span> <span class=keyword>'</span>())) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>table-get</span> (<span class=keyword>lambda</span> (<span class=variable>tbl</span> <span class=variable>k</span> . <span class=variable>d</span>) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>lassoc</span> <span class=variable>k</span> (<span class=variable>table.alist</span> <span class=variable>tbl</span>) (<span class=variable>table.equ</span> <span class=variable>tbl</span>)))) (<span class=keyword>cond</span> (<span class=variable>c</span> (<span class=variable>cdr</span> <span class=variable>c</span>)) ((<span class=variable>pair?</span> <span class=variable>d</span>) (<span class=variable>car</span> <span class=variable>d</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>lassoc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span> <span class=variable>al</span> <span class=variable>equ?</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>al</span> <span class=variable>al</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>al</span>) <span class=selfeval>#f</span> (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>car</span> <span class=variable>al</span>))) (<span class=keyword>if</span> (<span class=variable>equ?</span> (<span class=variable>car</span> <span class=variable>c</span>) <span class=variable>k</span>) <span class=variable>c</span> (<span class=variable>loop</span> (<span class=variable>cdr</span> <span class=variable>al</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>table-put!</span> (<span class=keyword>lambda</span> (<span class=variable>tbl</span> <span class=variable>k</span> <span class=variable>v</span>) (<span class=keyword>let</span> ((<span class=variable>al</span> (<span class=variable>table.alist</span> <span class=variable>tbl</span>))) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>lassoc</span> <span class=variable>k</span> <span class=variable>al</span> (<span class=variable>table.equ</span> <span class=variable>tbl</span>)))) (<span class=keyword>if</span> <span class=variable>c</span> (<span class=variable>set-cdr!</span> <span class=variable>c</span> <span class=variable>v</span>) (<span class=variable>set!table.alist</span> <span class=variable>tbl</span> (<span class=variable>cons</span> (<span class=variable>cons</span> <span class=variable>k</span> <span class=variable>v</span>) <span class=variable>al</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>table-for-each</span> (<span class=keyword>lambda</span> (<span class=variable>tbl</span> <span class=variable>p</span>) (<span class=variable>for-each</span> (<span class=keyword>lambda</span> (<span class=variable>c</span>) (<span class=variable>p</span> (<span class=variable>car</span> <span class=variable>c</span>) (<span class=variable>cdr</span> <span class=variable>c</span>))) (<span class=variable>table.alist</span> <span class=variable>tbl</span>)))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-13.html'>Chapter 11</a></h2> <pre class=scheme>(<span class=variable>file-or-directory-modify-seconds</span> <span class=selfeval>&quot;hello.scm&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>893189629</span> </pre> <pre class=scheme>(<span class=variable>system</span> <span class=selfeval>&quot;ls&quot;</span>) <span class=comment>;lists current directory</span> (<span class=keyword>define</span> <span class=variable>fname</span> <span class=selfeval>&quot;spot&quot;</span>) (<span class=variable>system</span> (<span class=variable>string-append</span> <span class=selfeval>&quot;test -f &quot;</span> <span class=variable>fname</span>)) <span class=comment>;tests if file `spot' exists</span> (<span class=variable>system</span> (<span class=variable>string-append</span> <span class=selfeval>&quot;rm -f &quot;</span> <span class=variable>fname</span>)) <span class=comment>;removes `spot'</span> </pre> <pre class=scheme>(<span class=variable>file-exists?</span> <span class=variable>fname</span>) (<span class=variable>delete-file</span> <span class=variable>fname</span>) </pre> <pre class=scheme>(<span class=variable>getenv</span> <span class=selfeval>&quot;HOME&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;/home/dorai&quot;</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;SHELL&quot;</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>&quot;/bin/bash&quot;</span> </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-14.html'>Chapter 12</a></h2> <pre class=scheme>(<span class=keyword>defstruct</span> <span class=variable>standard-class</span> <span class=variable>slots</span> <span class=variable>superclass</span> <span class=variable>method-names</span> <span class=variable>method-vector</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>trivial-bike-class</span> (<span class=variable>make-standard-class</span> <span class=keyword>'</span><span class=variable>superclass</span> <span class=selfeval>#t</span> <span class=keyword>'</span><span class=variable>slots</span> <span class=keyword>'</span>(<span class=variable>frame</span> <span class=variable>parts</span> <span class=variable>size</span>) <span class=keyword>'</span><span class=variable>method-names</span> <span class=keyword>'</span>() <span class=keyword>'</span><span class=variable>method-vector</span> <span class=selfeval>#</span>())) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=variable>create-class</span> (<span class=keyword>lambda</span> (<span class=variable>superclass</span> <span class=variable>slots</span> . <span class=variable>methods</span>) <span class=keyword>`</span>(<span class=variable>create-class-proc</span> <span class=keyword>,</span><span class=variable>superclass</span> (<span class=variable>list</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>slot</span>) <span class=keyword>`</span><span class=keyword>'</span><span class=keyword>,</span><span class=variable>slot</span>) <span class=variable>slots</span>)) (<span class=variable>list</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>method</span>) <span class=keyword>`</span><span class=keyword>'</span><span class=keyword>,</span>(<span class=variable>car</span> <span class=variable>method</span>)) <span class=variable>methods</span>)) (<span class=variable>vector</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>method</span>) <span class=keyword>`</span><span class=keyword>,</span>(<span class=variable>cadr</span> <span class=variable>method</span>)) <span class=variable>methods</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>make-instance</span> (<span class=keyword>lambda</span> (<span class=variable>class</span> . <span class=variable>slot-value-twosomes</span>) <span class=comment>;Find `n', the number of slots in `class'.</span> <span class=comment>;Create an instance vector of length `n + 1',</span> <span class=comment>;because we need one extra element in the instance</span> <span class=comment>;to contain the class.</span> (<span class=keyword>let*</span> ((<span class=variable>slotlist</span> (<span class=variable>standard-class.slots</span> <span class=variable>class</span>)) (<span class=variable>n</span> (<span class=variable>length</span> <span class=variable>slotlist</span>)) (<span class=variable>instance</span> (<span class=variable>make-vector</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>1</span>)))) (<span class=variable>vector-set!</span> <span class=variable>instance</span> <span class=selfeval>0</span> <span class=variable>class</span>) <span class=comment>;Fill each of the slots in the instance</span> <span class=comment>;with the value as specified in the call to</span> <span class=comment>;`make-instance'.</span> (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>slot-value-twosomes</span> <span class=variable>slot-value-twosomes</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>slot-value-twosomes</span>) <span class=variable>instance</span> (<span class=keyword>let</span> ((<span class=variable>k</span> (<span class=variable>list-position</span> (<span class=variable>car</span> <span class=variable>slot-value-twosomes</span>) <span class=variable>slotlist</span>))) (<span class=variable>vector-set!</span> <span class=variable>instance</span> (<span class=variable>+</span> <span class=variable>k</span> <span class=selfeval>1</span>) (<span class=variable>cadr</span> <span class=variable>slot-value-twosomes</span>)) (<span class=variable>loop</span> (<span class=variable>cddr</span> <span class=variable>slot-value-twosomes</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>my-bike</span> (<span class=variable>make-instance</span> <span class=variable>trivial-bike-class</span> <span class=keyword>'</span><span class=variable>frame</span> <span class=keyword>'</span><span class=variable>cromoly</span> <span class=keyword>'</span><span class=variable>size</span> <span class=keyword>'</span><span class=selfeval>18.5</span> <span class=keyword>'</span><span class=variable>parts</span> <span class=keyword>'</span><span class=variable>alivio</span>)) </pre> <pre class=scheme><span class=selfeval>#</span>(<span class=variable>&lt;trivial-bike-class&gt;</span> <span class=variable>cromoly</span> <span class=selfeval>18.5</span> <span class=variable>alivio</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>class-of</span> (<span class=keyword>lambda</span> (<span class=variable>instance</span>) (<span class=variable>vector-ref</span> <span class=variable>instance</span> <span class=selfeval>0</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>class-of</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=keyword>if</span> (<span class=variable>vector?</span> <span class=variable>x</span>) (<span class=keyword>let</span> ((<span class=variable>n</span> (<span class=variable>vector-length</span> <span class=variable>x</span>))) (<span class=keyword>if</span> (<span class=variable>&gt;=</span> <span class=variable>n</span> <span class=selfeval>1</span>) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>vector-ref</span> <span class=variable>x</span> <span class=selfeval>0</span>))) (<span class=keyword>if</span> (<span class=variable>standard-class?</span> <span class=variable>c</span>) <span class=variable>c</span> <span class=selfeval>#t</span>)) <span class=selfeval>#t</span>)) <span class=selfeval>#t</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>slot-value</span> (<span class=keyword>lambda</span> (<span class=variable>instance</span> <span class=variable>slot</span>) (<span class=keyword>let*</span> ((<span class=variable>class</span> (<span class=variable>class-of</span> <span class=variable>instance</span>)) (<span class=variable>slot-index</span> (<span class=variable>list-position</span> <span class=variable>slot</span> (<span class=variable>standard-class.slots</span> <span class=variable>class</span>)))) (<span class=variable>vector-ref</span> <span class=variable>instance</span> (<span class=variable>+</span> <span class=variable>slot-index</span> <span class=selfeval>1</span>))))) (<span class=keyword>define</span> <span class=variable>set!slot-value</span> (<span class=keyword>lambda</span> (<span class=variable>instance</span> <span class=variable>slot</span> <span class=variable>new-val</span>) (<span class=keyword>let*</span> ((<span class=variable>class</span> (<span class=variable>class-of</span> <span class=variable>instance</span>)) (<span class=variable>slot-index</span> (<span class=variable>list-position</span> <span class=variable>slot</span> (<span class=variable>standard-class.slots</span> <span class=variable>class</span>)))) (<span class=variable>vector-set!</span> <span class=variable>instance</span> (<span class=variable>+</span> <span class=variable>slot-index</span> <span class=selfeval>1</span>) <span class=variable>new-val</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>create-class-proc</span> (<span class=keyword>lambda</span> (<span class=variable>superclass</span> <span class=variable>slots</span> <span class=variable>method-names</span> <span class=variable>method-vector</span>) (<span class=variable>make-standard-class</span> <span class=keyword>'</span><span class=variable>superclass</span> <span class=variable>superclass</span> <span class=keyword>'</span><span class=variable>slots</span> (<span class=keyword>let</span> ((<span class=variable>superclass-slots</span> (<span class=keyword>if</span> (<span class=variable>not</span> (<span class=variable>eqv?</span> <span class=variable>superclass</span> <span class=selfeval>#t</span>)) (<span class=variable>standard-class.slots</span> <span class=variable>superclass</span>) <span class=keyword>'</span>()))) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>superclass-slots</span>) <span class=variable>slots</span> (<span class=variable>delete-duplicates</span> (<span class=variable>append</span> <span class=variable>slots</span> <span class=variable>superclass-slots</span>)))) <span class=keyword>'</span><span class=variable>method-names</span> <span class=variable>method-names</span> <span class=keyword>'</span><span class=variable>method-vector</span> <span class=variable>method-vector</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>delete-duplicates</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=variable>s</span> (<span class=keyword>let</span> ((<span class=variable>a</span> (<span class=variable>car</span> <span class=variable>s</span>)) (<span class=variable>d</span> (<span class=variable>cdr</span> <span class=variable>s</span>))) (<span class=keyword>if</span> (<span class=variable>memv</span> <span class=variable>a</span> <span class=variable>d</span>) (<span class=variable>delete-duplicates</span> <span class=variable>d</span>) (<span class=variable>cons</span> <span class=variable>a</span> (<span class=variable>delete-duplicates</span> <span class=variable>d</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>send</span> (<span class=keyword>lambda</span> (<span class=variable>method</span> <span class=variable>instance</span> . <span class=variable>args</span>) (<span class=keyword>let</span> ((<span class=keyword>proc</span> (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>class</span> (<span class=variable>class-of</span> <span class=variable>instance</span>))) (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>class</span> <span class=selfeval>#t</span>) (<span class=variable>error</span> <span class=keyword>'</span><span class=variable>send</span>) (<span class=keyword>let</span> ((<span class=variable>k</span> (<span class=variable>list-position</span> <span class=variable>method</span> (<span class=variable>standard-class.method-names</span> <span class=variable>class</span>)))) (<span class=keyword>if</span> <span class=variable>k</span> (<span class=variable>vector-ref</span> (<span class=variable>standard-class.method-vector</span> <span class=variable>class</span>) <span class=variable>k</span>) (<span class=variable>loop</span> (<span class=variable>standard-class.superclass</span> <span class=variable>class</span>)))))))) (<span class=variable>apply</span> <span class=keyword>proc</span> <span class=variable>instance</span> <span class=variable>args</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>bike-class</span> (<span class=variable>create-class</span> <span class=selfeval>#t</span> (<span class=variable>frame</span> <span class=variable>size</span> <span class=variable>parts</span> <span class=variable>chain</span> <span class=variable>tires</span>) (<span class=variable>check-fit</span> (<span class=keyword>lambda</span> (<span class=variable>me</span> <span class=variable>inseam</span>) (<span class=keyword>let</span> ((<span class=variable>bike-size</span> (<span class=variable>slot-value</span> <span class=variable>me</span> <span class=keyword>'</span><span class=variable>size</span>)) (<span class=variable>ideal-size</span> (<span class=variable>*</span> <span class=variable>inseam</span> <span class=selfeval>3/5</span>))) (<span class=keyword>let</span> ((<span class=variable>diff</span> (<span class=variable>-</span> <span class=variable>bike-size</span> <span class=variable>ideal-size</span>))) (<span class=keyword>cond</span> ((<span class=variable>&lt;=</span> <span class=selfeval>-1</span> <span class=variable>diff</span> <span class=selfeval>1</span>) <span class=keyword>'</span><span class=variable>perfect-fit</span>) ((<span class=variable>&lt;=</span> <span class=selfeval>-2</span> <span class=variable>diff</span> <span class=selfeval>2</span>) <span class=keyword>'</span><span class=variable>fits-well</span>) ((<span class=variable>&lt;</span> <span class=variable>diff</span> <span class=selfeval>-2</span>) <span class=keyword>'</span><span class=variable>too-small</span>) ((<span class=variable>&gt;</span> <span class=variable>diff</span> <span class=selfeval>2</span>) <span class=keyword>'</span><span class=variable>too-big</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>my-bike</span> (<span class=variable>make-instance</span> <span class=variable>bike-class</span> <span class=keyword>'</span><span class=variable>frame</span> <span class=keyword>'</span><span class=variable>titanium</span> <span class=comment>; I wish</span> <span class=keyword>'</span><span class=variable>size</span> <span class=selfeval>21</span> <span class=keyword>'</span><span class=variable>parts</span> <span class=keyword>'</span><span class=variable>ultegra</span> <span class=keyword>'</span><span class=variable>chain</span> <span class=keyword>'</span><span class=variable>sachs</span> <span class=keyword>'</span><span class=variable>tires</span> <span class=keyword>'</span><span class=variable>continental</span>)) </pre> <pre class=scheme>(<span class=variable>send</span> <span class=keyword>'</span><span class=variable>check-fit</span> <span class=variable>my-bike</span> <span class=selfeval>32</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>mtn-bike-class</span> (<span class=variable>create-class</span> <span class=variable>bike-class</span> (<span class=variable>suspension</span>) (<span class=variable>check-fit</span> (<span class=keyword>lambda</span> (<span class=variable>me</span> <span class=variable>inseam</span>) (<span class=keyword>let</span> ((<span class=variable>bike-size</span> (<span class=variable>slot-value</span> <span class=variable>me</span> <span class=keyword>'</span><span class=variable>size</span>)) (<span class=variable>ideal-size</span> (<span class=variable>-</span> (<span class=variable>*</span> <span class=variable>inseam</span> <span class=selfeval>3/5</span>) <span class=selfeval>2</span>))) (<span class=keyword>let</span> ((<span class=variable>diff</span> (<span class=variable>-</span> <span class=variable>bike-size</span> <span class=variable>ideal-size</span>))) (<span class=keyword>cond</span> ((<span class=variable>&lt;=</span> <span class=selfeval>-2</span> <span class=variable>diff</span> <span class=selfeval>2</span>) <span class=keyword>'</span><span class=variable>perfect-fit</span>) ((<span class=variable>&lt;=</span> <span class=selfeval>-4</span> <span class=variable>diff</span> <span class=selfeval>4</span>) <span class=keyword>'</span><span class=variable>fits-well</span>) ((<span class=variable>&lt;</span> <span class=variable>diff</span> <span class=selfeval>-4</span>) <span class=keyword>'</span><span class=variable>too-small</span>) ((<span class=variable>&gt;</span> <span class=variable>diff</span> <span class=selfeval>4</span>) <span class=keyword>'</span><span class=variable>too-big</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>standard-class</span> (<span class=variable>vector</span> <span class=keyword>'</span><span class=variable>value-of-standard-class-goes-here</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>slots</span> <span class=keyword>'</span><span class=variable>superclass</span> <span class=keyword>'</span><span class=variable>method-names</span> <span class=keyword>'</span><span class=variable>method-vector</span>) <span class=selfeval>#t</span> <span class=keyword>'</span>(<span class=variable>make-instance</span>) (<span class=variable>vector</span> <span class=variable>make-instance</span>))) </pre> <pre class=scheme>(<span class=variable>vector-set!</span> <span class=variable>standard-class</span> <span class=selfeval>0</span> <span class=variable>standard-class</span>) </pre> <pre class=scheme>(<span class=variable>standard-class?</span> <span class=variable>x</span>) (<span class=variable>standard-class.slots</span> <span class=variable>c</span>) (<span class=variable>standard-class.superclass</span> <span class=variable>c</span>) (<span class=variable>standard-class.method-names</span> <span class=variable>c</span>) (<span class=variable>standard-class.method-vector</span> <span class=variable>c</span>) (<span class=variable>make-standard-class</span> ...) </pre> <pre class=scheme>(<span class=keyword>and</span> (<span class=variable>vector?</span> <span class=variable>x</span>) (<span class=variable>eqv?</span> (<span class=variable>vector-ref</span> <span class=variable>x</span> <span class=selfeval>0</span>) <span class=variable>standard-class</span>)) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>1</span>) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>2</span>) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>3</span>) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>4</span>) (<span class=variable>send</span> <span class=keyword>'</span><span class=variable>make-instance</span> <span class=variable>standard-class</span> ...) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>standard-class</span> (<span class=variable>vector</span> <span class=keyword>'</span><span class=variable>value-of-standard-class-goes-here</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>slots</span> <span class=keyword>'</span><span class=variable>class-precedence-list</span> <span class=keyword>'</span><span class=variable>method-names</span> <span class=keyword>'</span><span class=variable>method-vector</span>) <span class=keyword>'</span>() <span class=keyword>'</span>(<span class=variable>make-instance</span>) (<span class=variable>vector</span> <span class=variable>make-instance</span>))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=variable>create-class</span> (<span class=keyword>lambda</span> (<span class=variable>direct-superclasses</span> <span class=variable>slots</span> . <span class=variable>methods</span>) <span class=keyword>`</span>(<span class=variable>create-class-proc</span> (<span class=variable>list</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>su</span>) <span class=keyword>`</span><span class=keyword>,</span><span class=variable>su</span>) <span class=variable>direct-superclasses</span>)) (<span class=variable>list</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>slot</span>) <span class=keyword>`</span><span class=keyword>'</span><span class=keyword>,</span><span class=variable>slot</span>) <span class=variable>slots</span>)) (<span class=variable>list</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>method</span>) <span class=keyword>`</span><span class=keyword>'</span><span class=keyword>,</span>(<span class=variable>car</span> <span class=variable>method</span>)) <span class=variable>methods</span>)) (<span class=variable>vector</span> <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>method</span>) <span class=keyword>`</span><span class=keyword>,</span>(<span class=variable>cadr</span> <span class=variable>method</span>)) <span class=variable>methods</span>)) ))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>create-class-proc</span> (<span class=keyword>lambda</span> (<span class=variable>direct-superclasses</span> <span class=variable>slots</span> <span class=variable>method-names</span> <span class=variable>method-vector</span>) (<span class=keyword>let</span> ((<span class=variable>class-precedence-list</span> (<span class=variable>delete-duplicates</span> (<span class=variable>append-map</span> (<span class=keyword>lambda</span> (<span class=variable>c</span>) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>2</span>)) <span class=variable>direct-superclasses</span>)))) (<span class=variable>send</span> <span class=keyword>'</span><span class=variable>make-instance</span> <span class=variable>standard-class</span> <span class=keyword>'</span><span class=variable>class-precedence-list</span> <span class=variable>class-precedence-list</span> <span class=keyword>'</span><span class=variable>slots</span> (<span class=variable>delete-duplicates</span> (<span class=variable>append</span> <span class=variable>slots</span> (<span class=variable>append-map</span> (<span class=keyword>lambda</span> (<span class=variable>c</span>) (<span class=variable>vector-ref</span> <span class=variable>c</span> <span class=selfeval>1</span>)) <span class=variable>class-precedence-list</span>))) <span class=keyword>'</span><span class=variable>method-names</span> <span class=variable>method-names</span> <span class=keyword>'</span><span class=variable>method-vector</span> <span class=variable>method-vector</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>append-map</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>s</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>s</span> <span class=variable>s</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=keyword>'</span>() (<span class=variable>append</span> (<span class=variable>f</span> (<span class=variable>car</span> <span class=variable>s</span>)) (<span class=variable>loop</span> (<span class=variable>cdr</span> <span class=variable>s</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>send</span> (<span class=keyword>lambda</span> (<span class=variable>method-name</span> <span class=variable>instance</span> . <span class=variable>args</span>) (<span class=keyword>let</span> ((<span class=keyword>proc</span> (<span class=keyword>let</span> ((<span class=variable>class</span> (<span class=variable>class-of</span> <span class=variable>instance</span>))) (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>class</span> <span class=selfeval>#t</span>) (<span class=variable>error</span> <span class=keyword>'</span><span class=variable>send</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>class</span> <span class=variable>class</span>) (<span class=variable>superclasses</span> (<span class=variable>vector-ref</span> <span class=variable>class</span> <span class=selfeval>2</span>))) (<span class=keyword>let</span> ((<span class=variable>k</span> (<span class=variable>list-position</span> <span class=variable>method-name</span> (<span class=variable>vector-ref</span> <span class=variable>class</span> <span class=selfeval>3</span>)))) (<span class=keyword>cond</span> (<span class=variable>k</span> (<span class=variable>vector-ref</span> (<span class=variable>vector-ref</span> <span class=variable>class</span> <span class=selfeval>4</span>) <span class=variable>k</span>)) ((<span class=variable>null?</span> <span class=variable>superclasses</span>) (<span class=variable>error</span> <span class=keyword>'</span><span class=variable>send</span>)) (<span class=keyword>else</span> (<span class=variable>loop</span> (<span class=variable>car</span> <span class=variable>superclasses</span>) (<span class=variable>cdr</span> <span class=variable>superclasses</span>)))) )))))) (<span class=variable>apply</span> <span class=keyword>proc</span> <span class=variable>instance</span> <span class=variable>args</span>)))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-15.html'>Chapter 13</a></h2> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=variable>+</span> <span class=selfeval>2</span> (<span class=variable>k</span> <span class=selfeval>3</span>))))) </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> []) </pre> <pre class=scheme>(<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=variable>+</span> <span class=selfeval>2</span> (<span class=variable>k</span> <span class=selfeval>3</span>))) </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> []) </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> <span class=selfeval>3</span>) </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>1</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=variable>+</span> <span class=selfeval>2</span> (<span class=variable>k</span> <span class=selfeval>3</span>))))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>4</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>r</span> <span class=selfeval>#f</span>) (<span class=variable>+</span> <span class=selfeval>1</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=variable>r</span> <span class=variable>k</span>) (<span class=variable>+</span> <span class=selfeval>2</span> (<span class=variable>k</span> <span class=selfeval>3</span>))))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>4</span> </pre> <pre class=scheme>(<span class=variable>r</span> <span class=selfeval>5</span>) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> </pre> <pre class=scheme>(<span class=variable>+</span> <span class=selfeval>3</span> (<span class=variable>r</span> <span class=selfeval>5</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>6</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>list-product</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=keyword>let</span> <span class=variable>recur</span> ((<span class=variable>s</span> <span class=variable>s</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=selfeval>1</span> (<span class=variable>*</span> (<span class=variable>car</span> <span class=variable>s</span>) (<span class=variable>recur</span> (<span class=variable>cdr</span> <span class=variable>s</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>list-product</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>exit</span>) (<span class=keyword>let</span> <span class=variable>recur</span> ((<span class=variable>s</span> <span class=variable>s</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=selfeval>1</span> (<span class=keyword>if</span> (<span class=variable>=</span> (<span class=variable>car</span> <span class=variable>s</span>) <span class=selfeval>0</span>) (<span class=variable>exit</span> <span class=selfeval>0</span>) (<span class=variable>*</span> (<span class=variable>car</span> <span class=variable>s</span>) (<span class=variable>recur</span> (<span class=variable>cdr</span> <span class=variable>s</span>)))))))))) </pre> <pre class=scheme>(<span class=variable>same-fringe?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> (<span class=selfeval>2</span> <span class=selfeval>3</span>)) <span class=keyword>'</span>((<span class=selfeval>1</span> <span class=selfeval>2</span>) <span class=selfeval>3</span>)) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#t</span> (<span class=variable>same-fringe?</span> <span class=keyword>'</span>(<span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span>) <span class=keyword>'</span>(<span class=selfeval>1</span> (<span class=selfeval>3</span> <span class=selfeval>2</span>))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>#f</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>same-fringe?</span> (<span class=keyword>lambda</span> (<span class=variable>tree1</span> <span class=variable>tree2</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>ftree1</span> (<span class=variable>flatten</span> <span class=variable>tree1</span>)) (<span class=variable>ftree2</span> (<span class=variable>flatten</span> <span class=variable>tree2</span>))) (<span class=keyword>cond</span> ((<span class=keyword>and</span> (<span class=variable>null?</span> <span class=variable>ftree1</span>) (<span class=variable>null?</span> <span class=variable>ftree2</span>)) <span class=selfeval>#t</span>) ((<span class=keyword>or</span> (<span class=variable>null?</span> <span class=variable>ftree1</span>) (<span class=variable>null?</span> <span class=variable>ftree2</span>)) <span class=selfeval>#f</span>) ((<span class=variable>eqv?</span> (<span class=variable>car</span> <span class=variable>ftree1</span>) (<span class=variable>car</span> <span class=variable>ftree2</span>)) (<span class=variable>loop</span> (<span class=variable>cdr</span> <span class=variable>ftree1</span>) (<span class=variable>cdr</span> <span class=variable>ftree2</span>))) (<span class=keyword>else</span> <span class=selfeval>#f</span>))))) (<span class=keyword>define</span> <span class=variable>flatten</span> (<span class=keyword>lambda</span> (<span class=variable>tree</span>) (<span class=keyword>cond</span> ((<span class=variable>null?</span> <span class=variable>tree</span>) <span class=keyword>'</span>()) ((<span class=variable>pair?</span> (<span class=variable>car</span> <span class=variable>tree</span>)) (<span class=variable>append</span> (<span class=variable>flatten</span> (<span class=variable>car</span> <span class=variable>tree</span>)) (<span class=variable>flatten</span> (<span class=variable>cdr</span> <span class=variable>tree</span>)))) (<span class=keyword>else</span> (<span class=variable>cons</span> (<span class=variable>car</span> <span class=variable>tree</span>) (<span class=variable>flatten</span> (<span class=variable>cdr</span> <span class=variable>tree</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>tree-&gt;generator</span> (<span class=keyword>lambda</span> (<span class=variable>tree</span>) (<span class=keyword>let</span> ((<span class=variable>caller</span> <span class=keyword>'</span><span class=variable>*</span>)) (<span class=keyword>letrec</span> ((<span class=variable>generate-leaves</span> (<span class=keyword>lambda</span> () (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>tree</span> <span class=variable>tree</span>)) (<span class=keyword>cond</span> ((<span class=variable>null?</span> <span class=variable>tree</span>) <span class=keyword>'</span><span class=variable>skip</span>) ((<span class=variable>pair?</span> <span class=variable>tree</span>) (<span class=variable>loop</span> (<span class=variable>car</span> <span class=variable>tree</span>)) (<span class=variable>loop</span> (<span class=variable>cdr</span> <span class=variable>tree</span>))) (<span class=keyword>else</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>rest-of-tree</span>) (<span class=keyword>set!</span> <span class=variable>generate-leaves</span> (<span class=keyword>lambda</span> () (<span class=variable>rest-of-tree</span> <span class=keyword>'</span><span class=variable>resume</span>))) (<span class=variable>caller</span> <span class=variable>tree</span>)))))) (<span class=variable>caller</span> <span class=keyword>'</span>())))) (<span class=keyword>lambda</span> () (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=variable>caller</span> <span class=variable>k</span>) (<span class=variable>generate-leaves</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>same-fringe?</span> (<span class=keyword>lambda</span> (<span class=variable>tree1</span> <span class=variable>tree2</span>) (<span class=keyword>let</span> ((<span class=variable>gen1</span> (<span class=variable>tree-&gt;generator</span> <span class=variable>tree1</span>)) (<span class=variable>gen2</span> (<span class=variable>tree-&gt;generator</span> <span class=variable>tree2</span>))) (<span class=keyword>let</span> <span class=variable>loop</span> () (<span class=keyword>let</span> ((<span class=variable>leaf1</span> (<span class=variable>gen1</span>)) (<span class=variable>leaf2</span> (<span class=variable>gen2</span>))) (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>leaf1</span> <span class=variable>leaf2</span>) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>leaf1</span>) <span class=selfeval>#t</span> (<span class=variable>loop</span>)) <span class=selfeval>#f</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>coroutine</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> . <span class=variable>body</span>) <span class=keyword>`</span>(<span class=keyword>letrec</span> ((<span class=variable>+local-control-state</span> (<span class=keyword>lambda</span> (<span class=keyword>,</span><span class=variable>x</span>) <span class=keyword>,@</span><span class=variable>body</span>)) (<span class=variable>resume</span> (<span class=keyword>lambda</span> (<span class=variable>c</span> <span class=variable>v</span>) (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=variable>+local-control-state</span> <span class=variable>k</span>) (<span class=variable>c</span> <span class=variable>v</span>)))))) (<span class=keyword>lambda</span> (<span class=variable>v</span>) (<span class=variable>+local-control-state</span> <span class=variable>v</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>make-matcher-coroutine</span> (<span class=keyword>lambda</span> (<span class=variable>tree-cor-1</span> <span class=variable>tree-cor-2</span>) (<span class=keyword>coroutine</span> <span class=variable>dont-need-an-init-arg</span> (<span class=keyword>let</span> <span class=variable>loop</span> () (<span class=keyword>let</span> ((<span class=variable>leaf1</span> (<span class=variable>resume</span> <span class=variable>tree-cor-1</span> <span class=keyword>'</span><span class=variable>get-a-leaf</span>)) (<span class=variable>leaf2</span> (<span class=variable>resume</span> <span class=variable>tree-cor-2</span> <span class=keyword>'</span><span class=variable>get-a-leaf</span>))) (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>leaf1</span> <span class=variable>leaf2</span>) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>leaf1</span>) <span class=selfeval>#t</span> (<span class=variable>loop</span>)) <span class=selfeval>#f</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>make-leaf-gen-coroutine</span> (<span class=keyword>lambda</span> (<span class=variable>tree</span> <span class=variable>matcher-cor</span>) (<span class=keyword>coroutine</span> <span class=variable>dont-need-an-init-arg</span> (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>tree</span> <span class=variable>tree</span>)) (<span class=keyword>cond</span> ((<span class=variable>null?</span> <span class=variable>tree</span>) <span class=keyword>'</span><span class=variable>skip</span>) ((<span class=variable>pair?</span> <span class=variable>tree</span>) (<span class=variable>loop</span> (<span class=variable>car</span> <span class=variable>tree</span>)) (<span class=variable>loop</span> (<span class=variable>cdr</span> <span class=variable>tree</span>))) (<span class=keyword>else</span> (<span class=variable>resume</span> <span class=variable>matcher-cor</span> <span class=variable>tree</span>)))) (<span class=variable>resume</span> <span class=variable>matcher-cor</span> <span class=keyword>'</span>())))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>same-fringe?</span> (<span class=keyword>lambda</span> (<span class=variable>tree1</span> <span class=variable>tree2</span>) (<span class=keyword>letrec</span> ((<span class=variable>tree-cor-1</span> (<span class=variable>make-leaf-gen-coroutine</span> <span class=variable>tree1</span> <span class=variable>matcher-cor</span>)) (<span class=variable>tree-cor-2</span> (<span class=variable>make-leaf-gen-coroutine</span> <span class=variable>tree2</span> <span class=variable>matcher-cor</span>)) (<span class=variable>matcher-cor</span> (<span class=variable>make-matcher-coroutine</span> <span class=variable>tree-cor-1</span> <span class=variable>tree-cor-2</span>))) (<span class=variable>matcher-cor</span> <span class=keyword>'</span><span class=variable>start-ball-rolling</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>same-fringe?</span> (<span class=keyword>lambda</span> (<span class=variable>tree1</span> <span class=variable>tree2</span>) (<span class=keyword>letrec</span> ((<span class=variable>tree-cor-1</span> (<span class=variable>make-leaf-gen-coroutine</span> <span class=variable>tree1</span> (<span class=keyword>lambda</span> (<span class=variable>v</span>) (<span class=variable>matcher-cor</span> <span class=variable>v</span>)))) (<span class=variable>tree-cor-2</span> (<span class=variable>make-leaf-gen-coroutine</span> <span class=variable>tree2</span> (<span class=keyword>lambda</span> (<span class=variable>v</span>) (<span class=variable>matcher-cor</span> <span class=variable>v</span>)))) (<span class=variable>matcher-cor</span> (<span class=variable>make-matcher-coroutine</span> (<span class=keyword>lambda</span> (<span class=variable>v</span>) (<span class=variable>tree-cor-1</span> <span class=variable>v</span>)) (<span class=keyword>lambda</span> (<span class=variable>v</span>) (<span class=variable>tree-cor-2</span> <span class=variable>v</span>))))) (<span class=variable>matcher-cor</span> <span class=keyword>'</span><span class=variable>start-ball-rolling</span>)))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-16.html'>Chapter 14</a></h2> <pre class=scheme>(<span class=keyword>amb</span> <span class=selfeval>1</span> <span class=selfeval>2</span>) </pre> <pre class=scheme>(<span class=keyword>amb</span>) <font color="red"><tt><strong><i>--&gt;ERROR!!!</i></strong></tt></font> <span class=keyword>amb</span> <span class=variable>tree</span> <span class=variable>exhausted</span> </pre> <pre class=scheme>(<span class=keyword>amb</span> <span class=selfeval>1</span> (<span class=keyword>amb</span>)) </pre> <pre class=scheme>(<span class=keyword>amb</span> (<span class=keyword>amb</span>) <span class=selfeval>1</span>) </pre> <pre class=scheme>(<span class=keyword>amb</span> <span class=selfeval>#f</span> <span class=selfeval>#t</span>) </pre> <pre class=scheme>(<span class=keyword>if</span> (<span class=keyword>amb</span> <span class=selfeval>#f</span> <span class=selfeval>#t</span>) <span class=selfeval>1</span> (<span class=keyword>amb</span>)) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>amb-fail</span> <span class=keyword>'</span><span class=variable>*</span>) (<span class=keyword>define</span> <span class=variable>initialize-amb-fail</span> (<span class=keyword>lambda</span> () (<span class=keyword>set!</span> <span class=variable>amb-fail</span> (<span class=keyword>lambda</span> () (<span class=variable>error</span> <span class=selfeval>&quot;amb tree exhausted&quot;</span>))))) (<span class=variable>initialize-amb-fail</span>) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>amb</span> (<span class=keyword>lambda</span> <span class=variable>alts...</span> <span class=keyword>`</span>(<span class=keyword>let</span> ((<span class=variable>+prev-amb-fail</span> <span class=variable>amb-fail</span>)) (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>+sk</span>) <span class=keyword>,@</span>(<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>alt</span>) <span class=keyword>`</span>(<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>+fk</span>) (<span class=keyword>set!</span> <span class=variable>amb-fail</span> (<span class=keyword>lambda</span> () (<span class=keyword>set!</span> <span class=variable>amb-fail</span> <span class=variable>+prev-amb-fail</span>) (<span class=variable>+fk</span> <span class=keyword>'</span><span class=variable>fail</span>))) (<span class=variable>+sk</span> <span class=keyword>,</span><span class=variable>alt</span>)))) <span class=variable>alts...</span>) (<span class=variable>+prev-amb-fail</span>)))))) </pre> <pre class=scheme>(<span class=keyword>amb</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span> <span class=selfeval>5</span> <span class=selfeval>6</span> <span class=selfeval>7</span> <span class=selfeval>8</span> <span class=selfeval>9</span> <span class=selfeval>10</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>number-between</span> (<span class=keyword>lambda</span> (<span class=variable>lo</span> <span class=variable>hi</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=variable>lo</span>)) (<span class=keyword>if</span> (<span class=variable>&gt;</span> <span class=variable>i</span> <span class=variable>hi</span>) (<span class=keyword>amb</span>) (<span class=keyword>amb</span> <span class=variable>i</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>assert</span> (<span class=keyword>lambda</span> (<span class=variable>pred</span>) (<span class=keyword>if</span> (<span class=variable>not</span> <span class=variable>pred</span>) (<span class=keyword>amb</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>gen-prime</span> (<span class=keyword>lambda</span> (<span class=variable>hi</span>) (<span class=keyword>let</span> ((<span class=variable>i</span> (<span class=variable>number-between</span> <span class=selfeval>2</span> <span class=variable>hi</span>))) (<span class=variable>assert</span> (<span class=variable>prime?</span> <span class=variable>i</span>)) <span class=variable>i</span>))) </pre> <pre class=scheme>(<span class=keyword>amb</span>) <span class=keyword>=&gt;</span> <span class=selfeval>3</span> </pre> <pre class=scheme>(<span class=keyword>amb</span>) <span class=keyword>=&gt;</span> <span class=selfeval>5</span> </pre> <pre class=scheme>(<span class=keyword>bag-of</span> (<span class=variable>gen-prime</span> <span class=selfeval>20</span>)) </pre> <pre class=scheme>(<span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>5</span> <span class=selfeval>7</span> <span class=selfeval>11</span> <span class=selfeval>13</span> <span class=selfeval>17</span> <span class=selfeval>19</span>) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=keyword>bag-of</span> (<span class=keyword>lambda</span> (<span class=variable>e</span>) <span class=keyword>`</span>(<span class=keyword>let</span> ((<span class=variable>+prev-amb-fail</span> <span class=variable>amb-fail</span>) (<span class=variable>+results</span> <span class=keyword>'</span>())) (<span class=keyword>if</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>+k</span>) (<span class=keyword>set!</span> <span class=variable>amb-fail</span> (<span class=keyword>lambda</span> () (<span class=variable>+k</span> <span class=selfeval>#f</span>))) (<span class=keyword>let</span> ((<span class=variable>+v</span> <span class=keyword>,</span><span class=variable>e</span>)) (<span class=keyword>set!</span> <span class=variable>+results</span> (<span class=variable>cons</span> <span class=variable>+v</span> <span class=variable>+results</span>)) (<span class=variable>+k</span> <span class=selfeval>#t</span>)))) (<span class=variable>amb-fail</span>)) (<span class=keyword>set!</span> <span class=variable>amb-fail</span> <span class=variable>+prev-amb-fail</span>) (<span class=variable>reverse!</span> <span class=variable>+results</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>solve-kalotan-puzzle</span> (<span class=keyword>lambda</span> () (<span class=keyword>let</span> ((<span class=variable>parent1</span> (<span class=keyword>amb</span> <span class=keyword>'</span><span class=variable>m</span> <span class=keyword>'</span><span class=variable>f</span>)) (<span class=variable>parent2</span> (<span class=keyword>amb</span> <span class=keyword>'</span><span class=variable>m</span> <span class=keyword>'</span><span class=variable>f</span>)) (<span class=variable>kibi</span> (<span class=keyword>amb</span> <span class=keyword>'</span><span class=variable>m</span> <span class=keyword>'</span><span class=variable>f</span>)) (<span class=variable>kibi-self-desc</span> (<span class=keyword>amb</span> <span class=keyword>'</span><span class=variable>m</span> <span class=keyword>'</span><span class=variable>f</span>)) (<span class=variable>kibi-lied?</span> (<span class=keyword>amb</span> <span class=selfeval>#t</span> <span class=selfeval>#f</span>))) (<span class=variable>assert</span> (<span class=variable>distinct?</span> (<span class=variable>list</span> <span class=variable>parent1</span> <span class=variable>parent2</span>))) (<span class=variable>assert</span> (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=variable>not</span> <span class=variable>kibi-lied?</span>))) (<span class=variable>assert</span> (<span class=keyword>if</span> <span class=variable>kibi-lied?</span> (<span class=variable>xor</span> (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi-self-desc</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>f</span>)) (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi-self-desc</span> <span class=keyword>'</span><span class=variable>f</span>) (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>m</span>))))) (<span class=variable>assert</span> (<span class=keyword>if</span> (<span class=variable>not</span> <span class=variable>kibi-lied?</span>) (<span class=variable>xor</span> (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi-self-desc</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>m</span>)) (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi-self-desc</span> <span class=keyword>'</span><span class=variable>f</span>) (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>f</span>))))) (<span class=variable>assert</span> (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>parent1</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi-self-desc</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=variable>xor</span> (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>f</span>) (<span class=variable>eqv?</span> <span class=variable>kibi-lied?</span> <span class=selfeval>#f</span>)) (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>m</span>) (<span class=variable>eqv?</span> <span class=variable>kibi-lied?</span> <span class=selfeval>#t</span>)))))) (<span class=variable>assert</span> (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>parent1</span> <span class=keyword>'</span><span class=variable>f</span>) (<span class=keyword>and</span> (<span class=variable>eqv?</span> <span class=variable>kibi</span> <span class=keyword>'</span><span class=variable>f</span>) (<span class=variable>eqv?</span> <span class=variable>kibi-lied?</span> <span class=selfeval>#t</span>)))) (<span class=variable>list</span> <span class=variable>parent1</span> <span class=variable>parent2</span> <span class=variable>kibi</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>choose-color</span> (<span class=keyword>lambda</span> () (<span class=keyword>amb</span> <span class=keyword>'</span><span class=variable>red</span> <span class=keyword>'</span><span class=variable>yellow</span> <span class=keyword>'</span><span class=variable>blue</span> <span class=keyword>'</span><span class=variable>white</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>color-europe</span> (<span class=keyword>lambda</span> () <span class=comment>;choose colors for each country</span> (<span class=keyword>let</span> ((<span class=variable>p</span> (<span class=variable>choose-color</span>)) <span class=comment>;Portugal</span> (<span class=variable>e</span> (<span class=variable>choose-color</span>)) <span class=comment>;Spain</span> (<span class=variable>f</span> (<span class=variable>choose-color</span>)) <span class=comment>;France</span> (<span class=variable>b</span> (<span class=variable>choose-color</span>)) <span class=comment>;Belgium</span> (<span class=variable>h</span> (<span class=variable>choose-color</span>)) <span class=comment>;Holland</span> (<span class=variable>g</span> (<span class=variable>choose-color</span>)) <span class=comment>;Germany</span> (<span class=variable>l</span> (<span class=variable>choose-color</span>)) <span class=comment>;Luxemb</span> (<span class=variable>i</span> (<span class=variable>choose-color</span>)) <span class=comment>;Italy</span> (<span class=variable>s</span> (<span class=variable>choose-color</span>)) <span class=comment>;Switz</span> (<span class=variable>a</span> (<span class=variable>choose-color</span>)) <span class=comment>;Austria</span> ) <span class=comment>;construct the adjacency list for</span> <span class=comment>;each country: the 1st element is</span> <span class=comment>;the name of the country; the 2nd</span> <span class=comment>;element is its color; the 3rd</span> <span class=comment>;element is the list of its</span> <span class=comment>;neighbors' colors</span> (<span class=keyword>let</span> ((<span class=variable>portugal</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>portugal</span> <span class=variable>p</span> (<span class=variable>list</span> <span class=variable>e</span>))) (<span class=variable>spain</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>spain</span> <span class=variable>e</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>p</span>))) (<span class=variable>france</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>france</span> <span class=variable>f</span> (<span class=variable>list</span> <span class=variable>e</span> <span class=variable>i</span> <span class=variable>s</span> <span class=variable>b</span> <span class=variable>g</span> <span class=variable>l</span>))) (<span class=variable>belgium</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>belgium</span> <span class=variable>b</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>h</span> <span class=variable>l</span> <span class=variable>g</span>))) (<span class=variable>holland</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>holland</span> <span class=variable>h</span> (<span class=variable>list</span> <span class=variable>b</span> <span class=variable>g</span>))) (<span class=variable>germany</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>germany</span> <span class=variable>g</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>s</span> <span class=variable>h</span> <span class=variable>b</span> <span class=variable>l</span>))) (<span class=variable>luxembourg</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>luxembourg</span> <span class=variable>l</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>b</span> <span class=variable>g</span>))) (<span class=variable>italy</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>italy</span> <span class=variable>i</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>s</span>))) (<span class=variable>switzerland</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>switzerland</span> <span class=variable>s</span> (<span class=variable>list</span> <span class=variable>f</span> <span class=variable>i</span> <span class=variable>a</span> <span class=variable>g</span>))) (<span class=variable>austria</span> (<span class=variable>list</span> <span class=keyword>'</span><span class=variable>austria</span> <span class=variable>a</span> (<span class=variable>list</span> <span class=variable>i</span> <span class=variable>s</span> <span class=variable>g</span>)))) (<span class=keyword>let</span> ((<span class=variable>countries</span> (<span class=variable>list</span> <span class=variable>portugal</span> <span class=variable>spain</span> <span class=variable>france</span> <span class=variable>belgium</span> <span class=variable>holland</span> <span class=variable>germany</span> <span class=variable>luxembourg</span> <span class=variable>italy</span> <span class=variable>switzerland</span> <span class=variable>austria</span>))) <span class=comment>;the color of a country</span> <span class=comment>;should not be the color of</span> <span class=comment>;any of its neighbors</span> (<span class=variable>for-each</span> (<span class=keyword>lambda</span> (<span class=variable>c</span>) (<span class=variable>assert</span> (<span class=variable>not</span> (<span class=variable>memq</span> (<span class=variable>cadr</span> <span class=variable>c</span>) (<span class=variable>caddr</span> <span class=variable>c</span>))))) <span class=variable>countries</span>) <span class=comment>;output the color</span> <span class=comment>;assignment</span> (<span class=variable>for-each</span> (<span class=keyword>lambda</span> (<span class=variable>c</span>) (<span class=variable>display</span> (<span class=variable>car</span> <span class=variable>c</span>)) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>display</span> (<span class=variable>cadr</span> <span class=variable>c</span>)) (<span class=variable>newline</span>)) <span class=variable>countries</span>)))))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-17.html'>Chapter 15</a></h2> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>printn-engine</span> (<span class=variable>make-engine</span> (<span class=keyword>lambda</span> () (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>0</span>)) (<span class=variable>display</span> <span class=variable>i</span>) (<span class=variable>display</span> <span class=selfeval>&quot; &quot;</span>) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=global>*more*</span> <span class=selfeval>#f</span>) (<span class=variable>printn-engine</span> <span class=selfeval>50</span> <span class=variable>list</span> (<span class=keyword>lambda</span> (<span class=variable>ne</span>) (<span class=keyword>set!</span> <span class=global>*more*</span> <span class=variable>ne</span>))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>0</span> <span class=selfeval>1</span> <span class=selfeval>2</span> <span class=selfeval>3</span> <span class=selfeval>4</span> <span class=selfeval>5</span> <span class=selfeval>6</span> <span class=selfeval>7</span> <span class=selfeval>8</span> <span class=selfeval>9</span> </pre> <pre class=scheme>(<span class=global>*more*</span> <span class=selfeval>50</span> <span class=variable>list</span> (<span class=keyword>lambda</span> (<span class=variable>ne</span>) (<span class=keyword>set!</span> <span class=global>*more*</span> <span class=variable>ne</span>))) <font color="teal"><tt><strong>=&gt;</strong></tt></font> <span class=selfeval>10</span> <span class=selfeval>11</span> <span class=selfeval>12</span> <span class=selfeval>13</span> <span class=selfeval>14</span> <span class=selfeval>15</span> <span class=selfeval>16</span> <span class=selfeval>17</span> <span class=selfeval>18</span> <span class=selfeval>19</span> </pre> <pre class=scheme>(<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set-handler</span> (<span class=keyword>lambda</span> () (<span class=variable>error</span> <span class=selfeval>&quot;Say goodnight, cat!&quot;</span>))) (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=selfeval>9</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=global>*engine-escape*</span> <span class=selfeval>#f</span>) (<span class=keyword>define</span> <span class=global>*engine-entrance*</span> <span class=selfeval>#f</span>) (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set-handler</span> (<span class=keyword>lambda</span> () (<span class=variable>call/cc</span> <span class=global>*engine-escape*</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>make-engine</span> (<span class=keyword>lambda</span> (<span class=variable>th</span>) (<span class=keyword>lambda</span> (<span class=variable>ticks</span> <span class=variable>success</span> <span class=variable>failure</span>) (<span class=keyword>let*</span> ((<span class=variable>ticks-left</span> <span class=selfeval>0</span>) (<span class=variable>engine-succeeded?</span> <span class=selfeval>#f</span>) (<span class=variable>result</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=global>*engine-escape*</span> <span class=variable>k</span>) (<span class=keyword>let</span> ((<span class=variable>result</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=global>*engine-entrance*</span> <span class=variable>k</span>) (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=variable>ticks</span>) (<span class=keyword>let</span> ((<span class=variable>v</span> (<span class=variable>th</span>))) (<span class=global>*engine-entrance*</span> <span class=variable>v</span>)))))) (<span class=keyword>set!</span> <span class=variable>ticks-left</span> (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=global>*infinity*</span>)) (<span class=keyword>set!</span> <span class=variable>engine-succeeded?</span> <span class=selfeval>#t</span>) <span class=variable>result</span>))))) (<span class=keyword>if</span> <span class=variable>engine-succeeded?</span> (<span class=variable>success</span> <span class=variable>result</span> <span class=variable>ticks-left</span>) (<span class=variable>failure</span> (<span class=variable>make-engine</span> (<span class=keyword>lambda</span> () (<span class=variable>result</span> <span class=keyword>'</span><span class=variable>resume</span>))))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>make-engine</span> (<span class=keyword>lambda</span> (<span class=variable>th</span>) (<span class=keyword>lambda</span> (<span class=variable>ticks</span> <span class=variable>s</span> <span class=variable>f</span>) (<span class=keyword>let*</span> ((<span class=variable>parent-ticks</span> (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=global>*infinity*</span>)) <span class=comment>;A child can't have more ticks than its parent's</span> <span class=comment>;remaining ticks</span> (<span class=variable>child-available-ticks</span> (<span class=variable>clock-min</span> <span class=variable>parent-ticks</span> <span class=variable>ticks</span>)) <span class=comment>;A child's ticks must be counted against the parent</span> <span class=comment>;too</span> (<span class=variable>parent-ticks-left</span> (<span class=variable>clock-minus</span> <span class=variable>parent-ticks</span> <span class=variable>child-available-ticks</span>)) <span class=comment>;If child was promised more ticks than parent could</span> <span class=comment>;afford, remember how much it was short-changed by</span> (<span class=variable>child-ticks-left</span> (<span class=variable>clock-minus</span> <span class=variable>ticks</span> <span class=variable>child-available-ticks</span>)) <span class=comment>;Used below to store ticks left in clock</span> <span class=comment>;if child completes in time</span> (<span class=variable>ticks-left</span> <span class=selfeval>0</span>) (<span class=variable>engine-succeeded?</span> <span class=selfeval>#f</span>) (<span class=variable>result</span> (<span class=keyword>fluid-let</span> ((<span class=global>*engine-escape*</span> <span class=selfeval>#f</span>) (<span class=global>*engine-entrance*</span> <span class=selfeval>#f</span>)) (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=global>*engine-escape*</span> <span class=variable>k</span>) (<span class=keyword>let</span> ((<span class=variable>result</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=global>*engine-entrance*</span> <span class=variable>k</span>) (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=variable>child-available-ticks</span>) (<span class=keyword>let</span> ((<span class=variable>v</span> (<span class=variable>th</span>))) (<span class=global>*engine-entrance*</span> <span class=variable>v</span>)))))) (<span class=keyword>set!</span> <span class=variable>ticks-left</span> (<span class=keyword>let</span> ((<span class=variable>n</span> (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=global>*infinity*</span>))) (<span class=keyword>if</span> (<span class=variable>eqv?</span> <span class=variable>n</span> <span class=global>*infinity*</span>) <span class=selfeval>0</span> <span class=variable>n</span>))) (<span class=keyword>set!</span> <span class=variable>engine-succeeded?</span> <span class=selfeval>#t</span>) <span class=variable>result</span>)))))) <span class=comment>;Parent can reclaim ticks that child didn't need</span> (<span class=keyword>set!</span> <span class=variable>parent-ticks-left</span> (<span class=variable>clock-plus</span> <span class=variable>parent-ticks-left</span> <span class=variable>ticks-left</span>)) <span class=comment>;This is the true ticks that child has left --</span> <span class=comment>;we include the ticks it was short-changed by</span> (<span class=keyword>set!</span> <span class=variable>ticks-left</span> (<span class=variable>clock-plus</span> <span class=variable>child-ticks-left</span> <span class=variable>ticks-left</span>)) <span class=comment>;Restart parent with its remaining ticks</span> (<span class=variable>clock</span> <span class=keyword>'</span><span class=variable>set</span> <span class=variable>parent-ticks-left</span>) <span class=comment>;The rest is now parent computation</span> (<span class=keyword>cond</span> <span class=comment>;Child finished in time -- celebrate its success</span> (<span class=variable>engine-succeeded?</span> (<span class=variable>s</span> <span class=variable>result</span> <span class=variable>ticks-left</span>)) <span class=comment>;Child failed because it ran out of promised time --</span> <span class=comment>;call failure procedure</span> ((<span class=variable>=</span> <span class=variable>ticks-left</span> <span class=selfeval>0</span>) (<span class=variable>f</span> (<span class=variable>make-engine</span> (<span class=keyword>lambda</span> () (<span class=variable>result</span> <span class=keyword>'</span><span class=variable>resume</span>))))) <span class=comment>;Child failed because parent didn't have enough time,</span> <span class=comment>;ie, parent failed too. If so, when parent is</span> <span class=comment>;resumed, its first order of duty is to resume the</span> <span class=comment>;child with its fair amount of ticks</span> (<span class=keyword>else</span> ((<span class=variable>make-engine</span> (<span class=keyword>lambda</span> () (<span class=variable>result</span> <span class=keyword>'</span><span class=variable>resume</span>))) <span class=variable>ticks-left</span> <span class=variable>s</span> <span class=variable>f</span>))))))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-18.html'>Chapter 16</a></h2> <pre class=verbatim>echo Hello, World!&nbsp; </pre> <pre class=verbatim>chmod +x hello&nbsp; </pre> <pre class=verbatim>hello&nbsp; </pre> <pre class=verbatim>&quot;:&quot;; exec mzscheme -r $0 &quot;$@&quot;&nbsp; &nbsp; (display &quot;Hello, World!&quot;)&nbsp; (newline))&nbsp; </pre> <pre class=verbatim>mzscheme -r /whereveritis/hello&nbsp; </pre> <pre class=verbatim>Hello, World!&nbsp; </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>; exec mzscheme -r $0 &quot;$@&quot;</span> <span class=comment>;Put in argv-count the number of arguments supplied</span> (<span class=keyword>define</span> <span class=variable>argv-count</span> (<span class=variable>vector-length</span> <span class=variable>argv</span>)) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>0</span>)) (<span class=keyword>unless</span> (<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>argv-count</span>) (<span class=variable>display</span> (<span class=variable>vector-ref</span> <span class=variable>argv</span> <span class=variable>i</span>)) (<span class=variable>newline</span>) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>)))) </pre> <pre class=verbatim>1&nbsp; 2&nbsp; 3&nbsp; </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>;exec mzscheme -r $0 &quot;$@&quot;</span> <span class=comment>;floppy-size = number of bytes that will comfortably fit on a</span> <span class=comment>; 3.5&quot; floppy</span> (<span class=keyword>define</span> <span class=variable>floppy-size</span> <span class=selfeval>1440000</span>) <span class=comment>;split splits the bigfile f into the smaller, floppy-sized</span> <span class=comment>;subfiles, viz, subfile-prefix.1, subfile-prefix.2, etc.</span> (<span class=keyword>define</span> <span class=variable>split</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>subfile-prefix</span>) (<span class=variable>call-with-input-file</span> <span class=variable>f</span> (<span class=keyword>lambda</span> (<span class=variable>i</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>n</span> <span class=selfeval>1</span>)) (<span class=keyword>if</span> (<span class=variable>copy-to-floppy-sized-subfile</span> <span class=variable>i</span> <span class=variable>subfile-prefix</span> <span class=variable>n</span>) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>1</span>)))))))) <span class=comment>;copy-to-floppy-sized-subfile copies the next 1.44 million</span> <span class=comment>;bytes (if there are less than that many bytes left, it</span> <span class=comment>;copies all of them) from the big file to the nth</span> <span class=comment>;subfile. Returns true if there are bytes left over,</span> <span class=comment>;otherwise returns false.</span> (<span class=keyword>define</span> <span class=variable>copy-to-floppy-sized-subfile</span> (<span class=keyword>lambda</span> (<span class=variable>i</span> <span class=variable>subfile-prefix</span> <span class=variable>n</span>) (<span class=keyword>let</span> ((<span class=variable>nth-subfile</span> (<span class=variable>string-append</span> <span class=variable>subfile-prefix</span> <span class=selfeval>&quot;.&quot;</span> (<span class=variable>number-&gt;string</span> <span class=variable>n</span>)))) (<span class=keyword>if</span> (<span class=variable>file-exists?</span> <span class=variable>nth-subfile</span>) (<span class=variable>delete-file</span> <span class=variable>nth-subfile</span>)) (<span class=variable>call-with-output-file</span> <span class=variable>nth-subfile</span> (<span class=keyword>lambda</span> (<span class=variable>o</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>k</span> <span class=selfeval>1</span>)) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>read-char</span> <span class=variable>i</span>))) (<span class=keyword>cond</span> ((<span class=variable>eof-object?</span> <span class=variable>c</span>) <span class=selfeval>#f</span>) (<span class=keyword>else</span> (<span class=variable>write-char</span> <span class=variable>c</span> <span class=variable>o</span>) (<span class=keyword>if</span> (<span class=variable>&lt;</span> <span class=variable>k</span> <span class=variable>floppy-size</span>) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>k</span> <span class=selfeval>1</span>)) <span class=selfeval>#t</span>)))))))))) <span class=comment>;bigfile = script's first arg</span> <span class=comment>; = the file that needs splitting</span> (<span class=keyword>define</span> <span class=variable>bigfile</span> (<span class=variable>vector-ref</span> <span class=variable>argv</span> <span class=selfeval>0</span>)) <span class=comment>;subfile-prefix = script's second arg</span> <span class=comment>; = the basename of the subfiles</span> (<span class=keyword>define</span> <span class=variable>subfile-prefix</span> (<span class=variable>vector-ref</span> <span class=variable>argv</span> <span class=selfeval>1</span>)) <span class=comment>;Call split, making subfile-prefix.{1,2,3,...} from</span> <span class=comment>;bigfile</span> (<span class=variable>split</span> <span class=variable>bigfile</span> <span class=variable>subfile-prefix</span>) </pre> <pre class=verbatim>split4floppy largefile chunk&nbsp; </pre> <pre class=verbatim>cat chunk.1 chunk.2 ... &gt; largefile&nbsp; </pre> <pre class=verbatim>copy /b chunk.1+chunk.2+... largefile&nbsp; </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-19.html'>Chapter 17</a></h2> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>;exec mzscheme -r $0 &quot;$@&quot;</span> </pre> <pre class=scheme><span class=selfeval>#!/bin/sh</span> <span class=selfeval>&quot;:&quot;</span><span class=comment>;exec /usr/local/bin/mzscheme -r $0 &quot;$@&quot;</span> </pre> <pre class=verbatim>content-type: text/plain&nbsp; </pre> <pre class=scheme><span class=selfeval>#!/bin/sh</span> <span class=selfeval>&quot;:&quot;</span><span class=comment>;exec /usr/local/bin/mzscheme -r $0 &quot;$@&quot;</span> <span class=comment>;Identify content-type as plain text.</span> (<span class=variable>display</span> <span class=selfeval>&quot;content-type: text/plain&quot;</span>) (<span class=variable>newline</span>) (<span class=variable>newline</span>) <span class=comment>;Generate a page with the requested info. This is</span> <span class=comment>;done by simply writing to standard output.</span> (<span class=variable>for-each</span> (<span class=keyword>lambda</span> (<span class=variable>env-var</span>) (<span class=variable>display</span> <span class=variable>env-var</span>) (<span class=variable>display</span> <span class=selfeval>&quot; = &quot;</span>) (<span class=variable>display</span> (<span class=keyword>or</span> (<span class=variable>getenv</span> <span class=variable>env-var</span>) <span class=selfeval>&quot;&quot;</span>)) (<span class=variable>newline</span>)) <span class=keyword>'</span>(<span class=selfeval>&quot;AUTH_TYPE&quot;</span> <span class=selfeval>&quot;CONTENT_LENGTH&quot;</span> <span class=selfeval>&quot;CONTENT_TYPE&quot;</span> <span class=selfeval>&quot;DOCUMENT_ROOT&quot;</span> <span class=selfeval>&quot;GATEWAY_INTERFACE&quot;</span> <span class=selfeval>&quot;HTTP_ACCEPT&quot;</span> <span class=selfeval>&quot;HTTP_REFERER&quot;</span> <span class=comment>; [sic]</span> <span class=selfeval>&quot;HTTP_USER_AGENT&quot;</span> <span class=selfeval>&quot;PATH_INFO&quot;</span> <span class=selfeval>&quot;PATH_TRANSLATED&quot;</span> <span class=selfeval>&quot;QUERY_STRING&quot;</span> <span class=selfeval>&quot;REMOTE_ADDR&quot;</span> <span class=selfeval>&quot;REMOTE_HOST&quot;</span> <span class=selfeval>&quot;REMOTE_IDENT&quot;</span> <span class=selfeval>&quot;REMOTE_USER&quot;</span> <span class=selfeval>&quot;REQUEST_METHOD&quot;</span> <span class=selfeval>&quot;SCRIPT_NAME&quot;</span> <span class=selfeval>&quot;SERVER_NAME&quot;</span> <span class=selfeval>&quot;SERVER_PORT&quot;</span> <span class=selfeval>&quot;SERVER_PROTOCOL&quot;</span> <span class=selfeval>&quot;SERVER_SOFTWARE&quot;</span>)) </pre> <pre class=verbatim>http://www.foo.org/cgi-bin/testcgi.scm&nbsp; </pre> <pre class=verbatim>... To view some common CGI environment variables, click&nbsp; &lt;a href=&quot;http://www.foo.org/cgi-bin/testcgi.scm&quot;&gt;here&lt;/a&gt;.&nbsp; ...&nbsp; </pre> <pre class=verbatim>AUTH_TYPE =&nbsp; CONTENT_LENGTH =&nbsp; CONTENT_TYPE =&nbsp; DOCUMENT_ROOT = /home/httpd/html&nbsp; GATEWAY_INTERFACE = CGI/1.1&nbsp; HTTP_ACCEPT = image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, */*&nbsp; HTTP_REFERER =&nbsp; HTTP_USER_AGENT = Mozilla/3.01Gold (X11; I; Linux 2.0.32 i586)&nbsp; PATH_INFO =&nbsp; PATH_TRANSLATED =&nbsp; QUERY_STRING =&nbsp; REMOTE_HOST = 127.0.0.1&nbsp; REMOTE_ADDR = 127.0.0.1&nbsp; REMOTE_IDENT =&nbsp; REMOTE_USER =&nbsp; REQUEST_METHOD = GET&nbsp; SCRIPT_NAME = /cgi-bin/testcgi.scm&nbsp; SERVER_NAME = localhost.localdomain&nbsp; SERVER_PORT = 80&nbsp; SERVER_PROTOCOL = HTTP/1.0&nbsp; SERVER_SOFTWARE = Apache/1.2.4&nbsp; </pre> <pre class=verbatim>&lt;html&gt;&nbsp; &lt;head&gt;&nbsp; &lt;title&gt;Form for checking environment variables&lt;/title&gt;&nbsp; &lt;/head&gt;&nbsp; &lt;body&gt;&nbsp; &nbsp; &lt;form method=get &nbsp; action=&quot;http://www.foo.org/cgi-bin/testcgi2.scm&quot;&gt;&nbsp; Enter environment variable: &lt;input type=text name=envvar size=30&gt;&nbsp; &lt;p&gt;&nbsp; &nbsp; &lt;input type=submit&gt;&nbsp; &lt;/form&gt;&nbsp; &nbsp; &lt;/body&gt;&nbsp; &lt;/html&gt;&nbsp; </pre> <pre class=scheme><span class=selfeval>#!/bin/sh</span> <span class=selfeval>&quot;:&quot;</span><span class=comment>;exec /usr/local/bin/mzscheme -r $0 &quot;$@&quot;</span> (<span class=variable>display</span> <span class=selfeval>&quot;content-type: text/plain&quot;</span>) (<span class=variable>newline</span>) (<span class=variable>newline</span>) <span class=comment>;string-index returns the leftmost index in string s</span> <span class=comment>;that has character c</span> (<span class=keyword>define</span> <span class=variable>string-index</span> (<span class=keyword>lambda</span> (<span class=variable>s</span> <span class=variable>c</span>) (<span class=keyword>let</span> ((<span class=variable>n</span> (<span class=variable>string-length</span> <span class=variable>s</span>))) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>0</span>)) (<span class=keyword>cond</span> ((<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>n</span>) <span class=selfeval>#f</span>) ((<span class=variable>char=?</span> (<span class=variable>string-ref</span> <span class=variable>s</span> <span class=variable>i</span>) <span class=variable>c</span>) <span class=variable>i</span>) (<span class=keyword>else</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>)))))))) <span class=comment>;split breaks string s into substrings separated by character c</span> (<span class=keyword>define</span> <span class=variable>split</span> (<span class=keyword>lambda</span> (<span class=variable>c</span> <span class=variable>s</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>s</span> <span class=variable>s</span>)) (<span class=keyword>if</span> (<span class=variable>string=?</span> <span class=variable>s</span> <span class=selfeval>&quot;&quot;</span>) <span class=keyword>'</span>() (<span class=keyword>let</span> ((<span class=variable>i</span> (<span class=variable>string-index</span> <span class=variable>s</span> <span class=variable>c</span>))) (<span class=keyword>if</span> <span class=variable>i</span> (<span class=variable>cons</span> (<span class=variable>substring</span> <span class=variable>s</span> <span class=selfeval>0</span> <span class=variable>i</span>) (<span class=variable>loop</span> (<span class=variable>substring</span> <span class=variable>s</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>string-length</span> <span class=variable>s</span>)))) (<span class=variable>list</span> <span class=variable>s</span>))))))) (<span class=keyword>define</span> <span class=variable>args</span> (<span class=variable>map</span> (<span class=keyword>lambda</span> (<span class=variable>par-arg</span>) (<span class=variable>split</span> <span class=selfeval>#\=</span> <span class=variable>par-arg</span>)) (<span class=variable>split</span> <span class=selfeval>#\&amp;</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;QUERY_STRING&quot;</span>)))) (<span class=keyword>define</span> <span class=variable>envvar</span> (<span class=variable>cadr</span> (<span class=variable>assoc</span> <span class=selfeval>&quot;envvar&quot;</span> <span class=variable>args</span>))) (<span class=variable>display</span> <span class=variable>envvar</span>) (<span class=variable>display</span> <span class=selfeval>&quot; = &quot;</span>) (<span class=variable>display</span> (<span class=variable>getenv</span> <span class=variable>envvar</span>)) (<span class=variable>newline</span>) </pre> <pre class=verbatim>20%25+%2b+30%25+%3d+50%25%2c+%26c%2e&nbsp; </pre> <pre class=scheme><span class=selfeval>#!/bin/sh</span> <span class=selfeval>&quot;:&quot;</span><span class=comment>;exec /usr/local/bin/mzscheme -r $0 &quot;$@&quot;</span> <span class=comment>;Load the cgi utilities</span> (<span class=variable>load-relatve</span> <span class=selfeval>&quot;cgi.scm&quot;</span>) (<span class=variable>display</span> <span class=selfeval>&quot;content-type: text/plain&quot;</span>) (<span class=variable>newline</span>) (<span class=variable>newline</span>) <span class=comment>;Read the data input via the form</span> (<span class=variable>parse-form-data</span>) <span class=comment>;Get the envvar parameter</span> (<span class=keyword>define</span> <span class=variable>envvar</span> (<span class=variable>form-data-get/1</span> <span class=selfeval>&quot;envvar&quot;</span>)) <span class=comment>;Display the value of the envvar</span> (<span class=variable>display</span> <span class=variable>envvar</span>) (<span class=variable>display</span> <span class=selfeval>&quot; = &quot;</span>) (<span class=variable>display</span> (<span class=variable>getenv</span> <span class=variable>envvar</span>)) (<span class=variable>newline</span>) </pre> <pre class=scheme><span class=comment>;Load our table definitions</span> (<span class=variable>load-relative</span> <span class=selfeval>&quot;table.scm&quot;</span>) <span class=comment>;Define the *form-data-table*</span> (<span class=keyword>define</span> <span class=global>*form-data-table*</span> (<span class=variable>make-table</span> <span class=keyword>'</span><span class=variable>equ</span> <span class=variable>string=?</span>)) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>parse-form-data</span> (<span class=keyword>lambda</span> () ((<span class=keyword>if</span> (<span class=variable>string-ci=?</span> (<span class=keyword>or</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;REQUEST_METHOD&quot;</span>) <span class=selfeval>&quot;GET&quot;</span>) <span class=selfeval>&quot;GET&quot;</span>) <span class=variable>parse-form-data-using-query-string</span> <span class=variable>parse-form-data-using-stdin</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>parse-form-data-using-query-string</span> (<span class=keyword>lambda</span> () (<span class=keyword>let</span> ((<span class=variable>query-string</span> (<span class=keyword>or</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;QUERY_STRING&quot;</span>) <span class=selfeval>&quot;&quot;</span>))) (<span class=variable>for-each</span> (<span class=keyword>lambda</span> (<span class=variable>par=arg</span>) (<span class=keyword>let</span> ((<span class=variable>par/arg</span> (<span class=variable>split</span> <span class=selfeval>#\=</span> <span class=variable>par=arg</span>))) (<span class=keyword>let</span> ((<span class=variable>par</span> (<span class=variable>url-decode</span> (<span class=variable>car</span> <span class=variable>par/arg</span>))) (<span class=variable>arg</span> (<span class=variable>url-decode</span> (<span class=variable>cadr</span> <span class=variable>par/arg</span>)))) (<span class=variable>table-put!</span> <span class=global>*form-data-table*</span> <span class=variable>par</span> (<span class=variable>cons</span> <span class=variable>arg</span> (<span class=variable>table-get</span> <span class=global>*form-data-table*</span> <span class=variable>par</span> <span class=keyword>'</span>())))))) (<span class=variable>split</span> <span class=selfeval>#\&amp;</span> <span class=variable>query-string</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>url-decode</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=keyword>let</span> ((<span class=variable>s</span> (<span class=variable>string-&gt;list</span> <span class=variable>s</span>))) (<span class=variable>list-&gt;string</span> (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>s</span> <span class=variable>s</span>)) (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>s</span>) <span class=keyword>'</span>() (<span class=keyword>let</span> ((<span class=variable>a</span> (<span class=variable>car</span> <span class=variable>s</span>)) (<span class=variable>d</span> (<span class=variable>cdr</span> <span class=variable>s</span>))) (<span class=keyword>case</span> <span class=variable>a</span> ((<span class=selfeval>#\+</span>) (<span class=variable>cons</span> <span class=selfeval>#\space</span> (<span class=variable>loop</span> <span class=variable>d</span>))) ((<span class=selfeval>#\%</span>) (<span class=variable>cons</span> (<span class=variable>hex-&gt;char</span> (<span class=variable>car</span> <span class=variable>d</span>) (<span class=variable>cadr</span> <span class=variable>d</span>)) (<span class=variable>loop</span> (<span class=variable>cddr</span> <span class=variable>d</span>)))) (<span class=keyword>else</span> (<span class=variable>cons</span> <span class=variable>a</span> (<span class=variable>loop</span> <span class=variable>d</span>))))))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>hex-&gt;char</span> (<span class=keyword>lambda</span> (<span class=variable>x</span> <span class=variable>y</span>) (<span class=variable>integer-&gt;char</span> (<span class=variable>string-&gt;number</span> (<span class=variable>string</span> <span class=variable>x</span> <span class=variable>y</span>) <span class=selfeval>16</span>)))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>parse-form-data-using-stdin</span> (<span class=keyword>lambda</span> () (<span class=keyword>let*</span> ((<span class=variable>content-length</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;CONTENT_LENGTH&quot;</span>)) (<span class=variable>content-length</span> (<span class=keyword>if</span> <span class=variable>content-length</span> (<span class=variable>string-&gt;number</span> <span class=variable>content-length</span>) <span class=selfeval>0</span>)) (<span class=variable>i</span> <span class=selfeval>0</span>)) (<span class=keyword>let</span> <span class=variable>par-loop</span> ((<span class=variable>par</span> <span class=keyword>'</span>())) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>read-char</span>))) (<span class=keyword>set!</span> <span class=variable>i</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>)) (<span class=keyword>if</span> (<span class=keyword>or</span> (<span class=variable>&gt;</span> <span class=variable>i</span> <span class=variable>content-length</span>) (<span class=variable>eof-object?</span> <span class=variable>c</span>) (<span class=variable>char=?</span> <span class=variable>c</span> <span class=selfeval>#\=</span>)) (<span class=keyword>let</span> <span class=variable>arg-loop</span> ((<span class=variable>arg</span> <span class=keyword>'</span>())) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>read-char</span>))) (<span class=keyword>set!</span> <span class=variable>i</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>)) (<span class=keyword>if</span> (<span class=keyword>or</span> (<span class=variable>&gt;</span> <span class=variable>i</span> <span class=variable>content-length</span>) (<span class=variable>eof-object?</span> <span class=variable>c</span>) (<span class=variable>char=?</span> <span class=variable>c</span> <span class=selfeval>#\&amp;</span>)) (<span class=keyword>let</span> ((<span class=variable>par</span> (<span class=variable>url-decode</span> (<span class=variable>list-&gt;string</span> (<span class=variable>reverse!</span> <span class=variable>par</span>)))) (<span class=variable>arg</span> (<span class=variable>url-decode</span> (<span class=variable>list-&gt;string</span> (<span class=variable>reverse!</span> <span class=variable>arg</span>))))) (<span class=variable>table-put!</span> <span class=global>*form-data-table*</span> <span class=variable>par</span> (<span class=variable>cons</span> <span class=variable>arg</span> (<span class=variable>table-get</span> <span class=global>*form-data-table*</span> <span class=variable>par</span> <span class=keyword>'</span>()))) (<span class=keyword>unless</span> (<span class=keyword>or</span> (<span class=variable>&gt;</span> <span class=variable>i</span> <span class=variable>content-length</span>) (<span class=variable>eof-object?</span> <span class=variable>c</span>)) (<span class=variable>par-loop</span> <span class=keyword>'</span>()))) (<span class=variable>arg-loop</span> (<span class=variable>cons</span> <span class=variable>c</span> <span class=variable>arg</span>))))) (<span class=variable>par-loop</span> (<span class=variable>cons</span> <span class=variable>c</span> <span class=variable>par</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>form-data-get</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=variable>table-get</span> <span class=global>*form-data-table*</span> <span class=variable>k</span> <span class=keyword>'</span>()))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>form-data-get/1</span> (<span class=keyword>lambda</span> (<span class=variable>k</span> . <span class=variable>default</span>) (<span class=keyword>let</span> ((<span class=variable>vv</span> (<span class=variable>form-data-get</span> <span class=variable>k</span>))) (<span class=keyword>cond</span> ((<span class=variable>pair?</span> <span class=variable>vv</span>) (<span class=variable>car</span> <span class=variable>vv</span>)) ((<span class=variable>pair?</span> <span class=variable>default</span>) (<span class=variable>car</span> <span class=variable>default</span>)) (<span class=keyword>else</span> <span class=selfeval>&quot;&quot;</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>display-html</span> (<span class=keyword>lambda</span> (<span class=variable>s</span> . <span class=variable>o</span>) (<span class=keyword>let</span> ((<span class=variable>o</span> (<span class=keyword>if</span> (<span class=variable>null?</span> <span class=variable>o</span>) (<span class=variable>current-output-port</span>) (<span class=variable>car</span> <span class=variable>o</span>)))) (<span class=keyword>let</span> ((<span class=variable>n</span> (<span class=variable>string-length</span> <span class=variable>s</span>))) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>i</span> <span class=selfeval>0</span>)) (<span class=keyword>unless</span> (<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>n</span>) (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>string-ref</span> <span class=variable>s</span> <span class=variable>i</span>))) (<span class=variable>display</span> (<span class=keyword>case</span> <span class=variable>c</span> ((<span class=selfeval>#\&lt;</span>) <span class=selfeval>&quot;&amp;lt;&quot;</span>) ((<span class=selfeval>#\&gt;</span>) <span class=selfeval>&quot;&amp;gt;&quot;</span>) ((<span class=selfeval>#\&quot;</span>) <span class=selfeval>&quot;&amp;quot;&quot;</span>) ((<span class=selfeval>#\&amp;</span>) <span class=selfeval>&quot;&amp;amp;&quot;</span>) (<span class=keyword>else</span> <span class=variable>c</span>)) <span class=variable>o</span>) (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>))))))))) </pre> <pre class=scheme><span class=selfeval>#!/bin/sh</span> <span class=selfeval>&quot;:&quot;</span><span class=comment>;exec /usr/local/bin/mzscheme -r $0</span> <span class=comment>;Load the CGI utilities</span> (<span class=variable>load-relative</span> <span class=selfeval>&quot;cgi.scm&quot;</span>) (<span class=keyword>define</span> <span class=variable>uhoh</span> <span class=selfeval>#f</span>) (<span class=keyword>define</span> <span class=variable>calc-eval</span> (<span class=keyword>lambda</span> (<span class=variable>e</span>) (<span class=keyword>if</span> (<span class=variable>pair?</span> <span class=variable>e</span>) (<span class=variable>apply</span> (<span class=variable>ensure-operator</span> (<span class=variable>car</span> <span class=variable>e</span>)) (<span class=variable>map</span> <span class=variable>calc-eval</span> (<span class=variable>cdr</span> <span class=variable>e</span>))) (<span class=variable>ensure-number</span> <span class=variable>e</span>)))) (<span class=keyword>define</span> <span class=variable>ensure-operator</span> (<span class=keyword>lambda</span> (<span class=variable>e</span>) (<span class=keyword>case</span> <span class=variable>e</span> ((<span class=variable>+</span>) <span class=variable>+</span>) ((<span class=variable>-</span>) <span class=variable>-</span>) ((<span class=variable>*</span>) <span class=variable>*</span>) ((<span class=variable>/</span>) <span class=variable>/</span>) ((<span class=variable>**</span>) <span class=variable>expt</span>) (<span class=keyword>else</span> (<span class=variable>uhoh</span> <span class=selfeval>&quot;unpermitted operator&quot;</span>))))) (<span class=keyword>define</span> <span class=variable>ensure-number</span> (<span class=keyword>lambda</span> (<span class=variable>e</span>) (<span class=keyword>if</span> (<span class=variable>number?</span> <span class=variable>e</span>) <span class=variable>e</span> (<span class=variable>uhoh</span> <span class=selfeval>&quot;non-number&quot;</span>)))) (<span class=keyword>define</span> <span class=variable>print-form</span> (<span class=keyword>lambda</span> () (<span class=variable>display</span> <span class=selfeval>&quot;&lt;form action=\&quot;&quot;</span>) (<span class=variable>display</span> (<span class=variable>getenv</span> <span class=selfeval>&quot;SCRIPT_NAME&quot;</span>)) (<span class=variable>display</span> <span class=selfeval>&quot;\&quot;&gt; Enter arithmetic expression:&lt;br&gt; &lt;input type=textarea name=arithexp&gt;&lt;p&gt; &lt;input type=submit value=\&quot;Evaluate\&quot;&gt; &lt;input type=reset value=\&quot;Clear\&quot;&gt; &lt;/form&gt;&quot;</span>))) (<span class=keyword>define</span> <span class=variable>print-page-begin</span> (<span class=keyword>lambda</span> () (<span class=variable>display</span> <span class=selfeval>&quot;content-type: text/html &lt;html&gt; &lt;head&gt; &lt;title&gt;A Scheme Calculator&lt;/title&gt; &lt;/head&gt; &lt;body&gt;&quot;</span>))) (<span class=keyword>define</span> <span class=variable>print-page-end</span> (<span class=keyword>lambda</span> () (<span class=variable>display</span> <span class=selfeval>&quot;&lt;/body&gt; &lt;/html&gt;&quot;</span>))) (<span class=variable>parse-form-data</span>) (<span class=variable>print-page-begin</span>) (<span class=keyword>let</span> ((<span class=variable>e</span> (<span class=variable>form-data-get</span> <span class=selfeval>&quot;arithexp&quot;</span>))) (<span class=keyword>unless</span> (<span class=variable>null?</span> <span class=variable>e</span>) (<span class=keyword>let</span> ((<span class=variable>e1</span> (<span class=variable>car</span> <span class=variable>e</span>))) (<span class=variable>display-html</span> <span class=variable>e1</span>) (<span class=variable>display</span> <span class=selfeval>&quot;&lt;p&gt; =&amp;gt;&amp;nbsp;&amp;nbsp;&quot;</span>) (<span class=variable>display-html</span> (<span class=variable>call/cc</span> (<span class=keyword>lambda</span> (<span class=variable>k</span>) (<span class=keyword>set!</span> <span class=variable>uhoh</span> (<span class=keyword>lambda</span> (<span class=variable>s</span>) (<span class=variable>k</span> (<span class=variable>string-append</span> <span class=selfeval>&quot;Error: &quot;</span> <span class=variable>s</span>)))) (<span class=variable>number-&gt;string</span> (<span class=variable>calc-eval</span> (<span class=variable>read</span> (<span class=variable>open-input-string</span> (<span class=variable>car</span> <span class=variable>e</span>)))))))) (<span class=variable>display</span> <span class=selfeval>&quot;&lt;p&gt;&quot;</span>)))) (<span class=variable>print-form</span>) (<span class=variable>print-page-end</span>) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-20.html'>Chapter 18</a></h2> <pre class=scheme>(<span class=variable>require</span> (<span class=variable>lib</span> <span class=selfeval>&quot;defmacro.ss&quot;</span>)) <span class=comment>;provides define-macro</span> (<span class=variable>require</span> (<span class=variable>lib</span> <span class=selfeval>&quot;process.ss&quot;</span>)) <span class=comment>;provides system</span> </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>file-or-directory-modify-seconds</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=variable>vector-ref</span> (<span class=variable>stat</span> <span class=variable>f</span>) <span class=selfeval>9</span>))) </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>;exec guile -s $0 &quot;$@&quot;</span> </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>; exec gosh -- $0 &quot;$@&quot;</span> </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>;exec scm -l $0 &quot;$@&quot;</span> </pre> <pre class=scheme><span class=selfeval>&quot;:&quot;</span><span class=comment>;exec snow -f $0 &quot;$@&quot;</span> </pre> <pre class=scheme>(<span class=keyword>define-macro</span> <span class=variable>MACRO-NAME</span> (<span class=keyword>lambda</span> <span class=variable>MACRO-ARGS</span> <span class=variable>MACRO-BODY</span> ...)) </pre> <pre class=scheme>(<span class=keyword>define-syntax</span> <span class=variable>MACRO-NAME</span> (<span class=variable>rsc-macro-transformer</span> (<span class=keyword>let</span> ((<span class=variable>xfmr</span> (<span class=keyword>lambda</span> <span class=variable>MACRO-ARGS</span> <span class=variable>MACRO-BODY</span> ...))) (<span class=keyword>lambda</span> (<span class=variable>e</span> <span class=variable>r</span>) (<span class=variable>apply</span> <span class=variable>xfmr</span> (<span class=variable>cdr</span> <span class=variable>e</span>)))))) </pre> <pre class=scheme>(<span class=variable>syntax-table-define</span> <span class=variable>system-global-syntax-table</span> <span class=keyword>'</span><span class=variable>MACRO-NAME</span> (<span class=variable>macro</span> <span class=variable>MACRO-ARGS</span> <span class=variable>MACRO-BODY</span> ...)) </pre> <pre class=scheme>(<span class=keyword>defmacro</span> <span class=variable>MACRO-NAME</span> <span class=variable>MACRO-ARGS</span> <span class=variable>MACRO-BODY</span> ...) </pre> <pre class=scheme>(<span class=keyword>define-macro</span> (<span class=variable>MACRO-NAME</span> . <span class=variable>MACRO-ARGS</span>) <span class=variable>MACRO-BODY</span> ...) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>load-relative</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=keyword>let*</span> ((<span class=variable>n</span> (<span class=variable>string-length</span> <span class=variable>f</span>)) (<span class=variable>full-pathname?</span> (<span class=keyword>and</span> (<span class=variable>&gt;</span> <span class=variable>n</span> <span class=selfeval>0</span>) (<span class=keyword>let</span> ((<span class=variable>c0</span> (<span class=variable>string-ref</span> <span class=variable>f</span> <span class=selfeval>0</span>))) (<span class=keyword>or</span> (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\/</span>) (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\~</span>)))))) (<span class=variable>basic-load</span> (<span class=keyword>if</span> <span class=variable>full-pathname?</span> <span class=variable>f</span> (<span class=keyword>let</span> ((<span class=variable>clp</span> (<span class=variable>current-load-port</span>))) (<span class=keyword>if</span> <span class=variable>clp</span> (<span class=variable>string-append</span> (<span class=variable>dirname</span> (<span class=variable>port-filename</span> <span class=variable>clp</span>)) <span class=selfeval>&quot;/&quot;</span> <span class=variable>f</span>) <span class=variable>f</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>load-relative</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=keyword>let*</span> ((<span class=variable>n</span> (<span class=variable>string-length</span> <span class=variable>f</span>)) (<span class=variable>full-pathname?</span> (<span class=keyword>and</span> (<span class=variable>&gt;</span> <span class=variable>n</span> <span class=selfeval>0</span>) (<span class=keyword>let</span> ((<span class=variable>c0</span> (<span class=variable>string-ref</span> <span class=variable>f</span> <span class=selfeval>0</span>))) (<span class=keyword>or</span> (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\/</span>) (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\~</span>)))))) (<span class=variable>load</span> (<span class=keyword>if</span> (<span class=keyword>and</span> <span class=global>*load-pathname*</span> <span class=variable>full-pathname?</span>) (<span class=variable>in-vicinity</span> (<span class=variable>program-vicinity</span>) <span class=variable>f</span>) <span class=variable>f</span>))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=global>*load-pathname*</span> <span class=selfeval>#f</span>) (<span class=keyword>define</span> <span class=variable>stk%load</span> <span class=variable>load</span>) (<span class=keyword>define</span> <span class=variable>load-relative</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=keyword>fluid-let</span> ((<span class=global>*load-pathname*</span> (<span class=keyword>if</span> (<span class=variable>not</span> <span class=global>*load-pathname*</span>) <span class=variable>f</span> (<span class=keyword>let*</span> ((<span class=variable>n</span> (<span class=variable>string-length</span> <span class=variable>f</span>)) (<span class=variable>full-pathname?</span> (<span class=keyword>and</span> (<span class=variable>&gt;</span> <span class=variable>n</span> <span class=selfeval>0</span>) (<span class=keyword>let</span> ((<span class=variable>c0</span> (<span class=variable>string-ref</span> <span class=variable>f</span> <span class=selfeval>0</span>))) (<span class=keyword>or</span> (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\/</span>) (<span class=variable>char=?</span> <span class=variable>c0</span> <span class=selfeval>#\~</span>)))))) (<span class=keyword>if</span> <span class=variable>full-pathname?</span> <span class=variable>f</span> (<span class=variable>string-append</span> (<span class=variable>dirname</span> <span class=global>*load-pathname*</span>) <span class=selfeval>&quot;/&quot;</span> <span class=variable>f</span>)))))) (<span class=variable>stk%load</span> <span class=global>*load-pathname*</span>)))) (<span class=keyword>define</span> <span class=variable>load</span> (<span class=keyword>lambda</span> (<span class=variable>f</span>) (<span class=variable>error</span> <span class=selfeval>&quot;Don't use load. Use load-relative instead.&quot;</span>))) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-21.html'>Chapter 19</a></h2> <pre class=verbatim>echo Hello, World!&nbsp; </pre> <pre class=verbatim>hello.bat&nbsp; </pre> <pre class=verbatim>hello&nbsp; </pre> <pre class=verbatim>;@echo off&nbsp; ;goto :start&nbsp; #|&nbsp; :start&nbsp; echo. &gt; c:\_temp.scm&nbsp; echo (load (find-executable-path &quot;hello.bat&quot; &gt;&gt; c:\_temp.scm&nbsp; echo &quot;hello.bat&quot;)) &gt;&gt; c:\_temp.scm&nbsp; mzscheme -r c:\_temp.scm %1 %2 %3 %4 %5 %6 %7 %8 %9&nbsp; goto :eof&nbsp; |#&nbsp; &nbsp; (display &quot;Hello, World!&quot;)&nbsp; (newline)&nbsp; &nbsp; ;:eof&nbsp; </pre> <pre class=scheme>(<span class=variable>load</span> (<span class=variable>find-executable-path</span> <span class=selfeval>&quot;hello.bat&quot;</span> <span class=selfeval>&quot;hello.bat&quot;</span>)) </pre> <pre class=verbatim>Hello, World!&nbsp; </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-22.html'>Chapter 20</a></h2> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>integrate-simpson</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>n</span>) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=keyword>unless</span> (<span class=variable>even?</span> <span class=variable>n</span>) (<span class=keyword>set!</span> <span class=variable>n</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>1</span>))) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=keyword>let*</span> ((<span class=variable>h</span> (<span class=variable>/</span> (<span class=variable>-</span> <span class=variable>b</span> <span class=variable>a</span>) <span class=variable>n</span>)) (<span class=variable>h*2</span> (<span class=variable>*</span> <span class=variable>h</span> <span class=selfeval>2</span>)) (<span class=variable>n/2</span> (<span class=variable>/</span> <span class=variable>n</span> <span class=selfeval>2</span>)) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=variable>sum-every-other-ordinate-starting-from</span> (<span class=keyword>lambda</span> (<span class=variable>x0</span> <span class=variable>num-ordinates</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>x</span> <span class=variable>x0</span>) (<span class=variable>i</span> <span class=selfeval>0</span>) (<span class=variable>r</span> <span class=selfeval>0</span>)) (<span class=keyword>if</span> (<span class=variable>&gt;=</span> <span class=variable>i</span> <span class=variable>num-ordinates</span>) <span class=variable>r</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>x</span> <span class=variable>h*2</span>) (<span class=variable>+</span> <span class=variable>i</span> <span class=selfeval>1</span>) (<span class=variable>+</span> <span class=variable>r</span> (<span class=variable>f</span> <span class=variable>x</span>))))))) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=variable>y0+yn</span> (<span class=variable>+</span> (<span class=variable>f</span> <span class=variable>a</span>) (<span class=variable>f</span> <span class=variable>b</span>))) (<span class=variable>y1+y3+...+y.n-1</span> (<span class=variable>sum-every-other-ordinate-starting-from</span> (<span class=variable>+</span> <span class=variable>a</span> <span class=variable>h</span>) <span class=variable>n/2</span>)) (<span class=variable>y2+y4+...+y.n-2</span> (<span class=variable>sum-every-other-ordinate-starting-from</span> (<span class=variable>+</span> <span class=variable>a</span> <span class=variable>h*2</span>) (<span class=variable>-</span> <span class=variable>n/2</span> <span class=selfeval>1</span>)))) (<span class=variable>*</span> <span class=selfeval>1/3</span> <span class=variable>h</span> (<span class=variable>+</span> <span class=variable>y0+yn</span> (<span class=variable>*</span> <span class=selfeval>4.0</span> <span class=variable>y1+y3+...+y.n-1</span>) (<span class=variable>*</span> <span class=selfeval>2.0</span> <span class=variable>y2+y4+...+y.n-2</span>)))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=global>*pi*</span> (<span class=variable>*</span> <span class=selfeval>4</span> (<span class=variable>atan</span> <span class=selfeval>1</span>))) (<span class=keyword>define</span> <span class=variable>phi</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>*</span> (<span class=variable>/</span> <span class=selfeval>1</span> (<span class=variable>sqrt</span> (<span class=variable>*</span> <span class=selfeval>2</span> <span class=global>*pi*</span>))) (<span class=variable>exp</span> (<span class=variable>-</span> (<span class=variable>*</span> <span class=selfeval>1/2</span> (<span class=variable>*</span> <span class=variable>x</span> <span class=variable>x</span>))))))) </pre> <pre class=scheme>(<span class=variable>integrate-simpson</span> <span class=variable>phi</span> <span class=selfeval>0</span> <span class=selfeval>1</span> <span class=selfeval>10</span>) (<span class=variable>integrate-simpson</span> <span class=variable>phi</span> <span class=selfeval>0</span> <span class=selfeval>2</span> <span class=selfeval>10</span>) (<span class=variable>integrate-simpson</span> <span class=variable>phi</span> <span class=selfeval>0</span> <span class=selfeval>3</span> <span class=selfeval>10</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>integrate-adaptive-simpson-first-try</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>e</span>) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>n</span> <span class=selfeval>4</span>) (<span class=variable>iprev</span> (<span class=variable>integrate-simpson</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=selfeval>2</span>))) (<span class=keyword>let</span> ((<span class=variable>icurr</span> (<span class=variable>integrate-simpson</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>n</span>))) (<span class=keyword>if</span> (<span class=variable>&lt;=</span> (<span class=variable>abs</span> (<span class=variable>-</span> <span class=variable>icurr</span> <span class=variable>iprev</span>)) <span class=variable>e</span>) <span class=variable>icurr</span> (<span class=variable>loop</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>2</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>integrate-adaptive-simpson-second-try</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>e</span>) (<span class=keyword>let</span> <span class=variable>integrate-segment</span> ((<span class=variable>a</span> <span class=variable>a</span>) (<span class=variable>b</span> <span class=variable>b</span>) (<span class=variable>e</span> <span class=variable>e</span>)) (<span class=keyword>let</span> ((<span class=variable>i2</span> (<span class=variable>integrate-simpson</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=selfeval>2</span>)) (<span class=variable>i4</span> (<span class=variable>integrate-simpson</span> <span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=selfeval>4</span>))) (<span class=keyword>if</span> (<span class=variable>&lt;=</span> (<span class=variable>abs</span> (<span class=variable>-</span> <span class=variable>i2</span> <span class=variable>i4</span>)) <span class=variable>e</span>) <span class=variable>i4</span> (<span class=keyword>let</span> ((<span class=variable>c</span> (<span class=variable>/</span> (<span class=variable>+</span> <span class=variable>a</span> <span class=variable>b</span>) <span class=selfeval>2</span>)) (<span class=variable>e</span> (<span class=variable>/</span> <span class=variable>e</span> <span class=selfeval>2</span>))) (<span class=variable>+</span> (<span class=variable>integrate-segment</span> <span class=variable>a</span> <span class=variable>c</span> <span class=variable>e</span>) (<span class=variable>integrate-segment</span> <span class=variable>c</span> <span class=variable>b</span> <span class=variable>e</span>)))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>integrate-adaptive-simpson</span> (<span class=keyword>lambda</span> (<span class=variable>f</span> <span class=variable>a</span> <span class=variable>b</span> <span class=variable>e</span>) (<span class=keyword>let*</span> ((<span class=variable>h</span> (<span class=variable>/</span> (<span class=variable>-</span> <span class=variable>b</span> <span class=variable>a</span>) <span class=selfeval>4</span>)) (<span class=variable>mid.a.b</span> (<span class=variable>+</span> <span class=variable>a</span> (<span class=variable>*</span> <span class=selfeval>2</span> <span class=variable>h</span>)))) (<span class=keyword>let</span> <span class=variable>integrate-segment</span> ((<span class=variable>x0</span> <span class=variable>a</span>) (<span class=variable>x2</span> <span class=variable>mid.a.b</span>) (<span class=variable>x4</span> <span class=variable>b</span>) (<span class=variable>y0</span> (<span class=variable>f</span> <span class=variable>a</span>)) (<span class=variable>y2</span> (<span class=variable>f</span> <span class=variable>mid.a.b</span>)) (<span class=variable>y4</span> (<span class=variable>f</span> <span class=variable>b</span>)) (<span class=variable>h</span> <span class=variable>h</span>) (<span class=variable>e</span> <span class=variable>e</span>)) (<span class=keyword>let*</span> ((<span class=variable>x1</span> (<span class=variable>+</span> <span class=variable>x0</span> <span class=variable>h</span>)) (<span class=variable>x3</span> (<span class=variable>+</span> <span class=variable>x2</span> <span class=variable>h</span>)) (<span class=variable>y1</span> (<span class=variable>f</span> <span class=variable>x1</span>)) (<span class=variable>y3</span> (<span class=variable>f</span> <span class=variable>x3</span>)) (<span class=variable>i2</span> (<span class=variable>*</span> <span class=selfeval>2/3</span> <span class=variable>h</span> (<span class=variable>+</span> <span class=variable>y0</span> <span class=variable>y4</span> (<span class=variable>*</span> <span class=selfeval>4.0</span> <span class=variable>y2</span>)))) (<span class=variable>i4</span> (<span class=variable>*</span> <span class=selfeval>1/3</span> <span class=variable>h</span> (<span class=variable>+</span> <span class=variable>y0</span> <span class=variable>y4</span> (<span class=variable>*</span> <span class=selfeval>4.0</span> (<span class=variable>+</span> <span class=variable>y1</span> <span class=variable>y3</span>)) (<span class=variable>*</span> <span class=selfeval>2.0</span> <span class=variable>y2</span>))))) (<span class=keyword>if</span> (<span class=variable>&lt;=</span> (<span class=variable>abs</span> (<span class=variable>-</span> <span class=variable>i2</span> <span class=variable>i4</span>)) <span class=variable>e</span>) <span class=variable>i4</span> (<span class=keyword>let</span> ((<span class=variable>h</span> (<span class=variable>/</span> <span class=variable>h</span> <span class=selfeval>2</span>)) (<span class=variable>e</span> (<span class=variable>/</span> <span class=variable>e</span> <span class=selfeval>2</span>))) (<span class=variable>+</span> (<span class=variable>integrate-segment</span> <span class=variable>x0</span> <span class=variable>x1</span> <span class=variable>x2</span> <span class=variable>y0</span> <span class=variable>y1</span> <span class=variable>y2</span> <span class=variable>h</span> <span class=variable>e</span>) (<span class=variable>integrate-segment</span> <span class=variable>x2</span> <span class=variable>x3</span> <span class=variable>x4</span> <span class=variable>y2</span> <span class=variable>y3</span> <span class=variable>y4</span> <span class=variable>h</span> <span class=variable>e</span>))))))))) </pre> <pre class=scheme>(<span class=variable>integrate-simpson</span> <span class=variable>exp</span> <span class=selfeval>0</span> <span class=selfeval>20</span> <span class=selfeval>10</span>) (<span class=variable>integrate-simpson</span> <span class=variable>exp</span> <span class=selfeval>0</span> <span class=selfeval>20</span> <span class=selfeval>20</span>) (<span class=variable>integrate-simpson</span> <span class=variable>exp</span> <span class=selfeval>0</span> <span class=selfeval>20</span> <span class=selfeval>40</span>) (<span class=variable>integrate-adaptive-simpson</span> <span class=variable>exp</span> <span class=selfeval>0</span> <span class=selfeval>20</span> <span class=selfeval>.001</span>) (<span class=variable>-</span> (<span class=variable>exp</span> <span class=selfeval>20</span>) <span class=selfeval>1</span>) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>gamma-1-to-2</span> (<span class=keyword>lambda</span> (<span class=variable>n</span> <span class=variable>e</span>) (<span class=keyword>unless</span> (<span class=variable>&lt;</span> <span class=selfeval>1</span> <span class=variable>n</span> <span class=selfeval>2</span>) (<span class=variable>error</span> <span class=keyword>'</span><span class=variable>gamma-1-to-2</span> <span class=selfeval>&quot;argument outside (1, 2)&quot;</span>)) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=keyword>let</span> ((<span class=variable>gamma-integrand</span> (<span class=keyword>let</span> ((<span class=variable>n-1</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))) (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>*</span> (<span class=variable>expt</span> <span class=variable>x</span> <span class=variable>n-1</span>) (<span class=variable>exp</span> (<span class=variable>-</span> <span class=variable>x</span>)))))) <span class=comment>;...</span> </pre> <pre class=scheme> <span class=comment>;...</span> (<span class=variable>e/100</span> (<span class=variable>/</span> <span class=variable>e</span> <span class=selfeval>100</span>))) (<span class=keyword>let</span> <span class=variable>loop</span> ((<span class=variable>xc</span> <span class=selfeval>4</span>) (<span class=variable>yc</span> (<span class=variable>gamma-integrand</span> <span class=selfeval>4</span>))) (<span class=keyword>let*</span> ((<span class=variable>tail-integrand</span> (<span class=keyword>lambda</span> (<span class=variable>x</span>) (<span class=variable>*</span> <span class=variable>yc</span> (<span class=variable>exp</span> (<span class=variable>-</span> (<span class=variable>-</span> <span class=variable>x</span> <span class=variable>xc</span>)))))) (<span class=variable>x1</span> (<span class=variable>*</span> <span class=selfeval>2</span> <span class=variable>xc</span>)) (<span class=variable>y1</span> (<span class=variable>gamma-integrand</span> <span class=variable>x1</span>)) (<span class=variable>y1-estimated</span> (<span class=variable>tail-integrand</span> <span class=variable>x1</span>))) (<span class=keyword>if</span> (<span class=variable>&lt;=</span> (<span class=variable>abs</span> (<span class=variable>-</span> <span class=variable>y1</span> <span class=variable>y1-estimated</span>)) <span class=variable>e/100</span>) (<span class=variable>+</span> (<span class=variable>integrate-adaptive-simpson</span> <span class=variable>gamma-integrand</span> <span class=selfeval>0</span> <span class=variable>xc</span> <span class=variable>e/100</span>) <span class=variable>yc</span>) (<span class=variable>loop</span> <span class=variable>x1</span> <span class=variable>y1</span>))))))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>gamma</span> (<span class=keyword>lambda</span> (<span class=variable>n</span> <span class=variable>e</span>) (<span class=keyword>cond</span> ((<span class=variable>&lt;</span> <span class=variable>n</span> <span class=selfeval>1</span>) (<span class=variable>/</span> (<span class=variable>gamma</span> (<span class=variable>+</span> <span class=variable>n</span> <span class=selfeval>1</span>) <span class=variable>e</span>) <span class=variable>n</span>)) ((<span class=variable>=</span> <span class=variable>n</span> <span class=selfeval>1</span>) <span class=selfeval>1</span>) ((<span class=variable>&lt;</span> <span class=selfeval>1</span> <span class=variable>n</span> <span class=selfeval>2</span>) (<span class=variable>gamma-1-to-2</span> <span class=variable>n</span> <span class=variable>e</span>)) (<span class=keyword>else</span> (<span class=keyword>let</span> ((<span class=variable>n-1</span> (<span class=variable>-</span> <span class=variable>n</span> <span class=selfeval>1</span>))) (<span class=variable>*</span> <span class=variable>n-1</span> (<span class=variable>gamma</span> <span class=variable>n-1</span> <span class=variable>e</span>))))))) </pre> <pre class=scheme>(<span class=variable>gamma</span> <span class=selfeval>3/2</span> <span class=selfeval>.001</span>) (<span class=variable>*</span> <span class=selfeval>1/2</span> (<span class=variable>sqrt</span> <span class=global>*pi*</span>)) </pre> <h2><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme-Z-H-23.html'>Chapter 21</a></h2> <pre class=scheme>(<span class=variable>sigaction</span> <span class=variable>SIGALRM</span> (<span class=keyword>lambda</span> (<span class=variable>sig</span>) (<span class=variable>display</span> <span class=selfeval>&quot;Signal &quot;</span>) (<span class=variable>display</span> <span class=variable>sig</span>) (<span class=variable>display</span> <span class=selfeval>&quot; raised. Continuing...&quot;</span>) (<span class=variable>newline</span>))) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=global>*infinity*</span> (<span class=variable>/</span> <span class=selfeval>1</span> <span class=selfeval>0</span>)) </pre> <pre class=scheme>(<span class=keyword>define</span> <span class=variable>clock</span> (<span class=keyword>let</span> ((<span class=variable>stopped?</span> <span class=selfeval>#t</span>) (<span class=variable>clock-interrupt-handler</span> (<span class=keyword>lambda</span> () (<span class=variable>error</span> <span class=selfeval>&quot;Clock interrupt!&quot;</span>)))) (<span class=keyword>let</span> ((<span class=variable>generate-clock-interrupt</span> (<span class=keyword>lambda</span> () (<span class=keyword>set!</span> <span class=variable>stopped?</span> <span class=selfeval>#t</span>) (<span class=variable>clock-interrupt-handler</span>)))) (<span class=variable>sigaction</span> <span class=variable>SIGALRM</span> (<span class=keyword>lambda</span> (<span class=variable>sig</span>) (<span class=variable>generate-clock-interrupt</span>))) (<span class=keyword>lambda</span> (<span class=variable>msg</span> <span class=variable>val</span>) (<span class=keyword>case</span> <span class=variable>msg</span> ((<span class=variable>set-handler</span>) (<span class=keyword>set!</span> <span class=variable>clock-interrupt-handler</span> <span class=variable>val</span>)) ((<span class=variable>set</span>) (<span class=keyword>cond</span> ((<span class=variable>=</span> <span class=variable>val</span> <span class=global>*infinity*</span>) <span class=comment>;This is equivalent to stopping the clock.</span> <span class=comment>;This is almost equivalent to (alarm 0), except</span> <span class=comment>;that if the clock is already stopped,</span> <span class=comment>;return *infinity*.</span> (<span class=keyword>let</span> ((<span class=variable>time-remaining</span> (<span class=variable>alarm</span> <span class=selfeval>0</span>))) (<span class=keyword>if</span> <span class=variable>stopped?</span> <span class=global>*infinity*</span> (<span class=keyword>begin</span> (<span class=keyword>set!</span> <span class=variable>stopped?</span> <span class=selfeval>#t</span>) <span class=variable>time-remaining</span>)))) ((<span class=variable>=</span> <span class=variable>val</span> <span class=selfeval>0</span>) <span class=comment>;This is equivalent to setting the alarm to</span> <span class=comment>;go off immediately. This is almost equivalent</span> <span class=comment>;to (alarm 0), except you force the alarm</span> <span class=comment>;handler to run.</span> (<span class=keyword>let</span> ((<span class=variable>time-remaining</span> (<span class=variable>alarm</span> <span class=selfeval>0</span>))) (<span class=keyword>if</span> <span class=variable>stopped?</span> (<span class=keyword>begin</span> (<span class=variable>generate-clock-interrupt</span>) <span class=global>*infinity*</span>) (<span class=keyword>begin</span> (<span class=variable>generate-clock-interrupt</span>) <span class=variable>time-remaining</span>)))) (<span class=keyword>else</span> <span class=comment>;This is equivalent to (alarm n) for n != 0.</span> <span class=comment>;Just remember to return *infinity* if the</span> <span class=comment>;clock was previously quiescent.</span> (<span class=keyword>let</span> ((<span class=variable>time-remaining</span> (<span class=variable>alarm</span> <span class=variable>val</span>))) (<span class=keyword>if</span> <span class=variable>stopped?</span> (<span class=keyword>begin</span> (<span class=keyword>set!</span> <span class=variable>stopped?</span> <span class=selfeval>#f</span>) <span class=global>*infinity*</span>) <span class=variable>time-remaining</span>)))))))))) </pre> <p><a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html'>Teach Yourself Scheme in <code>Fixnum</code> Days</a> -- Just The Code Samples.</p> <p>Honestly, <a href='http://www.ccs.neu.edu/home/dorai/t-y-scheme/t-y-scheme.html'>go read it</a>, it's a whole book from which i've <a href=''>snaffled Just The Code Samples</a></p> </body> </html>