Minor docs cleanup.

This commit is contained in:
Simon Forman 2018-06-06 11:20:31 -07:00
parent 0de5029c98
commit 637d4efa6a
13 changed files with 373 additions and 393 deletions

View File

@ -11799,10 +11799,16 @@ div#notebook {
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<p>Consider the <code>x</code> combinator:</p> <p>Consider the <code>x</code> combinator:</p>
<pre><code>x == dup i <pre><code>x == dup i</code></pre>
</code></pre> </div>
<p>We can apply it to a quoted program consisting of some value <code>a</code> and a function <code>B</code>:</p> </div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div>
<div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html">
<p>We can apply it to a quoted program consisting of some value <code>a</code> and some function <code>B</code>:</p>
<pre><code>[a B] x <pre><code>[a B] x
[a B] a B</code></pre> [a B] a B</code></pre>
@ -11844,7 +11850,7 @@ b [B] cons
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<p>Putting it together, this is the definition of <code>B</code>:</p> <p>Altogether, this is the definition of <code>B</code>:</p>
<pre><code>B == swap [C] dip rest cons</code></pre> <pre><code>B == swap [C] dip rest cons</code></pre>
@ -11855,7 +11861,7 @@ b [B] cons
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<p>We can create a quoted program that generates the Natural numbers (0, 1, 2, ...) by using <code>0</code> for <code>a</code> and <code>[dup ++]</code> for <code>[C]</code>:</p> <p>We can make a generator for the Natural numbers (0, 1, 2, ...) by using <code>0</code> for <code>a</code> and <code>[dup ++]</code> for <code>[C]</code>:</p>
<pre><code>[0 swap [dup ++] dip rest cons] <pre><code>[0 swap [dup ++] dip rest cons]
@ -11951,7 +11957,7 @@ b [B] cons
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h3 id="direco"><code>direco</code><a class="anchor-link" href="#direco">&#182;</a></h3> <h2 id="direco"><code>direco</code><a class="anchor-link" href="#direco">&#182;</a></h2>
</div> </div>
</div> </div>
</div> </div>
@ -12015,7 +12021,7 @@ b [B] cons
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Making-Generators">Making Generators<a class="anchor-link" href="#Making-Generators">&#182;</a></h1><p>We want to define a function that accepts <code>a</code> and <code>[C]</code> and builds our quoted program:</p> <h2 id="Making-Generators">Making Generators<a class="anchor-link" href="#Making-Generators">&#182;</a></h2><p>We want to define a function that accepts <code>a</code> and <code>[C]</code> and builds our quoted program:</p>
<pre><code> a [C] G <pre><code> a [C] G
------------------------- -------------------------
@ -12212,7 +12218,7 @@ G == [direco] cons [swap] swoncat cons</code></pre>
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Generating-Multiples-of-Three-and-Five">Generating Multiples of Three and Five<a class="anchor-link" href="#Generating-Multiples-of-Three-and-Five">&#182;</a></h1><p>Look at the treatment of the Project Euler Problem One in <a href="./Developing a Program.ipynb">Developing a Program.ipynb</a> and you'll see that we might be interested in generating an endless cycle of:</p> <h2 id="Generating-Multiples-of-Three-and-Five">Generating Multiples of Three and Five<a class="anchor-link" href="#Generating-Multiples-of-Three-and-Five">&#182;</a></h2><p>Look at the treatment of the Project Euler Problem One in <a href="./Developing a Program.ipynb">Developing a Program.ipynb</a> and you'll see that we might be interested in generating an endless cycle of:</p>
<pre><code>3 2 1 3 1 2 3 <pre><code>3 2 1 3 1 2 3
@ -12570,7 +12576,7 @@ G == [direco] cons [swap] swoncat cons</code></pre>
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="Project-Euler-Problem-One">Project Euler Problem One<a class="anchor-link" href="#Project-Euler-Problem-One">&#182;</a></h1> <h2 id="Project-Euler-Problem-One">Project Euler Problem One<a class="anchor-link" href="#Project-Euler-Problem-One">&#182;</a></h2>
</div> </div>
</div> </div>
</div> </div>
@ -12631,7 +12637,7 @@ G == [direco] cons [swap] swoncat cons</code></pre>
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="A-generator-for-the-Fibonacci-Sequence.">A generator for the Fibonacci Sequence.<a class="anchor-link" href="#A-generator-for-the-Fibonacci-Sequence.">&#182;</a></h1><p>Consider:</p> <h2 id="A-generator-for-the-Fibonacci-Sequence.">A generator for the Fibonacci Sequence.<a class="anchor-link" href="#A-generator-for-the-Fibonacci-Sequence.">&#182;</a></h2><p>Consider:</p>
<pre><code>[b a F] x <pre><code>[b a F] x
[b a F] b a F</code></pre> [b a F] b a F</code></pre>
@ -12818,7 +12824,7 @@ fib_gen == [1 1 F]</code></pre>
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h3 id="Project-Euler-Problem-Two">Project Euler Problem Two<a class="anchor-link" href="#Project-Euler-Problem-Two">&#182;</a></h3> <h2 id="Project-Euler-Problem-Two">Project Euler Problem Two<a class="anchor-link" href="#Project-Euler-Problem-Two">&#182;</a></h2>
<pre><code>By considering the terms in the Fibonacci sequence whose values do not exceed four million, <pre><code>By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms. find the sum of the even-valued terms.
@ -13115,7 +13121,7 @@ o + e = o
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="How-to-compile-these?">How to compile these?<a class="anchor-link" href="#How-to-compile-these?">&#182;</a></h1><p>You would probably start with a special version of <code>G</code>, and perhaps modifications to the default <code>x</code>?</p> <h2 id="How-to-compile-these?">How to compile these?<a class="anchor-link" href="#How-to-compile-these?">&#182;</a></h2><p>You would probably start with a special version of <code>G</code>, and perhaps modifications to the default <code>x</code>?</p>
</div> </div>
</div> </div>
@ -13124,7 +13130,7 @@ o + e = o
</div> </div>
<div class="inner_cell"> <div class="inner_cell">
<div class="text_cell_render border-box-sizing rendered_html"> <div class="text_cell_render border-box-sizing rendered_html">
<h1 id="An-Interesting-Variation">An Interesting Variation<a class="anchor-link" href="#An-Interesting-Variation">&#182;</a></h1> <h2 id="An-Interesting-Variation">An Interesting Variation<a class="anchor-link" href="#An-Interesting-Variation">&#182;</a></h2>
</div> </div>
</div> </div>
</div> </div>

View File

@ -24,9 +24,14 @@
"source": [ "source": [
"Consider the `x` combinator:\n", "Consider the `x` combinator:\n",
"\n", "\n",
" x == dup i\n", " x == dup i"
"\n", ]
"We can apply it to a quoted program consisting of some value `a` and a function `B`:\n", },
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can apply it to a quoted program consisting of some value `a` and some function `B`:\n",
"\n", "\n",
" [a B] x\n", " [a B] x\n",
" [a B] a B" " [a B] a B"
@ -62,7 +67,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"Putting it together, this is the definition of `B`:\n", "Altogether, this is the definition of `B`:\n",
"\n", "\n",
" B == swap [C] dip rest cons" " B == swap [C] dip rest cons"
] ]
@ -71,7 +76,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"We can create a quoted program that generates the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`:\n", "We can make a generator for the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`:\n",
"\n", "\n",
" [0 swap [dup ++] dip rest cons]\n", " [0 swap [dup ++] dip rest cons]\n",
"\n", "\n",
@ -136,7 +141,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### `direco`" "## `direco`"
] ]
}, },
{ {
@ -181,7 +186,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Making Generators\n", "## Making Generators\n",
"We want to define a function that accepts `a` and `[C]` and builds our quoted program:\n", "We want to define a function that accepts `a` and `[C]` and builds our quoted program:\n",
"\n", "\n",
" a [C] G\n", " a [C] G\n",
@ -310,8 +315,8 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Generating Multiples of Three and Five\n", "## Generating Multiples of Three and Five\n",
"Look at the treatment of the Project Euler Problem One in [Developing a Program.ipynb](./Developing a Program.ipynb) and you'll see that we might be interested in generating an endless cycle of:\n", "Look at the treatment of the Project Euler Problem One in the \"Developing a Program\" notebook and you'll see that we might be interested in generating an endless cycle of:\n",
"\n", "\n",
" 3 2 1 3 1 2 3\n", " 3 2 1 3 1 2 3\n",
"\n", "\n",
@ -541,7 +546,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# Project Euler Problem One" "## Project Euler Problem One"
] ]
}, },
{ {
@ -581,7 +586,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# A generator for the Fibonacci Sequence.\n", "## A generator for the Fibonacci Sequence.\n",
"Consider:\n", "Consider:\n",
"\n", "\n",
" [b a F] x\n", " [b a F] x\n",
@ -725,7 +730,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"### Project Euler Problem Two\n", "## Project Euler Problem Two\n",
" By considering the terms in the Fibonacci sequence whose values do not exceed four million,\n", " By considering the terms in the Fibonacci sequence whose values do not exceed four million,\n",
" find the sum of the even-valued terms.\n", " find the sum of the even-valued terms.\n",
"\n", "\n",
@ -921,7 +926,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# How to compile these?\n", "## How to compile these?\n",
"You would probably start with a special version of `G`, and perhaps modifications to the default `x`?" "You would probably start with a special version of `G`, and perhaps modifications to the default `x`?"
] ]
}, },
@ -929,7 +934,7 @@
"cell_type": "markdown", "cell_type": "markdown",
"metadata": {}, "metadata": {},
"source": [ "source": [
"# An Interesting Variation" "## An Interesting Variation"
] ]
}, },
{ {

View File

@ -12,7 +12,7 @@ Consider the `x` combinator:
x == dup i x == dup i
We can apply it to a quoted program consisting of some value `a` and a function `B`: We can apply it to a quoted program consisting of some value `a` and some function `B`:
[a B] x [a B] x
[a B] a B [a B] a B
@ -33,11 +33,11 @@ Now discard the quoted `a` with `rest` then `cons` `b`:
b [B] cons b [B] cons
[b B] [b B]
Putting it together, this is the definition of `B`: Altogether, this is the definition of `B`:
B == swap [C] dip rest cons B == swap [C] dip rest cons
We can create a quoted program that generates the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`: We can make a generator for the Natural numbers (0, 1, 2, ...) by using `0` for `a` and `[dup ++]` for `[C]`:
[0 swap [dup ++] dip rest cons] [0 swap [dup ++] dip rest cons]
@ -72,7 +72,7 @@ J('[0 swap [dup ++] dip rest cons] x x x x x pop')
0 1 2 3 4 0 1 2 3 4
### `direco` ## `direco`
```python ```python
@ -99,7 +99,7 @@ V('[0 swap [dup ++] direco] x')
0 [1 swap [dup ++] direco] . 0 [1 swap [dup ++] direco] .
# Making Generators ## Making Generators
We want to define a function that accepts `a` and `[C]` and builds our quoted program: We want to define a function that accepts `a` and `[C]` and builds our quoted program:
a [C] G a [C] G
@ -163,7 +163,7 @@ J('23 [dup ++] G 5 [x] times')
23 24 25 26 27 [28 swap [dup ++] direco] 23 24 25 26 27 [28 swap [dup ++] direco]
# Generating Multiples of Three and Five ## Generating Multiples of Three and Five
Look at the treatment of the Project Euler Problem One in [Developing a Program.ipynb](./Developing a Program.ipynb) and you'll see that we might be interested in generating an endless cycle of: Look at the treatment of the Project Euler Problem One in [Developing a Program.ipynb](./Developing a Program.ipynb) and you'll see that we might be interested in generating an endless cycle of:
3 2 1 3 1 2 3 3 2 1 3 1 2 3
@ -273,7 +273,7 @@ J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum')
999 999
# Project Euler Problem One ## Project Euler Problem One
```python ```python
@ -290,7 +290,7 @@ J('0 0 0 [PE1.1.check PE1.1] G 466 [x [PE1.2] dip] times popop')
233168 233168
# A generator for the Fibonacci Sequence. ## A generator for the Fibonacci Sequence.
Consider: Consider:
[b a F] x [b a F] x
@ -362,7 +362,7 @@ J('fib_gen 10 [x] times')
1 2 3 5 8 13 21 34 55 89 [144 89 fib] 1 2 3 5 8 13 21 34 55 89 [144 89 fib]
### Project Euler Problem Two ## Project Euler Problem Two
By considering the terms in the Fibonacci sequence whose values do not exceed four million, By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms. find the sum of the even-valued terms.
@ -463,10 +463,10 @@ J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec')
4613732 4613732
# How to compile these? ## How to compile these?
You would probably start with a special version of `G`, and perhaps modifications to the default `x`? You would probably start with a special version of `G`, and perhaps modifications to the default `x`?
# An Interesting Variation ## An Interesting Variation
```python ```python

View File

@ -14,8 +14,8 @@ Consider the ``x`` combinator:
x == dup i x == dup i
We can apply it to a quoted program consisting of some value ``a`` and a We can apply it to a quoted program consisting of some value ``a`` and
function ``B``: some function ``B``:
:: ::
@ -43,14 +43,14 @@ Now discard the quoted ``a`` with ``rest`` then ``cons`` ``b``:
b [B] cons b [B] cons
[b B] [b B]
Putting it together, this is the definition of ``B``: Altogether, this is the definition of ``B``:
:: ::
B == swap [C] dip rest cons B == swap [C] dip rest cons
We can create a quoted program that generates the Natural numbers (0, 1, We can make a generator for the Natural numbers (0, 1, 2, ...) by using
2, ...) by using ``0`` for ``a`` and ``[dup ++]`` for ``[C]``: ``0`` for ``a`` and ``[dup ++]`` for ``[C]``:
:: ::
@ -93,7 +93,7 @@ After one application of ``x`` the quoted program contains ``1`` and
``direco`` ``direco``
~~~~~~~~~~ ----------
.. code:: ipython2 .. code:: ipython2
@ -122,7 +122,7 @@ After one application of ``x`` the quoted program contains ``1`` and
Making Generators Making Generators
================= -----------------
We want to define a function that accepts ``a`` and ``[C]`` and builds We want to define a function that accepts ``a`` and ``[C]`` and builds
our quoted program: our quoted program:
@ -206,7 +206,7 @@ with the ``x`` combinator.
Generating Multiples of Three and Five Generating Multiples of Three and Five
====================================== --------------------------------------
Look at the treatment of the Project Euler Problem One in `Developing a Look at the treatment of the Project Euler Problem One in `Developing a
Program.ipynb <./Developing%20a%20Program.ipynb>`__ and you'll see that Program.ipynb <./Developing%20a%20Program.ipynb>`__ and you'll see that
@ -351,7 +351,7 @@ If we drive our generator 466 times and sum the stack we get 999.
Project Euler Problem One Project Euler Problem One
========================= -------------------------
.. code:: ipython2 .. code:: ipython2
@ -370,7 +370,7 @@ Now we can add ``PE1.2`` to the quoted program given to ``G``.
A generator for the Fibonacci Sequence. A generator for the Fibonacci Sequence.
======================================= ---------------------------------------
Consider: Consider:
@ -466,7 +466,7 @@ Putting it all together:
Project Euler Problem Two Project Euler Problem Two
~~~~~~~~~~~~~~~~~~~~~~~~~ -------------------------
:: ::
@ -589,13 +589,13 @@ Replace ``x`` with our new driver function ``PE2.2`` and start our
How to compile these? How to compile these?
===================== ---------------------
You would probably start with a special version of ``G``, and perhaps You would probably start with a special version of ``G``, and perhaps
modifications to the default ``x``? modifications to the default ``x``?
An Interesting Variation An Interesting Variation
======================== ------------------------
.. code:: ipython2 .. code:: ipython2

View File

@ -1177,14 +1177,17 @@
<span class="nd">@FunctionWrapper</span> <span class="nd">@FunctionWrapper</span>
<span class="k">def</span> <span class="nf">cond</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span> <span class="k">def</span> <span class="nf">cond</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> like a case statement; works by rewriting into a chain of ifte.</span> <span class="sd"> This combinator works like a case statement. It expects a single quote</span>
<span class="sd"> on the stack that must contain zero or more condition quotes and a </span>
<span class="sd"> default quote. Each condition clause should contain a quoted predicate</span>
<span class="sd"> followed by the function expression to run if that predicate returns</span>
<span class="sd"> true. If no predicates return true the default function runs.</span>
<span class="sd"> [..[[Bi] Ti]..[D]] -&gt; ...</span> <span class="sd"> It works by rewriting into a chain of nested `ifte` expressions, e.g.::</span>
<span class="sd"> [[[B0] T0] [[B1] T1] [D]] cond</span>
<span class="sd"> [[[B0] T0] [[B1] T1] [D]] cond</span> <span class="sd"> -----------------------------------------</span>
<span class="sd"> -----------------------------------------</span> <span class="sd"> [B0] [T0] [[B1] [T1] [D] ifte] ifte</span>
<span class="sd"> [B0] [T0] [[B1] [T1] [D] ifte] ifte</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="n">conditions</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">stack</span> <span class="n">conditions</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">stack</span>
@ -1433,9 +1436,10 @@
<div class="viewcode-block" id="cmp_"><a class="viewcode-back" href="../../library.html#joy.library.cmp_">[docs]</a><span class="nd">@inscribe</span> <div class="viewcode-block" id="cmp_"><a class="viewcode-back" href="../../library.html#joy.library.cmp_">[docs]</a><span class="nd">@inscribe</span>
<span class="nd">@FunctionWrapper</span> <span class="nd">@FunctionWrapper</span>
<span class="k">def</span> <span class="nf">cmp_</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span> <span class="k">def</span> <span class="nf">cmp_</span><span class="p">(</span><span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;</span> <span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> cmp takes two values and three quoted programs on the stack and runs</span> <span class="sd"> cmp takes two values and three quoted programs on the stack and runs</span>
<span class="sd"> one of the three depending on the results of comparing the two values:</span> <span class="sd"> one of the three depending on the results of comparing the two values:</span>
<span class="sd"> ::</span>
<span class="sd"> a b [G] [E] [L] cmp</span> <span class="sd"> a b [G] [E] [L] cmp</span>
<span class="sd"> ------------------------- a &gt; b</span> <span class="sd"> ------------------------- a &gt; b</span>
@ -1448,10 +1452,10 @@
<span class="sd"> a b [G] [E] [L] cmp</span> <span class="sd"> a b [G] [E] [L] cmp</span>
<span class="sd"> ------------------------- a &lt; b</span> <span class="sd"> ------------------------- a &lt; b</span>
<span class="sd"> L</span> <span class="sd"> L</span>
<span class="sd"> &#39;&#39;&#39;</span> <span class="sd"> &#39;&#39;&#39;</span>
<span class="n">L</span><span class="p">,</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">stack</span><span class="p">))))</span> <span class="o">=</span> <span class="n">stack</span> <span class="n">L</span><span class="p">,</span> <span class="p">(</span><span class="n">E</span><span class="p">,</span> <span class="p">(</span><span class="n">G</span><span class="p">,</span> <span class="p">(</span><span class="n">b</span><span class="p">,</span> <span class="p">(</span><span class="n">a</span><span class="p">,</span> <span class="n">stack</span><span class="p">))))</span> <span class="o">=</span> <span class="n">stack</span>
<span class="n">expression</span> <span class="o">=</span> <span class="n">pushback</span><span class="p">(</span><span class="n">G</span> <span class="k">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span> <span class="k">else</span> <span class="n">L</span> <span class="k">if</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span> <span class="k">else</span> <span class="n">E</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span> <span class="n">expression</span> <span class="o">=</span> <span class="n">pushback</span><span class="p">(</span><span class="n">G</span> <span class="k">if</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span> <span class="k">else</span> <span class="n">L</span> <span class="k">if</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span> <span class="k">else</span> <span class="n">E</span><span class="p">,</span> <span class="n">expression</span><span class="p">)</span>
<span class="k">return</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span></div> <span class="k">return</span> <span class="n">stack</span><span class="p">,</span> <span class="n">expression</span><span class="p">,</span> <span class="n">dictionary</span></div>
<span class="c1">#def nullary(S, expression, dictionary):</span> <span class="c1">#def nullary(S, expression, dictionary):</span>

View File

@ -135,6 +135,7 @@ interesting aspects. Its quite a treasure trove.</p>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Replacing.html">Replacing Functions in the Dictionary</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Replacing.html">Replacing Functions in the Dictionary</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Ordered_Binary_Trees.html">Treating Trees I: Ordered Binary Trees</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Ordered_Binary_Trees.html">Treating Trees I: Ordered Binary Trees</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Treestep.html">Treating Trees II: <code class="docutils literal notranslate"><span class="pre">treestep</span></code></a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Treestep.html">Treating Trees II: <code class="docutils literal notranslate"><span class="pre">treestep</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Generator Programs.html">Using <code class="docutils literal notranslate"><span class="pre">x</span></code> to Generate Values</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Newton-Raphson.html">Newtons method</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Newton-Raphson.html">Newtons method</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/Quadratic.html">Quadratic formula</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/Quadratic.html">Quadratic formula</a></li>
<li class="toctree-l2"><a class="reference internal" href="notebooks/NoUpdates.html">No Updates</a></li> <li class="toctree-l2"><a class="reference internal" href="notebooks/NoUpdates.html">No Updates</a></li>

View File

@ -198,24 +198,19 @@ Boolean value (so empty string, zero, etc. are counted as false, etc.)</p>
<code class="descclassname">joy.library.</code><code class="descname">cmp_</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cmp_"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cmp_" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.library.</code><code class="descname">cmp_</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cmp_"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cmp_" title="Permalink to this definition"></a></dt>
<dd><p>cmp takes two values and three quoted programs on the stack and runs <dd><p>cmp takes two values and three quoted programs on the stack and runs
one of the three depending on the results of comparing the two values:</p> one of the three depending on the results of comparing the two values:</p>
<blockquote> <div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="n">a</span> <span class="n">b</span> <span class="p">[</span><span class="n">G</span><span class="p">]</span> <span class="p">[</span><span class="n">E</span><span class="p">]</span> <span class="p">[</span><span class="n">L</span><span class="p">]</span> <span class="nb">cmp</span>
<div><blockquote> <span class="o">-------------------------</span> <span class="n">a</span> <span class="o">&gt;</span> <span class="n">b</span>
<div>a b [G] [E] [L] cmp</div></blockquote> <span class="n">G</span>
<dl class="docutils">
<dt>————————- a &gt; b</dt> <span class="n">a</span> <span class="n">b</span> <span class="p">[</span><span class="n">G</span><span class="p">]</span> <span class="p">[</span><span class="n">E</span><span class="p">]</span> <span class="p">[</span><span class="n">L</span><span class="p">]</span> <span class="nb">cmp</span>
<dd><blockquote class="first"> <span class="o">-------------------------</span> <span class="n">a</span> <span class="o">=</span> <span class="n">b</span>
<div>G</div></blockquote> <span class="n">E</span>
<p class="last">a b [G] [E] [L] cmp</p>
</dd> <span class="n">a</span> <span class="n">b</span> <span class="p">[</span><span class="n">G</span><span class="p">]</span> <span class="p">[</span><span class="n">E</span><span class="p">]</span> <span class="p">[</span><span class="n">L</span><span class="p">]</span> <span class="nb">cmp</span>
<dt>————————- a = b</dt> <span class="o">-------------------------</span> <span class="n">a</span> <span class="o">&lt;</span> <span class="n">b</span>
<dd><blockquote class="first"> <span class="n">L</span>
<div>E</div></blockquote> </pre></div>
<p class="last">a b [G] [E] [L] cmp</p> </div>
</dd>
<dt>————————- a &lt; b</dt>
<dd>L</dd>
</dl>
</div></blockquote>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">
@ -227,12 +222,17 @@ one of the three depending on the results of comparing the two values:</p>
<dl class="function"> <dl class="function">
<dt id="joy.library.cond"> <dt id="joy.library.cond">
<code class="descclassname">joy.library.</code><code class="descname">cond</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cond"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cond" title="Permalink to this definition"></a></dt> <code class="descclassname">joy.library.</code><code class="descname">cond</code><span class="sig-paren">(</span><em>stack</em>, <em>expression</em>, <em>dictionary</em><span class="sig-paren">)</span><a class="reference internal" href="_modules/joy/library.html#cond"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.cond" title="Permalink to this definition"></a></dt>
<dd><p>like a case statement; works by rewriting into a chain of ifte.</p> <dd><p>This combinator works like a case statement. It expects a single quote
<p>[..[[Bi] Ti]..[D]] -&gt;</p> on the stack that must contain zero or more condition quotes and a
<blockquote> default quote. Each condition clause should contain a quoted predicate
<div>[[[B0] T0] [[B1] T1] [D]] cond</div></blockquote> followed by the function expression to run if that predicate returns
<blockquote> true. If no predicates return true the default function runs.</p>
<div>[B0] [T0] [[B1] [T1] [D] ifte] ifte</div></blockquote> <p>It works by rewriting into a chain of nested <cite>ifte</cite> expressions, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span> <span class="p">[[[</span><span class="n">B0</span><span class="p">]</span> <span class="n">T0</span><span class="p">]</span> <span class="p">[[</span><span class="n">B1</span><span class="p">]</span> <span class="n">T1</span><span class="p">]</span> <span class="p">[</span><span class="n">D</span><span class="p">]]</span> <span class="n">cond</span>
<span class="o">-----------------------------------------</span>
<span class="p">[</span><span class="n">B0</span><span class="p">]</span> <span class="p">[</span><span class="n">T0</span><span class="p">]</span> <span class="p">[[</span><span class="n">B1</span><span class="p">]</span> <span class="p">[</span><span class="n">T1</span><span class="p">]</span> <span class="p">[</span><span class="n">D</span><span class="p">]</span> <span class="n">ifte</span><span class="p">]</span> <span class="n">ifte</span>
</pre></div>
</div>
</dd></dl> </dd></dl>
<dl class="function"> <dl class="function">

File diff suppressed because one or more lines are too long

View File

@ -73,6 +73,17 @@
<li class="toctree-l2"><a class="reference internal" href="Treestep.html#putting-it-together">Putting it together</a></li> <li class="toctree-l2"><a class="reference internal" href="Treestep.html#putting-it-together">Putting it together</a></li>
</ul> </ul>
</li> </li>
<li class="toctree-l1"><a class="reference internal" href="Generator Programs.html">Using <code class="docutils literal notranslate"><span class="pre">x</span></code> to Generate Values</a><ul>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#direco"><code class="docutils literal notranslate"><span class="pre">direco</span></code></a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#making-generators">Making Generators</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#generating-multiples-of-three-and-five">Generating Multiples of Three and Five</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#project-euler-problem-one">Project Euler Problem One</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#a-generator-for-the-fibonacci-sequence">A generator for the Fibonacci Sequence.</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#project-euler-problem-two">Project Euler Problem Two</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#how-to-compile-these">How to compile these?</a></li>
<li class="toctree-l2"><a class="reference internal" href="Generator Programs.html#an-interesting-variation">An Interesting Variation</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="Newton-Raphson.html">Newtons method</a><ul> <li class="toctree-l1"><a class="reference internal" href="Newton-Raphson.html">Newtons method</a><ul>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#a-generator-for-approximations">A Generator for Approximations</a></li> <li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#a-generator-for-approximations">A Generator for Approximations</a></li>
<li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance</a></li> <li class="toctree-l2"><a class="reference internal" href="Newton-Raphson.html#finding-consecutive-approximations-within-a-tolerance">Finding Consecutive Approximations <code class="docutils literal notranslate"><span class="pre">within</span></code> a Tolerance</a></li>

File diff suppressed because one or more lines are too long

View File

@ -8,31 +8,40 @@ Cf. jp-reprod.html
from notebook_preamble import J, V, define from notebook_preamble import J, V, define
Consider the ``x`` combinator ``x == dup i``: Consider the ``x`` combinator:
::
x == dup i
We can apply it to a quoted program consisting of some value ``a`` and
some function ``B``:
:: ::
[a B] x [a B] x
[a B] a B [a B] a B
Let ``B`` ``swap`` the ``a`` with the quote and run some function Let ``B`` function ``swap`` the ``a`` with the quote and run some
``[C]`` on it. function ``C`` on it to generate a new value ``b``:
:: ::
B == swap [C] dip
[a B] a B [a B] a B
[a B] a swap [C] dip [a B] a swap [C] dip
a [a B] [C] dip a [a B] [C] dip
a C [a B] a C [a B]
b [a B]
Now discard the quoted ``a`` with ``rest`` and ``cons`` the result of Now discard the quoted ``a`` with ``rest`` then ``cons`` ``b``:
``C`` on ``a`` whatever that is:
:: ::
aC [a B] rest cons b [a B] rest cons
aC [B] cons b [B] cons
[aC B] [b B]
Altogether, this is the definition of ``B``: Altogether, this is the definition of ``B``:
@ -40,9 +49,8 @@ Altogether, this is the definition of ``B``:
B == swap [C] dip rest cons B == swap [C] dip rest cons
We can create a quoted program that generates the Natural numbers We can make a generator for the Natural numbers (0, 1, 2, ...) by using
(integers 0, 1, 2, ...) by using ``0`` for ``a`` and ``[dup ++]`` for ``0`` for ``a`` and ``[dup ++]`` for ``[C]``:
``[C]``:
:: ::
@ -85,7 +93,7 @@ After one application of ``x`` the quoted program contains ``1`` and
``direco`` ``direco``
~~~~~~~~~~ ----------
.. code:: ipython2 .. code:: ipython2
@ -113,20 +121,17 @@ After one application of ``x`` the quoted program contains ``1`` and
0 [1 swap [dup ++] direco] . 0 [1 swap [dup ++] direco] .
Generating Generators Making Generators
===================== -----------------
We want to go from: We want to define a function that accepts ``a`` and ``[C]`` and builds
our quoted program:
:: ::
a [C] G a [C] G
-------------------------
to: [a swap [C] direco]
::
[a swap [C] direco]
Working in reverse: Working in reverse:
@ -145,65 +150,30 @@ Reading from the bottom up:
G == [direco] cons [swap] swap concat cons G == [direco] cons [swap] swap concat cons
G == [direco] cons [swap] swoncat cons G == [direco] cons [swap] swoncat cons
We can try it out:
::
0 [dup ++] G
.. code:: ipython2 .. code:: ipython2
define('G == [direco] cons [swap] swoncat cons') define('G == [direco] cons [swap] swoncat cons')
Let's try it out:
.. code:: ipython2 .. code:: ipython2
V('0 [dup ++] G') J('0 [dup ++] G')
.. parsed-literal:: .. parsed-literal::
. 0 [dup ++] G [0 swap [dup ++] direco]
0 . [dup ++] G
0 [dup ++] . G
0 [dup ++] . [direco] cons [swap] swoncat cons
0 [dup ++] [direco] . cons [swap] swoncat cons
0 [[dup ++] direco] . [swap] swoncat cons
0 [[dup ++] direco] [swap] . swoncat cons
0 [[dup ++] direco] [swap] . swap concat cons
0 [swap] [[dup ++] direco] . concat cons
0 [swap [dup ++] direco] . cons
[0 swap [dup ++] direco] .
.. code:: ipython2 .. code:: ipython2
V('0 [dup ++] G x') J('0 [dup ++] G x x x pop')
.. parsed-literal:: .. parsed-literal::
. 0 [dup ++] G x 0 1 2
0 . [dup ++] G x
0 [dup ++] . G x
0 [dup ++] . [direco] cons [swap] swoncat cons x
0 [dup ++] [direco] . cons [swap] swoncat cons x
0 [[dup ++] direco] . [swap] swoncat cons x
0 [[dup ++] direco] [swap] . swoncat cons x
0 [[dup ++] direco] [swap] . swap concat cons x
0 [swap] [[dup ++] direco] . concat cons x
0 [swap [dup ++] direco] . cons x
[0 swap [dup ++] direco] . x
[0 swap [dup ++] direco] . 0 swap [dup ++] direco
[0 swap [dup ++] direco] 0 . swap [dup ++] direco
0 [0 swap [dup ++] direco] . [dup ++] direco
0 [0 swap [dup ++] direco] [dup ++] . direco
0 [0 swap [dup ++] direco] [dup ++] . dip rest cons
0 . dup ++ [0 swap [dup ++] direco] rest cons
0 0 . ++ [0 swap [dup ++] direco] rest cons
0 1 . [0 swap [dup ++] direco] rest cons
0 1 [0 swap [dup ++] direco] . rest cons
0 1 [swap [dup ++] direco] . cons
0 [1 swap [dup ++] direco] .
Powers of 2 Powers of 2
@ -211,77 +181,32 @@ Powers of 2
.. code:: ipython2 .. code:: ipython2
J('1 [dup 1 <<] G x x x x x x x x x') J('1 [dup 1 <<] G x x x x x x x x x pop')
.. parsed-literal:: .. parsed-literal::
1 2 4 8 16 32 64 128 256 [512 swap [dup 1 <<] direco] 1 2 4 8 16 32 64 128 256
``n [x] times`` ``[x] times``
=============== ~~~~~~~~~~~~~
If we have one of these quoted programs we can drive it using ``times`` If we have one of these quoted programs we can drive it using ``times``
with the ``x`` combinator. with the ``x`` combinator.
Let's define a word ``n_range`` that takes a starting integer and a
count and leaves that many consecutive integers on the stack. For
example:
.. code:: ipython2 .. code:: ipython2
J('23 [dup ++] G 5 [x] times pop') J('23 [dup ++] G 5 [x] times')
.. parsed-literal:: .. parsed-literal::
23 24 25 26 27 23 24 25 26 27 [28 swap [dup ++] direco]
We can use ``dip`` to untangle ``[dup ++] G`` from the arguments.
.. code:: ipython2
J('23 5 [[dup ++] G] dip [x] times pop')
.. parsed-literal::
23 24 25 26 27
Now that the givens (arguments) are on the left we have the definition
we're looking for:
.. code:: ipython2
define('n_range == [[dup ++] G] dip [x] times pop')
.. code:: ipython2
J('450 10 n_range')
.. parsed-literal::
450 451 452 453 454 455 456 457 458 459
This is better just using the ``times`` combinator though...
.. code:: ipython2
J('450 9 [dup ++] times')
.. parsed-literal::
450 451 452 453 454 455 456 457 458 459
Generating Multiples of Three and Five Generating Multiples of Three and Five
====================================== --------------------------------------
Look at the treatment of the Project Euler Problem One in `Developing a Look at the treatment of the Project Euler Problem One in `Developing a
Program.ipynb <./Developing%20a%20Program.ipynb>`__ and you'll see that Program.ipynb <./Developing%20a%20Program.ipynb>`__ and you'll see that
@ -338,16 +263,6 @@ If we plug ``14811`` and ``[PE1.1]`` into our generator form...
[14811 swap [PE1.1] direco] [14811 swap [PE1.1] direco]
.. code:: ipython2
J('[14811 swap [PE1.1] direco] x')
.. parsed-literal::
3 [3702 swap [PE1.1] direco]
...we get a generator that works for seven cycles before it reaches ...we get a generator that works for seven cycles before it reaches
zero: zero:
@ -371,6 +286,16 @@ if so.
define('PE1.1.check == dup [pop 14811] [] branch') define('PE1.1.check == dup [pop 14811] [] branch')
.. code:: ipython2
J('14811 [PE1.1.check PE1.1] G')
.. parsed-literal::
[14811 swap [PE1.1.check PE1.1] direco]
.. code:: ipython2 .. code:: ipython2
J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times') J('[14811 swap [PE1.1.check PE1.1] direco] 21 [x] times')
@ -381,6 +306,11 @@ if so.
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco] 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 [0 swap [PE1.1.check PE1.1] direco]
(It would be more efficient to reset the int every seven cycles but
that's a little beyond the scope of this article. This solution does
extra work, but not much, and we're not using it "in production" as they
say.)
Run 466 times Run 466 times
~~~~~~~~~~~~~ ~~~~~~~~~~~~~
@ -402,17 +332,17 @@ If we drive our generator 466 times and sum the stack we get 999.
.. code:: ipython2 .. code:: ipython2
J('[14811 swap [PE1.1.check PE1.1] dip rest cons] 466 [x] times') J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times')
.. parsed-literal:: .. parsed-literal::
3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] dip rest cons] 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 1 2 3 3 2 1 3 [57 swap [PE1.1.check PE1.1] direco]
.. code:: ipython2 .. code:: ipython2
J('[14811 swap [PE1.1.check PE1.1] dip rest cons] 466 [x] times pop enstacken sum') J('[14811 swap [PE1.1.check PE1.1] direco] 466 [x] times pop enstacken sum')
.. parsed-literal:: .. parsed-literal::
@ -421,25 +351,13 @@ If we drive our generator 466 times and sum the stack we get 999.
Project Euler Problem One Project Euler Problem One
========================= -------------------------
.. code:: ipython2 .. code:: ipython2
define('PE1.2 == + dup [+] dip') define('PE1.2 == + dup [+] dip')
Now we can add ``PE1.2`` to the quoted program given to ``times``. Now we can add ``PE1.2`` to the quoted program given to ``G``.
.. code:: ipython2
J('0 0 [0 swap [PE1.1.check PE1.1] direco] 466 [x [PE1.2] dip] times popop')
.. parsed-literal::
233168
Or using ``G`` we can write:
.. code:: ipython2 .. code:: ipython2
@ -452,7 +370,7 @@ Or using ``G`` we can write:
A generator for the Fibonacci Sequence. A generator for the Fibonacci Sequence.
======================================= ---------------------------------------
Consider: Consider:
@ -507,30 +425,31 @@ And therefore:
[b+a b a F] [popdd over] infra [b+a b a F] [popdd over] infra
[b b+a b F] [b b+a b F]
And lastly: But we can just use ``cons`` to carry ``b+a`` into the quote:
::
[b a F] b+a [popdd over] cons infra
[b a F] [b+a popdd over] infra
[b b+a b F]
Lastly:
:: ::
[b b+a b F] uncons [b b+a b F] uncons
b [b+a b F] b [b+a b F]
Done.
Putting it all together: Putting it all together:
:: ::
F == + swons [popdd over] infra uncons F == + [popdd over] cons infra uncons
And:
::
fib_gen == [1 1 F] fib_gen == [1 1 F]
.. code:: ipython2 .. code:: ipython2
define('fib == + swons [popdd over] infra uncons') define('fib == + [popdd over] cons infra uncons')
.. code:: ipython2 .. code:: ipython2
@ -547,11 +466,12 @@ And:
Project Euler Problem Two Project Euler Problem Two
~~~~~~~~~~~~~~~~~~~~~~~~~ -------------------------
:: ::
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms. By considering the terms in the Fibonacci sequence whose values do not exceed four million,
find the sum of the even-valued terms.
Now that we have a generator for the Fibonacci sequence, we need a Now that we have a generator for the Fibonacci sequence, we need a
function that adds a term in the sequence to a sum if it is even, and function that adds a term in the sequence to a sum if it is even, and
@ -669,7 +589,51 @@ Replace ``x`` with our new driver function ``PE2.2`` and start our
How to compile these? How to compile these?
===================== ---------------------
You would probably start with a special version of ``G``, and perhaps You would probably start with a special version of ``G``, and perhaps
modifications to the default ``x``? modifications to the default ``x``?
An Interesting Variation
------------------------
.. code:: ipython2
define('codireco == cons dip rest cons')
.. code:: ipython2
V('[0 [dup ++] codireco] x')
.. parsed-literal::
. [0 [dup ++] codireco] x
[0 [dup ++] codireco] . x
[0 [dup ++] codireco] . 0 [dup ++] codireco
[0 [dup ++] codireco] 0 . [dup ++] codireco
[0 [dup ++] codireco] 0 [dup ++] . codireco
[0 [dup ++] codireco] 0 [dup ++] . cons dip rest cons
[0 [dup ++] codireco] [0 dup ++] . dip rest cons
. 0 dup ++ [0 [dup ++] codireco] rest cons
0 . dup ++ [0 [dup ++] codireco] rest cons
0 0 . ++ [0 [dup ++] codireco] rest cons
0 1 . [0 [dup ++] codireco] rest cons
0 1 [0 [dup ++] codireco] . rest cons
0 1 [[dup ++] codireco] . cons
0 [1 [dup ++] codireco] .
.. code:: ipython2
define('G == [codireco] cons cons')
.. code:: ipython2
J('230 [dup ++] G 5 [x] times pop')
.. parsed-literal::
230 231 232 233 234

View File

@ -12,6 +12,7 @@ These essays are adapted from Jupyter notebooks. I hope to have those hosted so
Replacing Replacing
Ordered_Binary_Trees Ordered_Binary_Trees
Treestep Treestep
Generator Programs
Newton-Raphson Newton-Raphson
Quadratic Quadratic
NoUpdates NoUpdates

View File

@ -1144,14 +1144,17 @@ def ifte(stack, expression, dictionary):
@FunctionWrapper @FunctionWrapper
def cond(stack, expression, dictionary): def cond(stack, expression, dictionary):
''' '''
like a case statement; works by rewriting into a chain of ifte. This combinator works like a case statement. It expects a single quote
on the stack that must contain zero or more condition quotes and a
default quote. Each condition clause should contain a quoted predicate
followed by the function expression to run if that predicate returns
true. If no predicates return true the default function runs.
[..[[Bi] Ti]..[D]] -> ... It works by rewriting into a chain of nested `ifte` expressions, e.g.::
[[[B0] T0] [[B1] T1] [D]] cond
[[[B0] T0] [[B1] T1] [D]] cond -----------------------------------------
----------------------------------------- [B0] [T0] [[B1] [T1] [D] ifte] ifte
[B0] [T0] [[B1] [T1] [D] ifte] ifte
''' '''
conditions, stack = stack conditions, stack = stack
@ -1400,9 +1403,10 @@ def loop(stack, expression, dictionary):
@inscribe @inscribe
@FunctionWrapper @FunctionWrapper
def cmp_(stack, expression, dictionary): def cmp_(stack, expression, dictionary):
''' '''
cmp takes two values and three quoted programs on the stack and runs cmp takes two values and three quoted programs on the stack and runs
one of the three depending on the results of comparing the two values: one of the three depending on the results of comparing the two values:
::
a b [G] [E] [L] cmp a b [G] [E] [L] cmp
------------------------- a > b ------------------------- a > b
@ -1415,10 +1419,10 @@ def cmp_(stack, expression, dictionary):
a b [G] [E] [L] cmp a b [G] [E] [L] cmp
------------------------- a < b ------------------------- a < b
L L
''' '''
L, (E, (G, (b, (a, stack)))) = stack L, (E, (G, (b, (a, stack)))) = stack
expression = pushback(G if a > b else L if a < b else E, expression) expression = pushback(G if a > b else L if a < b else E, expression)
return stack, expression, dictionary return stack, expression, dictionary
#def nullary(S, expression, dictionary): #def nullary(S, expression, dictionary):