Working on the notebooks.

This commit is contained in:
sforman 2023-08-16 10:04:52 -07:00
parent 7d8e2ae611
commit 387d9d4ed4
5 changed files with 3567 additions and 20599 deletions

File diff suppressed because it is too large Load Diff

View File

@ -244,103 +244,160 @@ b [b+a b F]
</code></pre> </code></pre>
<p>Putting it all together:</p> <p>Putting it all together:</p>
<pre><code>F == + [popdd over] cons infra uncons <pre><code>F == + [popdd over] cons infra uncons
fib_gen == [1 1 F]
</code></pre> </code></pre>
<p>Let's call <code>F</code> <code>fib_gen</code>:</p> <h3><code>fib-gen</code></h3>
<pre><code>[fib_gen + [popdd over] cons infra uncons] inscribe <p>Let's call <code>F</code> <code>fib-gen</code>:</p>
<pre><code>[fib-gen + [popdd over] cons infra uncons] inscribe
</code></pre> </code></pre>
<p>We can just write the initial quote and then "force" it with <code>x</code>:</p> <p>We can just write the initial quote and then "force" it with <code>x</code>:</p>
<pre><code>joy? [1 1 fib_gen] 10 [x] times <pre><code>joy? [1 1 fib-gen] 10 [x] times
1 2 3 5 8 13 21 34 55 89 [144 89 fib_gen] 1 2 3 5 8 13 21 34 55 89 [144 89 fib-gen]
</code></pre> </code></pre>
<p>It skips the first term (1) but if that bothers you you can just prepend it to the program:</p> <p>It skips the first term (1) but if that bothers you you can just prepend it to the program:</p>
<pre><code>1 [1 1 fib_gen] 10 [x] times <pre><code>1 [1 1 fib-gen] 10 [x] times
</code></pre> </code></pre>
<h2>Project Euler Problem Two</h2> <h2>Project Euler Problem Two</h2>
<blockquote> <blockquote>
<p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p> <p>By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.</p>
</blockquote> </blockquote>
<p>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 <code>pop</code>s it otherwise.</p> <p>Now that we have a generator for the Fibonacci sequence, we need a function that adds
<p><code>python a term in the sequence to a sum if it is even, and <code>pop</code>s it otherwise.</p>
define('PE2.1 == dup 2 % [+] [pop] branch')</code></p> <h3><code>even</code></h3>
<pre><code>[even 2 % bool] inscribe
</code></pre>
<h3><code>PE2.1</code></h3>
<pre><code>[PE2.1 dup even [+] [pop] branch] inscribe
</code></pre>
<p>And a predicate function that detects when the terms in the series "exceed four million".</p> <p>And a predicate function that detects when the terms in the series "exceed four million".</p>
<p><code>python <h3><code>&gt;4M</code></h3>
define('&gt;4M == 4000000 &gt;')</code></p> <pre><code>[&gt;4M 4000000 &gt;] inscribe
<p>Now it's straightforward to define <code>PE2</code> as a recursive function that generates terms in the Fibonacci sequence until they exceed four million and sums the even ones.</p>
<p><code>python
define('PE2 == 0 fib_gen x [pop &gt;4M] [popop] [[PE2.1] dip x] primrec')</code></p>
<p><code>python
J('PE2')</code></p>
<pre><code>4613732
</code></pre> </code></pre>
<p>Here's the collected program definitions:</p> <p>Now it's straightforward to define <code>PE2</code> as a recursive function that generates terms
<pre><code>fib == + swons [popdd over] infra uncons in the Fibonacci sequence until they exceed four million and sums the even ones.</p>
fib_gen == [1 1 fib] <pre><code>joy? 0 [1 1 fib-gen] x [pop &gt;4M] [popop] [[PE2.1] dip x] tailrec
4613732
even == dup 2 %
&gt;4M == 4000000 &gt;
PE2.1 == even [+] [pop] branch
PE2 == 0 fib_gen x [pop &gt;4M] [popop] [[PE2.1] dip x] primrec
</code></pre> </code></pre>
<h3>Even-valued Fibonacci Terms</h3> <h3><code>PE2</code></h3>
<pre><code>[PE2 0 [1 1 fib-gen] x [pop &gt;4M] [popop] [[PE2.1] dip x] tailrec] inscribe
</code></pre>
<p>Here's the collected program definitions (with a little editorializing):</p>
<pre><code>fib-gen + [popdd over] cons infra uncons
even 2 % bool
&gt;4M 4000000 &gt;
PE2.1 dup even [+] [pop] branch
PE2.2 [PE2.1] dip x
PE2.init 0 [1 1 fib-gen] x
PE2.rec [pop &gt;4M] [popop] [PE2.2] tailrec
PE2 PE2.init PE2.rec
</code></pre>
<h3>Hmm...</h3>
<pre><code>fib-gen + swons [popdd over] infra uncons
</code></pre>
<h2>Even-valued Fibonacci Terms</h2>
<p>Using <code>o</code> for odd and <code>e</code> for even:</p> <p>Using <code>o</code> for odd and <code>e</code> for even:</p>
<pre><code>o + o = e <pre><code>o + o = e
e + e = e e + e = e
o + e = o o + e = o
</code></pre> </code></pre>
<p>So the Fibonacci sequence considered in terms of just parity would be:</p> <p>So the Fibonacci sequence considered in terms of just parity would be:</p>
<pre><code>o o e o o e o o e o o e o o e o o e <pre><code>o o e o o e o o e o o e . . .
1 1 2 3 5 8 . . . 1 1 2 3 5 8 13 21 34 55 89 144 . . .
</code></pre> </code></pre>
<p>Every third term is even.</p> <p>Every third term is even.</p>
<p><code>python <p>So what if we drive the generator three times and discard the odd terms?
J('[1 0 fib] x x x') # To start the sequence with 1 1 2 3 instead of 1 2 3.</code></p> We would have to initialize our <code>fib</code> generator with 1 0:</p>
<pre><code>1 1 2 [3 2 fib] <pre><code>[1 0 fib-gen]
</code></pre> </code></pre>
<p>Drive the generator three times and <code>popop</code> the two odd terms.</p> <h3><code>third-term</code></h3>
<p><code>python <pre><code>[third-term x x x [popop] dipd] inscribe
J('[1 0 fib] x x x [popop] dipd')</code></p>
<pre><code>2 [3 2 fib]
</code></pre> </code></pre>
<p><code>python <p>So:</p>
define('PE2.2 == x x x [popop] dipd')</code></p> <pre><code>joy? [1 0 fib-gen]
<p><code>python [1 0 fib-gen]
J('[1 0 fib] 10 [PE2.2] times')</code></p>
<pre><code>2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib] joy? third-term
2 [3 2 fib-gen]
joy? third-term
2 8 [13 8 fib-gen]
joy? third-term
2 8 34 [55 34 fib-gen]
joy? third-term
2 8 34 144 [233 144 fib-gen]
</code></pre> </code></pre>
<p>Replace <code>x</code> with our new driver function <code>PE2.2</code> and start our <code>fib</code> generator at <code>1 0</code>.</p> <p>So now we need a sum:</p>
<p><code>python <pre><code>joy? 0
J('0 [1 0 fib] PE2.2 [pop &gt;4M] [popop] [[PE2.1] dip PE2.2] primrec')</code></p> 0
<pre><code>4613732
</code></pre> </code></pre>
<h2>How to compile these?</h2> <p>And our Fibonacci generator:</p>
<p>You would probably start with a special version of <code>G</code>, and perhaps modifications to the default <code>x</code>?</p> <pre><code>joy? [1 0 fib-gen]
<h2>An Interesting Variation</h2> 0 [1 0 fib-gen]
<p><code>python
define('codireco == cons dip rest cons')</code></p>
<p><code>python
V('[0 [dup ++] codireco] x')</code></p>
<pre><code> . [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></pre> </code></pre>
<p><code>python <p>We want to generate the initial term:</p>
define('G == [codireco] cons cons')</code></p> <pre><code>joy? third-term
<p><code>python 0 2 [3 2 fib-gen]
J('230 [dup ++] G 5 [x] times pop')</code></p>
<pre><code>230 231 232 233 234
</code></pre> </code></pre>
<p>Now we check if the term is less than four million,
if so we add it and recur,
otherwise we discard the term and the generator leaving the sum on the stack:</p>
<pre><code>joy? [pop &gt;4M] [popop] [[PE2.1] dip third-term] tailrec
4613732
</code></pre>
<h2>Math</h2>
<pre><code>a b
b a+b
a+b a+b+b
a+b+b a+a+b+b+b
</code></pre>
<p>So if (a,b) and a is even then the next even term pair is (a+2b, 2a+3b)</p>
<p>Reconsider:</p>
<pre><code>[b a F] x
[b a F] b a F
</code></pre>
<p>From here we want to arrive at:</p>
<pre><code>(a+2b) [(2a+3b) (a+2b) F]
b a F
b a [F0] [F1] fork
b a over [+] ii
---------------------
a+2b
</code></pre>
<p>And:</p>
<pre><code> b a over [dup + +] ii
---------------------------
2a+3b
[over [dup + +] ii] [over [+] ii] clop
roll&lt; rrest [tuck] dip ccons
[b a F] b a F
[b a F] (2a+3b) (a+2b) roll&lt;
(2a+3b) (a+2b) [b a F] rrest
(2a+3b) (a+2b) [F] [tuck] dip ccons
joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
[1 0 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
joy? x
2 [3 2 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
joy? x
2 8 [13 8 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
joy? x
2 8 34 [55 34 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
joy? x
2 8 34 144 [233 144 [over [dup + +] ii] [over [+] ii] clop roll&lt; rrest [tuck] dip ccons]
</code></pre>
<p>And so it goes...</p>
</body> </body>
</html> </html>

View File

@ -93,3 +93,43 @@ them in host language for greater efficiency if you like.)
| NOT | `not` | | | | NOT | `not` | | |
## An Interesting Variation
codireco == cons dip rest cons')
[0 [dup ++] codireco] x
. [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] .
```python
define('G == [codireco] cons cons')
```
```python
J('230 [dup ++] G 5 [x] times pop')
```
230 231 232 233 234

File diff suppressed because it is too large Load Diff

View File

@ -294,66 +294,72 @@ Lastly:
Putting it all together: Putting it all together:
F == + [popdd over] cons infra uncons F == + [popdd over] cons infra uncons
fib_gen == [1 1 F]
Let's call `F` `fib_gen`: ### `fib-gen`
[fib_gen + [popdd over] cons infra uncons] inscribe Let's call `F` `fib-gen`:
[fib-gen + [popdd over] cons infra uncons] inscribe
We can just write the initial quote and then "force" it with `x`: We can just write the initial quote and then "force" it with `x`:
joy? [1 1 fib_gen] 10 [x] times joy? [1 1 fib-gen] 10 [x] times
1 2 3 5 8 13 21 34 55 89 [144 89 fib_gen] 1 2 3 5 8 13 21 34 55 89 [144 89 fib-gen]
It skips the first term (1) but if that bothers you you can just prepend it to the program: It skips the first term (1) but if that bothers you you can just prepend it to the program:
1 [1 1 fib_gen] 10 [x] times 1 [1 1 fib-gen] 10 [x] times
## 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 function that adds a term in the sequence to a sum if it is even, and `pop`s it otherwise. 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 `pop`s it otherwise.
### `even`
```python [even 2 % bool] inscribe
define('PE2.1 == dup 2 % [+] [pop] branch')
``` ### `PE2.1`
[PE2.1 dup even [+] [pop] branch] inscribe
And a predicate function that detects when the terms in the series "exceed four million". And a predicate function that detects when the terms in the series "exceed four million".
### `>4M`
```python [>4M 4000000 >] inscribe
define('>4M == 4000000 >')
```
Now it's straightforward to define `PE2` as a recursive function that generates terms in the Fibonacci sequence until they exceed four million and sums the even ones. Now it's straightforward to define `PE2` as a recursive function that generates terms
in the Fibonacci sequence until they exceed four million and sums the even ones.
```python
define('PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec')
```
```python
J('PE2')
```
joy? 0 [1 1 fib-gen] x [pop >4M] [popop] [[PE2.1] dip x] tailrec
4613732 4613732
### `PE2`
Here's the collected program definitions: [PE2 0 [1 1 fib-gen] x [pop >4M] [popop] [[PE2.1] dip x] tailrec] inscribe
fib == + swons [popdd over] infra uncons Here's the collected program definitions (with a little editorializing):
fib_gen == [1 1 fib]
even == dup 2 % fib-gen + [popdd over] cons infra uncons
>4M == 4000000 > even 2 % bool
>4M 4000000 >
PE2.1 dup even [+] [pop] branch
PE2.2 [PE2.1] dip x
PE2.init 0 [1 1 fib-gen] x
PE2.rec [pop >4M] [popop] [PE2.2] tailrec
PE2 PE2.init PE2.rec
PE2.1 == even [+] [pop] branch
PE2 == 0 fib_gen x [pop >4M] [popop] [[PE2.1] dip x] primrec
### Even-valued Fibonacci Terms ### Hmm...
fib-gen + swons [popdd over] infra uncons
## Even-valued Fibonacci Terms
Using `o` for odd and `e` for even: Using `o` for odd and `e` for even:
@ -363,93 +369,116 @@ Using `o` for odd and `e` for even:
So the Fibonacci sequence considered in terms of just parity would be: So the Fibonacci sequence considered in terms of just parity would be:
o o e o o e o o e o o e o o e o o e o o e o o e o o e o o e . . .
1 1 2 3 5 8 . . . 1 1 2 3 5 8 13 21 34 55 89 144 . . .
Every third term is even. Every third term is even.
So what if we drive the generator three times and discard the odd terms?
We would have to initialize our `fib` generator with 1 0:
[1 0 fib-gen]
```python ### `third-term`
J('[1 0 fib] x x x') # To start the sequence with 1 1 2 3 instead of 1 2 3.
```
1 1 2 [3 2 fib] [third-term x x x [popop] dipd] inscribe
So:
Drive the generator three times and `popop` the two odd terms. joy? [1 0 fib-gen]
[1 0 fib-gen]
joy? third-term
2 [3 2 fib-gen]
joy? third-term
2 8 [13 8 fib-gen]
joy? third-term
2 8 34 [55 34 fib-gen]
joy? third-term
2 8 34 144 [233 144 fib-gen]
So now we need a sum:
```python joy? 0
J('[1 0 fib] x x x [popop] dipd') 0
```
2 [3 2 fib] And our Fibonacci generator:
joy? [1 0 fib-gen]
0 [1 0 fib-gen]
We want to generate the initial term:
```python joy? third-term
define('PE2.2 == x x x [popop] dipd') 0 2 [3 2 fib-gen]
```
Now we check if the term is less than four million,
if so we add it and recur,
otherwise we discard the term and the generator leaving the sum on the stack:
```python joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
J('[1 0 fib] 10 [PE2.2] times')
```
2 8 34 144 610 2584 10946 46368 196418 832040 [1346269 832040 fib]
Replace `x` with our new driver function `PE2.2` and start our `fib` generator at `1 0`.
```python
J('0 [1 0 fib] PE2.2 [pop >4M] [popop] [[PE2.1] dip PE2.2] primrec')
```
4613732 4613732
## Math
## How to compile these? a b
You would probably start with a special version of `G`, and perhaps modifications to the default `x`? b a+b
a+b a+b+b
a+b+b a+a+b+b+b
## An Interesting Variation So if (a,b) and a is even then the next even term pair is (a+2b, 2a+3b)
Reconsider:
[b a F] x
[b a F] b a F
From here we want to arrive at:
(a+2b) [(2a+3b) (a+2b) F]
b a F
b a [F0] [F1] fork
b a over [+] ii
---------------------
a+2b
And:
b a over [dup + +] ii
---------------------------
2a+3b
```python [over [dup + +] ii] [over [+] ii] clop
define('codireco == cons dip rest cons') roll< rrest [tuck] dip ccons
```
```python [b a F] b a F
V('[0 [dup ++] codireco] x')
```
. [0 [dup ++] codireco] x [b a F] (2a+3b) (a+2b) roll<
[0 [dup ++] codireco] . x (2a+3b) (a+2b) [b a F] rrest
[0 [dup ++] codireco] . 0 [dup ++] codireco (2a+3b) (a+2b) [F] [tuck] dip ccons
[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] .
joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
joy? x
2 [3 2 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
joy? x
2 8 [13 8 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
joy? x
2 8 34 [55 34 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
joy? x
2 8 34 144 [233 144 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
```python And so it goes...
define('G == [codireco] cons cons')
```
```python
J('230 [dup ++] G 5 [x] times pop')
```
230 231 232 233 234