Update some of the docs.

This commit is contained in:
Simon Forman 2020-05-20 19:15:47 -07:00
parent 6a6b63bf62
commit ffabda0407
60 changed files with 5182 additions and 5853 deletions

View File

@ -13133,17 +13133,84 @@ joy? </code></pre>
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">inspect</span>
<span class="kn">import</span> <span class="nn">joy.utils.stack</span>
<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>When talking about Joy we use the terms &#34;stack&#34;, &#34;quote&#34;, &#34;sequence&#34;,
&#34;list&#34;, and others to mean the same thing: a simple linear datatype that
permits certain operations such as iterating and pushing and popping
values from (at least) one end.
There is no &#34;Stack&#34; Python class, instead we use the `cons list`_, a
venerable two-tuple recursive sequence datastructure, where the
empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the
recursive form of a stack with one or more items on it::
stack := () | (item, stack)
Putting some numbers onto a stack::
()
(1, ())
(2, (1, ()))
(3, (2, (1, ())))
...
Python has very nice &#34;tuple packing and unpacking&#34; in its syntax which
means we can directly &#34;unpack&#34; the expected arguments to a Joy function.
For example::
def dup((head, tail)):
return head, (head, tail)
We replace the argument &#34;stack&#34; by the expected structure of the stack,
in this case &#34;(head, tail)&#34;, and Python takes care of unpacking the
incoming tuple and assigning values to the names. (Note that Python
syntax doesn&#39;t require parentheses around tuples used in expressions
where they would be redundant.)
Unfortunately, the Sphinx documentation generator, which is used to generate this
web page, doesn&#39;t handle tuples in the function parameters. And in Python 3, this
syntax was removed entirely. Instead you would have to write::
def dup(stack):
head, tail = stack
return head, (head, tail)
We have two very simple functions, one to build up a stack from a Python
iterable and another to iterate through a stack and yield its items
one-by-one in order. There are also two functions to generate string representations
of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are
printed left-to-right. These functions are written to support :doc:`../pretty`.
.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
@ -13158,26 +13225,66 @@ joy? </code></pre>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">list_to_stack</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">list_to_stack</span><span class="p">([</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">])</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[2]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>(1, (2, (3, ())))</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))))</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">((</span><span class="mi">1</span><span class="p">,</span> <span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="p">(</span><span class="mi">3</span><span class="p">,</span> <span class="p">())))))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt output_prompt">Out[3]:</div>
<div class="output_text output_subarea output_execute_result">
<pre>[1, 2, 3]</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
@ -13192,19 +13299,38 @@ joy? </code></pre>
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">stack</span> <span class="o">=</span> <span class="p">()</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">stack</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">[</span><span class="mi">1</span><span class="p">,</span> <span class="mi">2</span><span class="p">,</span> <span class="mi">3</span><span class="p">]:</span>
<span class="n">stack</span> <span class="o">=</span> <span class="n">n</span><span class="p">,</span> <span class="n">stack</span>
<span class="nb">print</span> <span class="n">stack</span>
<span class="nb">print</span> <span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">))</span>
<span class="nb">print</span><span class="p">(</span><span class="n">stack</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">utils</span><span class="o">.</span><span class="n">stack</span><span class="o">.</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">stack</span><span class="p">)))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>(3, (2, (1, ())))
[3, 2, 1]
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
@ -13228,15 +13354,64 @@ joy? </code></pre>
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">joy.joy</span>
<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="o">.</span><span class="n">joy</span><span class="p">))</span>
</pre></div>
</div>
</div>
</div>
<div class="output_wrapper">
<div class="output">
<div class="output_area">
<div class="prompt"></div>
<div class="output_subarea output_stream output_stdout output_text">
<pre>def joy(stack, expression, dictionary, viewer=None):
&#39;&#39;&#39;Evaluate a Joy expression on a stack.
This function iterates through a sequence of terms which are either
literals (strings, numbers, sequences of terms) or function symbols.
Literals are put onto the stack and functions are looked up in the
disctionary and executed.
The viewer is a function that is called with the stack and expression
on every iteration, its return value is ignored.
:param stack stack: The stack.
:param stack expression: The expression to evaluate.
:param dict dictionary: A ``dict`` mapping names to Joy functions.
:param function viewer: Optional viewer function.
:rtype: (stack, (), dictionary)
&#39;&#39;&#39;
while expression:
if viewer: viewer(stack, expression)
term, expression = expression
if isinstance(term, Symbol):
term = dictionary[term]
stack, expression, dictionary = term(stack, expression, dictionary)
else:
stack = term, stack
if viewer: viewer(stack, expression)
return stack, expression, dictionary
</pre>
</div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing text_cell rendered"><div class="prompt input_prompt">
</div><div class="inner_cell">
@ -13274,9 +13449,9 @@ joy? </code></pre>
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">joy.parser</span>
<span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getdoc</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="p">))</span>
</pre></div>
</div>
@ -13330,7 +13505,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">_parse</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">_parse</span><span class="p">))</span>
</pre></div>
</div>
@ -13391,7 +13566,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 2 3 4 5&#39;</span><span class="p">)</span> <span class="c1"># A simple sequence.</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 2 3 4 5&#39;</span><span class="p">)</span> <span class="c1"># A simple sequence.</span>
</pre></div>
</div>
@ -13424,7 +13599,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[1 2 3] 4 5&#39;</span><span class="p">)</span> <span class="c1"># Three items, the first is a list with three items</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[1 2 3] 4 5&#39;</span><span class="p">)</span> <span class="c1"># Three items, the first is a list with three items</span>
</pre></div>
</div>
@ -13457,7 +13632,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 23 [&quot;four&quot; [-5.0] cons] 8888&#39;</span><span class="p">)</span> <span class="c1"># A mixed bag. cons is</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;1 23 [&quot;four&quot; [-5.0] cons] 8888&#39;</span><span class="p">)</span> <span class="c1"># A mixed bag. cons is</span>
<span class="c1"># a Symbol, no lookup at</span>
<span class="c1"># parse-time. Haiku docs.</span>
</pre></div>
@ -13492,7 +13667,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[][][][][]&#39;</span><span class="p">)</span> <span class="c1"># Five empty lists.</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[][][][][]&#39;</span><span class="p">)</span> <span class="c1"># Five empty lists.</span>
</pre></div>
</div>
@ -13525,7 +13700,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[[[[[]]]]]&#39;</span><span class="p">)</span> <span class="c1"># Five nested lists.</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">joy</span><span class="o">.</span><span class="n">parser</span><span class="o">.</span><span class="n">text_to_expression</span><span class="p">(</span><span class="s1">&#39;[[[[[]]]]]&#39;</span><span class="p">)</span> <span class="c1"># Five nested lists.</span>
</pre></div>
</div>
@ -13566,9 +13741,9 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">import</span> <span class="nn">joy.library</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">import</span> <span class="nn">joy.library</span>
<span class="nb">print</span> <span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">initialize</span><span class="p">()))</span>
<span class="nb">print</span><span class="p">(</span><span class="s1">&#39; &#39;</span><span class="o">.</span><span class="n">join</span><span class="p">(</span><span class="nb">sorted</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">initialize</span><span class="p">())))</span>
</pre></div>
</div>
@ -13585,7 +13760,7 @@ around square brackets.
<div class="output_subarea output_stream output_stdout output_text">
<pre>!= % &amp; * *fraction *fraction0 + ++ - -- / // /floor &lt; &lt;&lt; &lt;= &lt;&gt; = &gt; &gt;= &gt;&gt; ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll&lt; roll&gt; rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
<pre>!= % &amp; * *fraction *fraction0 + ++ - -- / // /floor &lt; &lt;&lt; &lt;= &lt;&gt; = &gt; &gt;= &gt;&gt; ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll&lt; roll&gt; rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
</pre>
</div>
</div>
@ -13607,7 +13782,7 @@ around square brackets.
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">print</span> <span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">dip</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">inspect</span><span class="o">.</span><span class="n">getsource</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">dip</span><span class="p">))</span>
</pre></div>
</div>
@ -13625,7 +13800,6 @@ around square brackets.
<div class="output_subarea output_stream output_stdout output_text">
<pre>@inscribe
@combinator_effect(_COMB_NUMS(), a1, s1)
@FunctionWrapper
def dip(stack, expression, dictionary):
&#39;&#39;&#39;
@ -13664,7 +13838,7 @@ def dip(stack, expression, dictionary):
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="nb">print</span> <span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">definitions</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="nb">print</span><span class="p">(</span><span class="n">joy</span><span class="o">.</span><span class="n">library</span><span class="o">.</span><span class="n">definitions</span><span class="p">)</span>
</pre></div>
</div>
@ -13681,43 +13855,44 @@ def dip(stack, expression, dictionary):
<div class="output_subarea output_stream output_stdout output_text">
<pre>? == dup truthy
*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 == concat [[swap] dip * [*] dip] infra
anamorphism == [pop []] swap [dip swons] genrec
average == [sum 1.0 *] [size] cleave /
binary == nullary [popop] dip
cleave == fork [popd] dip
codireco == cons dip rest cons
dinfrirst == dip infra first
unstack == ? [uncons ?] loop pop
down_to_zero == [0 &gt;] [dup --] while
dupdipd == dup dipd
enstacken == stack [clear] dip
flatten == [] swap [concat] step
fork == [i] app2
gcd == 1 [tuck modulus dup 0 &gt;] loop pop
ifte == [nullary not] dipd branch
ii == [dip] dupdip i
least_fraction == dup [gcd] infra [div] concat map
make_generator == [codireco] ccons
nullary == [stack] dinfrirst
of == swap at
pam == [i] map
primrec == [i] genrec
product == 1 swap [*] step
quoted == [unit] dip
range == [0 &lt;=] [1 - dup] anamorphism
range_to_zero == unit [down_to_zero] infra
run == [] swap infra
size == 0 swap [pop ++] step
sqr == dup mul
step_zero == 0 roll&gt; step
swoncat == swap concat
ternary == unary [popop] dip
unary == nullary popd
unquoted == [i] dip
while == swap [nullary] cons dup dipd concat loop
<pre>? dup truthy
*fraction [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 concat [[swap] dip * [*] dip] infra
anamorphism [pop []] swap [dip swons] genrec
average [sum 1.0 *] [size] cleave /
binary nullary [popop] dip
cleave fork [popd] dip
codireco cons dip rest cons
dinfrirst dip infra first
unstack ? [uncons ?] loop pop
down_to_zero [0 &gt;] [dup --] while
dupdipd dup dipd
enstacken stack [clear] dip
flatten [] swap [concat] step
fork [i] app2
gcd 1 [tuck modulus dup 0 &gt;] loop pop
ifte [nullary not] dipd branch
ii [dip] dupdip i
least_fraction dup [gcd] infra [div] concat map
make_generator [codireco] ccons
nullary [stack] dinfrirst
of swap at
pam [i] map
tailrec [i] genrec
product 1 swap [*] step
quoted [unit] dip
range [0 &lt;=] [1 - dup] anamorphism
range_to_zero unit [down_to_zero] infra
run [] swap infra
size 0 swap [pop ++] step
sqr dup mul
step_zero 0 roll&gt; step
swoncat swap concat
tailrec [i] genrec
ternary unary [popop] dip
unary nullary popd
unquoted [i] dip
while swap [nullary] cons dup dipd concat loop
</pre>
</div>
@ -13746,7 +13921,7 @@ while == swap [nullary] cons dup dipd concat loop
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>

View File

@ -549,9 +549,9 @@
"\ton the rest of the stack.\n",
"\t::\n",
"\n",
"\t\t\t ... x [Q] dip\n",
"\t\t ... x [Q] dip\n",
"\t\t-------------------\n",
"\t\t\t\t ... Q x\n",
"\t\t ... Q x\n",
"\n",
"\t'''\n",
"\t(quote, (x, stack)) = stack\n",

View File

@ -48,9 +48,61 @@ import inspect
import joy.utils.stack
print inspect.getdoc(joy.utils.stack)
print(inspect.getdoc(joy.utils.stack))
```
When talking about Joy we use the terms "stack", "quote", "sequence",
"list", and others to mean the same thing: a simple linear datatype that
permits certain operations such as iterating and pushing and popping
values from (at least) one end.
There is no "Stack" Python class, instead we use the `cons list`_, a
venerable two-tuple recursive sequence datastructure, where the
empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the
recursive form of a stack with one or more items on it::
stack := () | (item, stack)
Putting some numbers onto a stack::
()
(1, ())
(2, (1, ()))
(3, (2, (1, ())))
...
Python has very nice "tuple packing and unpacking" in its syntax which
means we can directly "unpack" the expected arguments to a Joy function.
For example::
def dup((head, tail)):
return head, (head, tail)
We replace the argument "stack" by the expected structure of the stack,
in this case "(head, tail)", and Python takes care of unpacking the
incoming tuple and assigning values to the names. (Note that Python
syntax doesn't require parentheses around tuples used in expressions
where they would be redundant.)
Unfortunately, the Sphinx documentation generator, which is used to generate this
web page, doesn't handle tuples in the function parameters. And in Python 3, this
syntax was removed entirely. Instead you would have to write::
def dup(stack):
head, tail = stack
return head, (head, tail)
We have two very simple functions, one to build up a stack from a Python
iterable and another to iterate through a stack and yield its items
one-by-one in order. There are also two functions to generate string representations
of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are
printed left-to-right. These functions are written to support :doc:`../pretty`.
.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists
### The utility functions maintain order.
The 0th item in the list will be on the top of the stack and *vise versa*.
@ -60,10 +112,24 @@ joy.utils.stack.list_to_stack([1, 2, 3])
```
(1, (2, (3, ())))
```python
list(joy.utils.stack.iter_stack((1, (2, (3, ())))))
```
[1, 2, 3]
This requires reversing the sequence (or iterating backwards) otherwise:
@ -73,10 +139,14 @@ stack = ()
for n in [1, 2, 3]:
stack = n, stack
print stack
print list(joy.utils.stack.iter_stack(stack))
print(stack)
print(list(joy.utils.stack.iter_stack(stack)))
```
(3, (2, (1, ())))
[3, 2, 1]
### Purely Functional Datastructures.
Because Joy lists are made out of Python tuples they are immutable, so all Joy datastructures are *[purely functional](https://en.wikipedia.org/wiki/Purely_functional_data_structure)*.
@ -90,9 +160,43 @@ Each function is passed the stack, expression, and dictionary and returns them.
```python
import joy.joy
print inspect.getsource(joy.joy.joy)
print(inspect.getsource(joy.joy.joy))
```
def joy(stack, expression, dictionary, viewer=None):
'''Evaluate a Joy expression on a stack.
This function iterates through a sequence of terms which are either
literals (strings, numbers, sequences of terms) or function symbols.
Literals are put onto the stack and functions are looked up in the
disctionary and executed.
The viewer is a function that is called with the stack and expression
on every iteration, its return value is ignored.
:param stack stack: The stack.
:param stack expression: The expression to evaluate.
:param dict dictionary: A ``dict`` mapping names to Joy functions.
:param function viewer: Optional viewer function.
:rtype: (stack, (), dictionary)
'''
while expression:
if viewer: viewer(stack, expression)
term, expression = expression
if isinstance(term, Symbol):
term = dictionary[term]
stack, expression, dictionary = term(stack, expression, dictionary)
else:
stack = term, stack
if viewer: viewer(stack, expression)
return stack, expression, dictionary
### View function
The `joy()` function accepts a "viewer" function which it calls on each iteration passing the current stack and expression just before evaluation. This can be used for tracing, breakpoints, retrying after exceptions, or interrupting an evaluation and saving to disk or sending over the network to resume later. The stack and expression together contain all the state of the computation at each step.
@ -109,7 +213,7 @@ One day I thought, What happens if you rewrite Joy to use [CSP](https://en.wikip
```python
import joy.parser
print inspect.getdoc(joy.parser)
print(inspect.getdoc(joy.parser))
```
This module exports a single function for converting text to a joy
@ -134,7 +238,7 @@ The parser is extremely simple, the undocumented `re.Scanner` class does most of
```python
print inspect.getsource(joy.parser._parse)
print(inspect.getsource(joy.parser._parse))
```
def _parse(tokens):
@ -233,21 +337,20 @@ The Joy library of functions (aka commands, or "words" after Forth usage) encaps
```python
import joy.library
print ' '.join(sorted(joy.library.initialize()))
print(' '.join(sorted(joy.library.initialize())))
```
!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
Many of the functions are defined in Python, like `dip`:
```python
print inspect.getsource(joy.library.dip)
print(inspect.getsource(joy.library.dip))
```
@inscribe
@combinator_effect(_COMB_NUMS(), a1, s1)
@FunctionWrapper
def dip(stack, expression, dictionary):
'''
@ -271,46 +374,47 @@ Some functions are defined in equations in terms of other functions. When the i
```python
print joy.library.definitions
print(joy.library.definitions)
```
? == dup truthy
*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 == concat [[swap] dip * [*] dip] infra
anamorphism == [pop []] swap [dip swons] genrec
average == [sum 1.0 *] [size] cleave /
binary == nullary [popop] dip
cleave == fork [popd] dip
codireco == cons dip rest cons
dinfrirst == dip infra first
unstack == ? [uncons ?] loop pop
down_to_zero == [0 >] [dup --] while
dupdipd == dup dipd
enstacken == stack [clear] dip
flatten == [] swap [concat] step
fork == [i] app2
gcd == 1 [tuck modulus dup 0 >] loop pop
ifte == [nullary not] dipd branch
ii == [dip] dupdip i
least_fraction == dup [gcd] infra [div] concat map
make_generator == [codireco] ccons
nullary == [stack] dinfrirst
of == swap at
pam == [i] map
primrec == [i] genrec
product == 1 swap [*] step
quoted == [unit] dip
range == [0 <=] [1 - dup] anamorphism
range_to_zero == unit [down_to_zero] infra
run == [] swap infra
size == 0 swap [pop ++] step
sqr == dup mul
step_zero == 0 roll> step
swoncat == swap concat
ternary == unary [popop] dip
unary == nullary popd
unquoted == [i] dip
while == swap [nullary] cons dup dipd concat loop
? dup truthy
*fraction [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 concat [[swap] dip * [*] dip] infra
anamorphism [pop []] swap [dip swons] genrec
average [sum 1.0 *] [size] cleave /
binary nullary [popop] dip
cleave fork [popd] dip
codireco cons dip rest cons
dinfrirst dip infra first
unstack ? [uncons ?] loop pop
down_to_zero [0 >] [dup --] while
dupdipd dup dipd
enstacken stack [clear] dip
flatten [] swap [concat] step
fork [i] app2
gcd 1 [tuck modulus dup 0 >] loop pop
ifte [nullary not] dipd branch
ii [dip] dupdip i
least_fraction dup [gcd] infra [div] concat map
make_generator [codireco] ccons
nullary [stack] dinfrirst
of swap at
pam [i] map
tailrec [i] genrec
product 1 swap [*] step
quoted [unit] dip
range [0 <=] [1 - dup] anamorphism
range_to_zero unit [down_to_zero] infra
run [] swap infra
size 0 swap [pop ++] step
sqr dup mul
step_zero 0 roll> step
swoncat swap concat
tailrec [i] genrec
ternary unary [popop] dip
unary nullary popd
unquoted [i] dip
while swap [nullary] cons dup dipd concat loop

View File

@ -65,13 +65,68 @@ both the stack and the expression. It is a `cons
list <https://en.wikipedia.org/wiki/Cons#Lists>`__ made from Python
tuples.
.. code:: ipython2
.. code:: ipython3
import inspect
import joy.utils.stack
print inspect.getdoc(joy.utils.stack)
print(inspect.getdoc(joy.utils.stack))
.. parsed-literal::
When talking about Joy we use the terms "stack", "quote", "sequence",
"list", and others to mean the same thing: a simple linear datatype that
permits certain operations such as iterating and pushing and popping
values from (at least) one end.
There is no "Stack" Python class, instead we use the `cons list`_, a
venerable two-tuple recursive sequence datastructure, where the
empty tuple ``()`` is the empty stack and ``(head, rest)`` gives the
recursive form of a stack with one or more items on it::
stack := () | (item, stack)
Putting some numbers onto a stack::
()
(1, ())
(2, (1, ()))
(3, (2, (1, ())))
...
Python has very nice "tuple packing and unpacking" in its syntax which
means we can directly "unpack" the expected arguments to a Joy function.
For example::
def dup((head, tail)):
return head, (head, tail)
We replace the argument "stack" by the expected structure of the stack,
in this case "(head, tail)", and Python takes care of unpacking the
incoming tuple and assigning values to the names. (Note that Python
syntax doesn't require parentheses around tuples used in expressions
where they would be redundant.)
Unfortunately, the Sphinx documentation generator, which is used to generate this
web page, doesn't handle tuples in the function parameters. And in Python 3, this
syntax was removed entirely. Instead you would have to write::
def dup(stack):
head, tail = stack
return head, (head, tail)
We have two very simple functions, one to build up a stack from a Python
iterable and another to iterate through a stack and yield its items
one-by-one in order. There are also two functions to generate string representations
of stacks. They only differ in that one prints the terms in stack from left-to-right while the other prints from right-to-left. In both functions *internal stacks* are
printed left-to-right. These functions are written to support :doc:`../pretty`.
.. _cons list: https://en.wikipedia.org/wiki/Cons#Lists
The utility functions maintain order.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -79,32 +134,57 @@ The utility functions maintain order.
The 0th item in the list will be on the top of the stack and *vise
versa*.
.. code:: ipython2
.. code:: ipython3
joy.utils.stack.list_to_stack([1, 2, 3])
.. code:: ipython2
.. parsed-literal::
(1, (2, (3, ())))
.. code:: ipython3
list(joy.utils.stack.iter_stack((1, (2, (3, ())))))
.. parsed-literal::
[1, 2, 3]
This requires reversing the sequence (or iterating backwards) otherwise:
.. code:: ipython2
.. code:: ipython3
stack = ()
for n in [1, 2, 3]:
stack = n, stack
print stack
print list(joy.utils.stack.iter_stack(stack))
print(stack)
print(list(joy.utils.stack.iter_stack(stack)))
.. parsed-literal::
(3, (2, (1, ())))
[3, 2, 1]
Purely Functional Datastructures.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Because Joy lists are made out of Python tuples they are immutable, so
all Joy datastructures are `purely
functional <https://en.wikipedia.org/wiki/Purely_functional_data_structure>`__.
all Joy datastructures are *`purely
functional <https://en.wikipedia.org/wiki/Purely_functional_data_structure>`__*.
The ``joy()`` function.
=======================
@ -119,21 +199,58 @@ looks up in the dictionary.
Each function is passed the stack, expression, and dictionary and
returns them. Whatever the function returns becomes the new stack,
expression, and dictionary. (The dictionary is passed to enable
e.g. writing words that let you enter new words into the dictionary at
expression, and dictionary. (The dictionary is passed to enable e.g.
writing words that let you enter new words into the dictionary at
runtime, which nothing does yet and may be a bad idea, and the ``help``
command.)
.. code:: ipython2
.. code:: ipython3
import joy.joy
print inspect.getsource(joy.joy.joy)
print(inspect.getsource(joy.joy.joy))
.. parsed-literal::
def joy(stack, expression, dictionary, viewer=None):
'''Evaluate a Joy expression on a stack.
This function iterates through a sequence of terms which are either
literals (strings, numbers, sequences of terms) or function symbols.
Literals are put onto the stack and functions are looked up in the
disctionary and executed.
The viewer is a function that is called with the stack and expression
on every iteration, its return value is ignored.
:param stack stack: The stack.
:param stack expression: The expression to evaluate.
:param dict dictionary: A ``dict`` mapping names to Joy functions.
:param function viewer: Optional viewer function.
:rtype: (stack, (), dictionary)
'''
while expression:
if viewer: viewer(stack, expression)
term, expression = expression
if isinstance(term, Symbol):
term = dictionary[term]
stack, expression, dictionary = term(stack, expression, dictionary)
else:
stack = term, stack
if viewer: viewer(stack, expression)
return stack, expression, dictionary
View function
~~~~~~~~~~~~~
The ``joy()`` function accepts a “viewer” function which it calls on
The ``joy()`` function accepts a "viewer" function which it calls on
each iteration passing the current stack and expression just before
evaluation. This can be used for tracing, breakpoints, retrying after
exceptions, or interrupting an evaluation and saving to disk or sending
@ -147,7 +264,7 @@ A ``viewer`` records each step of the evaluation of a Joy program. The
``TracePrinter`` has a facility for printing out a trace of the
evaluation, one line per step. Each step is aligned to the current
interpreter position, signified by a period separating the stack on the
left from the pending expression (“continuation”) on the right.
left from the pending expression ("continuation") on the right.
`Continuation-Passing Style <https://en.wikipedia.org/wiki/Continuation-passing_style>`__
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
@ -162,11 +279,11 @@ modifying the expression rather than making recursive calls to the
Parser
======
.. code:: ipython2
.. code:: ipython3
import joy.parser
print inspect.getdoc(joy.parser)
print(inspect.getdoc(joy.parser))
.. parsed-literal::
@ -191,12 +308,12 @@ Parser
The parser is extremely simple, the undocumented ``re.Scanner`` class
does most of the tokenizing work and then you just build the tuple
structure out of the tokens. Theres no Abstract Syntax Tree or anything
structure out of the tokens. There's no Abstract Syntax Tree or anything
like that.
.. code:: ipython2
.. code:: ipython3
print inspect.getsource(joy.parser._parse)
print(inspect.getsource(joy.parser._parse))
.. parsed-literal::
@ -226,9 +343,9 @@ like that.
Thats pretty much all there is to it.
That's pretty much all there is to it.
.. code:: ipython2
.. code:: ipython3
joy.parser.text_to_expression('1 2 3 4 5') # A simple sequence.
@ -241,7 +358,7 @@ Thats pretty much all there is to it.
.. code:: ipython2
.. code:: ipython3
joy.parser.text_to_expression('[1 2 3] 4 5') # Three items, the first is a list with three items
@ -254,7 +371,7 @@ Thats pretty much all there is to it.
.. code:: ipython2
.. code:: ipython3
joy.parser.text_to_expression('1 23 ["four" [-5.0] cons] 8888') # A mixed bag. cons is
# a Symbol, no lookup at
@ -269,7 +386,7 @@ Thats pretty much all there is to it.
.. code:: ipython2
.. code:: ipython3
joy.parser.text_to_expression('[][][][][]') # Five empty lists.
@ -282,7 +399,7 @@ Thats pretty much all there is to it.
.. code:: ipython2
.. code:: ipython3
joy.parser.text_to_expression('[[[[[]]]]]') # Five nested lists.
@ -298,35 +415,34 @@ Thats pretty much all there is to it.
Library
=======
The Joy library of functions (aka commands, or “words” after Forth
The Joy library of functions (aka commands, or "words" after Forth
usage) encapsulates all the actual functionality (no pun intended) of
the Joy system. There are simple functions such as addition ``add`` (or
``+``, the library module supports aliases), and combinators which
provide control-flow and higher-order operations.
.. code:: ipython2
.. code:: ipython3
import joy.library
print ' '.join(sorted(joy.library.initialize()))
print(' '.join(sorted(joy.library.initialize())))
.. parsed-literal::
!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infer infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
!= % & * *fraction *fraction0 + ++ - -- / // /floor < << <= <> = > >= >> ? ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add anamorphism and app1 app2 app3 at average b binary bool branch ccons choice clear cleave cmp codireco concat cond cons dinfrirst dip dipd dipdd disenstacken div divmod down_to_zero drop dup dupd dupdd dupdip dupdipd enstacken eq first first_two flatten floor floordiv fork fourth gcd ge genrec getitem gt help i id ifte ii infra inscribe le least_fraction loop lshift lt make_generator map max min mod modulus mul ne neg not nullary of or over pam parse pick pm pop popd popdd popop popopd popopdd pow pred primrec product quoted range range_to_zero rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift run second select sharing shunt size sort sqr sqrt stack step step_zero stuncons stununcons sub succ sum swaack swap swoncat swons tailrec take ternary third times truediv truthy tuck unary uncons unique unit unquoted unstack unswons void warranty while words x xor zip •
Many of the functions are defined in Python, like ``dip``:
.. code:: ipython2
.. code:: ipython3
print inspect.getsource(joy.library.dip)
print(inspect.getsource(joy.library.dip))
.. parsed-literal::
@inscribe
@combinator_effect(_COMB_NUMS(), a1, s1)
@FunctionWrapper
def dip(stack, expression, dictionary):
'''
@ -351,89 +467,90 @@ When the interpreter executes a definition function that function just
pushes its body expression onto the pending expression (the
continuation) and returns control to the interpreter.
.. code:: ipython2
.. code:: ipython3
print joy.library.definitions
print(joy.library.definitions)
.. parsed-literal::
? == dup truthy
*fraction == [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 == concat [[swap] dip * [*] dip] infra
anamorphism == [pop []] swap [dip swons] genrec
average == [sum 1.0 *] [size] cleave /
binary == nullary [popop] dip
cleave == fork [popd] dip
codireco == cons dip rest cons
dinfrirst == dip infra first
unstack == ? [uncons ?] loop pop
down_to_zero == [0 >] [dup --] while
dupdipd == dup dipd
enstacken == stack [clear] dip
flatten == [] swap [concat] step
fork == [i] app2
gcd == 1 [tuck modulus dup 0 >] loop pop
ifte == [nullary not] dipd branch
ii == [dip] dupdip i
least_fraction == dup [gcd] infra [div] concat map
make_generator == [codireco] ccons
nullary == [stack] dinfrirst
of == swap at
pam == [i] map
primrec == [i] genrec
product == 1 swap [*] step
quoted == [unit] dip
range == [0 <=] [1 - dup] anamorphism
range_to_zero == unit [down_to_zero] infra
run == [] swap infra
size == 0 swap [pop ++] step
sqr == dup mul
step_zero == 0 roll> step
swoncat == swap concat
ternary == unary [popop] dip
unary == nullary popd
unquoted == [i] dip
while == swap [nullary] cons dup dipd concat loop
? dup truthy
*fraction [uncons] dip uncons [swap] dip concat [*] infra [*] dip cons
*fraction0 concat [[swap] dip * [*] dip] infra
anamorphism [pop []] swap [dip swons] genrec
average [sum 1.0 *] [size] cleave /
binary nullary [popop] dip
cleave fork [popd] dip
codireco cons dip rest cons
dinfrirst dip infra first
unstack ? [uncons ?] loop pop
down_to_zero [0 >] [dup --] while
dupdipd dup dipd
enstacken stack [clear] dip
flatten [] swap [concat] step
fork [i] app2
gcd 1 [tuck modulus dup 0 >] loop pop
ifte [nullary not] dipd branch
ii [dip] dupdip i
least_fraction dup [gcd] infra [div] concat map
make_generator [codireco] ccons
nullary [stack] dinfrirst
of swap at
pam [i] map
tailrec [i] genrec
product 1 swap [*] step
quoted [unit] dip
range [0 <=] [1 - dup] anamorphism
range_to_zero unit [down_to_zero] infra
run [] swap infra
size 0 swap [pop ++] step
sqr dup mul
step_zero 0 roll> step
swoncat swap concat
tailrec [i] genrec
ternary unary [popop] dip
unary nullary popd
unquoted [i] dip
while swap [nullary] cons dup dipd concat loop
Currently, theres no function to add new definitions to the dictionary
from “within” Joy code itself. Adding new definitions remains a
Currently, there's no function to add new definitions to the dictionary
from "within" Joy code itself. Adding new definitions remains a
meta-interpreter action. You have to do it yourself, in Python, and wash
your hands afterward.
It would be simple enough to define one, but it would open the door to
*name binding* and break the idea that all state is captured in the
stack and expression. Theres an implicit *standard dictionary* that
stack and expression. There's an implicit *standard dictionary* that
defines the actual semantics of the syntactic stack and expression
datastructures (which only contain symbols, not the actual functions.
Pickle some and see for yourself.)
“There should be only one.”
"There should be only one."
^^^^^^^^^^^^^^^^^^^^^^^^^^^
Which brings me to talking about one of my hopes and dreams for this
notation: “There should be only one.” What I mean is that there should
notation: "There should be only one." What I mean is that there should
be one universal standard dictionary of commands, and all bespoke work
done in a UI for purposes takes place by direct interaction and macros.
There would be a *Grand Refactoring* biannually (two years, not six
months, thats semi-annually) where any new definitions factored out of
months, that's semi-annually) where any new definitions factored out of
the usage and macros of the previous time, along with new algorithms and
such, were entered into the dictionary and posted to e.g. IPFS.
such, were entered into the dictionary and posted to e.g. IPFS.
Code should not burgeon wildly, as it does today. The variety of code
should map more-or-less to the well-factored variety of human
computably-solvable problems. There shouldnt be dozens of chat apps, JS
frameworks, programming languages. Its a waste of time, a `fractal
“thundering herd”
computably-solvable problems. There shouldn't be dozens of chat apps, JS
frameworks, programming languages. It's a waste of time, a `fractal
"thundering herd"
attack <https://en.wikipedia.org/wiki/Thundering_herd_problem>`__ on
human mentality.
Literary Code Library
^^^^^^^^^^^^^^^^^^^^^
If you read over the other notebooks youll see that developing code in
If you read over the other notebooks you'll see that developing code in
Joy is a lot like doing simple mathematics, and the descriptions of the
code resemble math papers. The code also works the first time, no bugs.
If you have any experience programming at all, you are probably

View File

@ -13087,7 +13087,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">joy.joy</span> <span class="kn">import</span> <span class="n">run</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">joy.joy</span> <span class="kn">import</span> <span class="n">run</span>
<span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">initialize</span>
<span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">stack_to_string</span>
<span class="kn">from</span> <span class="nn">joy.utils.pretty_print</span> <span class="kn">import</span> <span class="n">TracePrinter</span>
@ -13111,12 +13111,12 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">D</span> <span class="o">=</span> <span class="n">initialize</span><span class="p">()</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">D</span> <span class="o">=</span> <span class="n">initialize</span><span class="p">()</span>
<span class="n">S</span> <span class="o">=</span> <span class="p">()</span>
<span class="k">def</span> <span class="nf">J</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">stack_to_string</span><span class="p">(</span><span class="n">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">D</span><span class="p">)[</span><span class="mi">0</span><span class="p">])</span>
<span class="nb">print</span><span class="p">(</span><span class="n">stack_to_string</span><span class="p">(</span><span class="n">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">S</span><span class="p">,</span> <span class="n">D</span><span class="p">)[</span><span class="mi">0</span><span class="p">]))</span>
<span class="k">def</span> <span class="nf">V</span><span class="p">(</span><span class="n">text</span><span class="p">):</span>
@ -13142,7 +13142,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 18 +&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 18 +&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13173,7 +13173,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;45 30 gcd&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;45 30 gcd&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13212,7 +13212,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;23 18 +&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;23 18 +&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13229,10 +13229,10 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 23 18 +
23 . 18 +
23 18 . +
41 .
<pre> 23 18 +
23 18 +
23 18 +
41
</pre>
</div>
</div>
@ -13246,7 +13246,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;45 30 gcd&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;45 30 gcd&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13263,28 +13263,28 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 45 30 gcd
45 . 30 gcd
45 30 . gcd
45 30 . 1 [tuck modulus dup 0 &gt;] loop pop
45 30 1 . [tuck modulus dup 0 &gt;] loop pop
45 30 1 [tuck modulus dup 0 &gt;] . loop pop
45 30 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 45 30 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 15 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 15 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 True . [tuck modulus dup 0 &gt;] loop pop
30 15 True [tuck modulus dup 0 &gt;] . loop pop
30 15 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 30 15 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 0 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 0 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 False . [tuck modulus dup 0 &gt;] loop pop
15 0 False [tuck modulus dup 0 &gt;] . loop pop
15 0 . pop
15 .
<pre> 45 30 gcd
45 30 gcd
45 30 gcd
45 30 1 [tuck modulus dup 0 &gt;] loop pop
45 30 1 [tuck modulus dup 0 &gt;] loop pop
45 30 1 [tuck modulus dup 0 &gt;] loop pop
45 30 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 45 30 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 15 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 15 0 &gt; [tuck modulus dup 0 &gt;] loop pop
30 15 True [tuck modulus dup 0 &gt;] loop pop
30 15 True [tuck modulus dup 0 &gt;] loop pop
30 15 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 30 15 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 0 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 0 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 0 False [tuck modulus dup 0 &gt;] loop pop
15 0 False [tuck modulus dup 0 &gt;] loop pop
15 0 pop
15
</pre>
</div>
</div>
@ -13306,7 +13306,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;96 27 gcd&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;96 27 gcd&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13323,42 +13323,42 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 96 27 gcd
96 . 27 gcd
96 27 . gcd
96 27 . 1 [tuck modulus dup 0 &gt;] loop pop
96 27 1 . [tuck modulus dup 0 &gt;] loop pop
96 27 1 [tuck modulus dup 0 &gt;] . loop pop
96 27 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 96 27 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 15 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 15 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 True . [tuck modulus dup 0 &gt;] loop pop
27 15 True [tuck modulus dup 0 &gt;] . loop pop
27 15 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 27 15 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 12 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 12 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 True . [tuck modulus dup 0 &gt;] loop pop
15 12 True [tuck modulus dup 0 &gt;] . loop pop
15 12 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 15 12 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 3 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 3 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 True . [tuck modulus dup 0 &gt;] loop pop
12 3 True [tuck modulus dup 0 &gt;] . loop pop
12 3 . tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 12 3 . modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 . dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 0 . 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 0 0 . &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 False . [tuck modulus dup 0 &gt;] loop pop
3 0 False [tuck modulus dup 0 &gt;] . loop pop
3 0 . pop
3 .
<pre> 96 27 gcd
96 27 gcd
96 27 gcd
96 27 1 [tuck modulus dup 0 &gt;] loop pop
96 27 1 [tuck modulus dup 0 &gt;] loop pop
96 27 1 [tuck modulus dup 0 &gt;] loop pop
96 27 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 96 27 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 15 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 15 0 &gt; [tuck modulus dup 0 &gt;] loop pop
27 15 True [tuck modulus dup 0 &gt;] loop pop
27 15 True [tuck modulus dup 0 &gt;] loop pop
27 15 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 27 15 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 12 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 12 0 &gt; [tuck modulus dup 0 &gt;] loop pop
15 12 True [tuck modulus dup 0 &gt;] loop pop
15 12 True [tuck modulus dup 0 &gt;] loop pop
15 12 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 15 12 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 3 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 3 0 &gt; [tuck modulus dup 0 &gt;] loop pop
12 3 True [tuck modulus dup 0 &gt;] loop pop
12 3 True [tuck modulus dup 0 &gt;] loop pop
12 3 tuck modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 12 3 modulus dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 dup 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 0 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 0 0 &gt; [tuck modulus dup 0 &gt;] loop pop
3 0 False [tuck modulus dup 0 &gt;] loop pop
3 0 False [tuck modulus dup 0 &gt;] loop pop
3 0 pop
3
</pre>
</div>
</div>
@ -13366,19 +13366,6 @@ div#notebook {
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>

View File

@ -39,7 +39,7 @@
"\n",
"\n",
"def J(text):\n",
" print stack_to_string(run(text, S, D)[0])\n",
" print(stack_to_string(run(text, S, D)[0]))\n",
"\n",
"\n",
"def V(text):\n",
@ -107,10 +107,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 18 +\n",
" 23 . 18 +\n",
"23 18 . +\n",
" 41 . \n"
" 23 18 +\n",
" 23 18 +\n",
"23 18 +\n",
" 41 \n"
]
}
],
@ -127,28 +127,28 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 45 30 gcd\n",
" 45 . 30 gcd\n",
" 45 30 . gcd\n",
" 45 30 . 1 [tuck modulus dup 0 >] loop pop\n",
" 45 30 1 . [tuck modulus dup 0 >] loop pop\n",
" 45 30 1 [tuck modulus dup 0 >] . loop pop\n",
" 45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 15 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 15 0 . > [tuck modulus dup 0 >] loop pop\n",
" 30 15 True . [tuck modulus dup 0 >] loop pop\n",
"30 15 True [tuck modulus dup 0 >] . loop pop\n",
" 30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 0 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 0 0 . > [tuck modulus dup 0 >] loop pop\n",
" 15 0 False . [tuck modulus dup 0 >] loop pop\n",
"15 0 False [tuck modulus dup 0 >] . loop pop\n",
" 15 0 . pop\n",
" 15 . \n"
" 45 30 gcd\n",
" 45 30 gcd\n",
" 45 30 gcd\n",
" 45 30 1 [tuck modulus dup 0 >] loop pop\n",
" 45 30 1 [tuck modulus dup 0 >] loop pop\n",
" 45 30 1 [tuck modulus dup 0 >] loop pop\n",
" 45 30 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 45 30 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 15 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 15 0 > [tuck modulus dup 0 >] loop pop\n",
" 30 15 True [tuck modulus dup 0 >] loop pop\n",
"30 15 True [tuck modulus dup 0 >] loop pop\n",
" 30 15 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 30 15 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 0 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 0 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 0 False [tuck modulus dup 0 >] loop pop\n",
"15 0 False [tuck modulus dup 0 >] loop pop\n",
" 15 0 pop\n",
" 15 \n"
]
}
],
@ -172,55 +172,48 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 96 27 gcd\n",
" 96 . 27 gcd\n",
" 96 27 . gcd\n",
" 96 27 . 1 [tuck modulus dup 0 >] loop pop\n",
" 96 27 1 . [tuck modulus dup 0 >] loop pop\n",
" 96 27 1 [tuck modulus dup 0 >] . loop pop\n",
" 96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 15 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 15 0 . > [tuck modulus dup 0 >] loop pop\n",
" 27 15 True . [tuck modulus dup 0 >] loop pop\n",
"27 15 True [tuck modulus dup 0 >] . loop pop\n",
" 27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 12 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 12 0 . > [tuck modulus dup 0 >] loop pop\n",
" 15 12 True . [tuck modulus dup 0 >] loop pop\n",
"15 12 True [tuck modulus dup 0 >] . loop pop\n",
" 15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 3 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 3 0 . > [tuck modulus dup 0 >] loop pop\n",
" 12 3 True . [tuck modulus dup 0 >] loop pop\n",
" 12 3 True [tuck modulus dup 0 >] . loop pop\n",
" 12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 . dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 0 . 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 0 0 . > [tuck modulus dup 0 >] loop pop\n",
" 3 0 False . [tuck modulus dup 0 >] loop pop\n",
" 3 0 False [tuck modulus dup 0 >] . loop pop\n",
" 3 0 . pop\n",
" 3 . \n"
" 96 27 gcd\n",
" 96 27 gcd\n",
" 96 27 gcd\n",
" 96 27 1 [tuck modulus dup 0 >] loop pop\n",
" 96 27 1 [tuck modulus dup 0 >] loop pop\n",
" 96 27 1 [tuck modulus dup 0 >] loop pop\n",
" 96 27 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 96 27 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 15 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 15 0 > [tuck modulus dup 0 >] loop pop\n",
" 27 15 True [tuck modulus dup 0 >] loop pop\n",
"27 15 True [tuck modulus dup 0 >] loop pop\n",
" 27 15 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 27 15 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 12 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 12 0 > [tuck modulus dup 0 >] loop pop\n",
" 15 12 True [tuck modulus dup 0 >] loop pop\n",
"15 12 True [tuck modulus dup 0 >] loop pop\n",
" 15 12 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 15 12 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 3 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 3 0 > [tuck modulus dup 0 >] loop pop\n",
" 12 3 True [tuck modulus dup 0 >] loop pop\n",
" 12 3 True [tuck modulus dup 0 >] loop pop\n",
" 12 3 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 12 3 modulus dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 dup 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 0 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 0 0 > [tuck modulus dup 0 >] loop pop\n",
" 3 0 False [tuck modulus dup 0 >] loop pop\n",
" 3 0 False [tuck modulus dup 0 >] loop pop\n",
" 3 0 pop\n",
" 3 \n"
]
}
],
"source": [
"V('96 27 gcd')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -232,14 +225,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -19,7 +19,7 @@ S = ()
def J(text):
print stack_to_string(run(text, S, D)[0])
print(stack_to_string(run(text, S, D)[0]))
def V(text):
@ -55,10 +55,10 @@ A `viewer` records each step of the evaluation of a Joy program. The `TracePrin
V('23 18 +')
```
. 23 18 +
23 . 18 +
23 18 . +
41 .
23 18 +
23 18 +
23 18 +
41
@ -66,28 +66,28 @@ V('23 18 +')
V('45 30 gcd')
```
. 45 30 gcd
45 . 30 gcd
45 30 . gcd
45 30 . 1 [tuck modulus dup 0 >] loop pop
45 30 1 . [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] . loop pop
45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 15 . dup 0 > [tuck modulus dup 0 >] loop pop
30 15 15 . 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 . > [tuck modulus dup 0 >] loop pop
30 15 True . [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] . loop pop
30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 0 . dup 0 > [tuck modulus dup 0 >] loop pop
15 0 0 . 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 . > [tuck modulus dup 0 >] loop pop
15 0 False . [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] . loop pop
15 0 . pop
15 .
45 30 gcd
45 30 gcd
45 30 gcd
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 45 30 modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 15 dup 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 > [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] loop pop
30 15 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 30 15 modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 0 dup 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 > [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] loop pop
15 0 pop
15
Here's a longer trace.
@ -97,45 +97,40 @@ Here's a longer trace.
V('96 27 gcd')
```
. 96 27 gcd
96 . 27 gcd
96 27 . gcd
96 27 . 1 [tuck modulus dup 0 >] loop pop
96 27 1 . [tuck modulus dup 0 >] loop pop
96 27 1 [tuck modulus dup 0 >] . loop pop
96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 15 . dup 0 > [tuck modulus dup 0 >] loop pop
27 15 15 . 0 > [tuck modulus dup 0 >] loop pop
27 15 15 0 . > [tuck modulus dup 0 >] loop pop
27 15 True . [tuck modulus dup 0 >] loop pop
27 15 True [tuck modulus dup 0 >] . loop pop
27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 12 . dup 0 > [tuck modulus dup 0 >] loop pop
15 12 12 . 0 > [tuck modulus dup 0 >] loop pop
15 12 12 0 . > [tuck modulus dup 0 >] loop pop
15 12 True . [tuck modulus dup 0 >] loop pop
15 12 True [tuck modulus dup 0 >] . loop pop
15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 3 . dup 0 > [tuck modulus dup 0 >] loop pop
12 3 3 . 0 > [tuck modulus dup 0 >] loop pop
12 3 3 0 . > [tuck modulus dup 0 >] loop pop
12 3 True . [tuck modulus dup 0 >] loop pop
12 3 True [tuck modulus dup 0 >] . loop pop
12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 0 . dup 0 > [tuck modulus dup 0 >] loop pop
3 0 0 . 0 > [tuck modulus dup 0 >] loop pop
3 0 0 0 . > [tuck modulus dup 0 >] loop pop
3 0 False . [tuck modulus dup 0 >] loop pop
3 0 False [tuck modulus dup 0 >] . loop pop
3 0 . pop
3 .
96 27 gcd
96 27 gcd
96 27 gcd
96 27 1 [tuck modulus dup 0 >] loop pop
96 27 1 [tuck modulus dup 0 >] loop pop
96 27 1 [tuck modulus dup 0 >] loop pop
96 27 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 96 27 modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 15 dup 0 > [tuck modulus dup 0 >] loop pop
27 15 15 0 > [tuck modulus dup 0 >] loop pop
27 15 15 0 > [tuck modulus dup 0 >] loop pop
27 15 True [tuck modulus dup 0 >] loop pop
27 15 True [tuck modulus dup 0 >] loop pop
27 15 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 27 15 modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 12 dup 0 > [tuck modulus dup 0 >] loop pop
15 12 12 0 > [tuck modulus dup 0 >] loop pop
15 12 12 0 > [tuck modulus dup 0 >] loop pop
15 12 True [tuck modulus dup 0 >] loop pop
15 12 True [tuck modulus dup 0 >] loop pop
15 12 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 15 12 modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 3 dup 0 > [tuck modulus dup 0 >] loop pop
12 3 3 0 > [tuck modulus dup 0 >] loop pop
12 3 3 0 > [tuck modulus dup 0 >] loop pop
12 3 True [tuck modulus dup 0 >] loop pop
12 3 True [tuck modulus dup 0 >] loop pop
12 3 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 12 3 modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 0 dup 0 > [tuck modulus dup 0 >] loop pop
3 0 0 0 > [tuck modulus dup 0 >] loop pop
3 0 0 0 > [tuck modulus dup 0 >] loop pop
3 0 False [tuck modulus dup 0 >] loop pop
3 0 False [tuck modulus dup 0 >] loop pop
3 0 pop
3
```python
```

View File

@ -3,7 +3,7 @@ Preamble
First, import what we need.
.. code:: ipython2
.. code:: ipython3
from joy.joy import run
from joy.library import initialize
@ -13,14 +13,14 @@ First, import what we need.
Define a dictionary, an initial stack, and two helper functions to run
Joy code and print results for us.
.. code:: ipython2
.. code:: ipython3
D = initialize()
S = ()
def J(text):
print stack_to_string(run(text, S, D)[0])
print(stack_to_string(run(text, S, D)[0]))
def V(text):
@ -31,7 +31,7 @@ Joy code and print results for us.
Run some simple programs
~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: ipython2
.. code:: ipython3
J('23 18 +')
@ -41,7 +41,7 @@ Run some simple programs
41
.. code:: ipython2
.. code:: ipython3
J('45 30 gcd')
@ -58,97 +58,96 @@ A ``viewer`` records each step of the evaluation of a Joy program. The
``TracePrinter`` has a facility for printing out a trace of the
evaluation, one line per step. Each step is aligned to the current
interpreter position, signified by a period separating the stack on the
left from the pending expression (“continuation”) on the right. I find
left from the pending expression ("continuation") on the right. I find
these traces beautiful, like a kind of art.
.. code:: ipython2
.. code:: ipython3
V('23 18 +')
.. parsed-literal::
. 23 18 +
23 . 18 +
23 18 . +
41 .
23 18 +
23 18 +
23 18 +
41
.. code:: ipython2
.. code:: ipython3
V('45 30 gcd')
.. parsed-literal::
. 45 30 gcd
45 . 30 gcd
45 30 . gcd
45 30 . 1 [tuck modulus dup 0 >] loop pop
45 30 1 . [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] . loop pop
45 30 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 45 30 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 15 . dup 0 > [tuck modulus dup 0 >] loop pop
30 15 15 . 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 . > [tuck modulus dup 0 >] loop pop
30 15 True . [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] . loop pop
30 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 30 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 0 . dup 0 > [tuck modulus dup 0 >] loop pop
15 0 0 . 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 . > [tuck modulus dup 0 >] loop pop
15 0 False . [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] . loop pop
15 0 . pop
15 .
45 30 gcd
45 30 gcd
45 30 gcd
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 1 [tuck modulus dup 0 >] loop pop
45 30 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 45 30 modulus dup 0 > [tuck modulus dup 0 >] loop pop
30 15 dup 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 > [tuck modulus dup 0 >] loop pop
30 15 15 0 > [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] loop pop
30 15 True [tuck modulus dup 0 >] loop pop
30 15 tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 30 15 modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 0 dup 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 > [tuck modulus dup 0 >] loop pop
15 0 0 0 > [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] loop pop
15 0 False [tuck modulus dup 0 >] loop pop
15 0 pop
15
Heres a longer trace.
Here's a longer trace.
.. code:: ipython2
.. code:: ipython3
V('96 27 gcd')
.. parsed-literal::
. 96 27 gcd
96 . 27 gcd
96 27 . gcd
96 27 . 1 [tuck modulus dup 0 >] loop pop
96 27 1 . [tuck modulus dup 0 >] loop pop
96 27 1 [tuck modulus dup 0 >] . loop pop
96 27 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 96 27 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 15 . dup 0 > [tuck modulus dup 0 >] loop pop
27 15 15 . 0 > [tuck modulus dup 0 >] loop pop
27 15 15 0 . > [tuck modulus dup 0 >] loop pop
27 15 True . [tuck modulus dup 0 >] loop pop
27 15 True [tuck modulus dup 0 >] . loop pop
27 15 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 27 15 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 12 . dup 0 > [tuck modulus dup 0 >] loop pop
15 12 12 . 0 > [tuck modulus dup 0 >] loop pop
15 12 12 0 . > [tuck modulus dup 0 >] loop pop
15 12 True . [tuck modulus dup 0 >] loop pop
15 12 True [tuck modulus dup 0 >] . loop pop
15 12 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 15 12 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 3 . dup 0 > [tuck modulus dup 0 >] loop pop
12 3 3 . 0 > [tuck modulus dup 0 >] loop pop
12 3 3 0 . > [tuck modulus dup 0 >] loop pop
12 3 True . [tuck modulus dup 0 >] loop pop
12 3 True [tuck modulus dup 0 >] . loop pop
12 3 . tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 12 3 . modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 0 . dup 0 > [tuck modulus dup 0 >] loop pop
3 0 0 . 0 > [tuck modulus dup 0 >] loop pop
3 0 0 0 . > [tuck modulus dup 0 >] loop pop
3 0 False . [tuck modulus dup 0 >] loop pop
3 0 False [tuck modulus dup 0 >] . loop pop
3 0 . pop
3 .
• 96 27 gcd
96 • 27 gcd
96 27 • gcd
96 27 • 1 [tuck modulus dup 0 >] loop pop
96 27 1 • [tuck modulus dup 0 >] loop pop
96 27 1 [tuck modulus dup 0 >] • loop pop
96 27 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 96 27 • modulus dup 0 > [tuck modulus dup 0 >] loop pop
27 15 • dup 0 > [tuck modulus dup 0 >] loop pop
27 15 15 • 0 > [tuck modulus dup 0 >] loop pop
27 15 15 0 • > [tuck modulus dup 0 >] loop pop
27 15 True • [tuck modulus dup 0 >] loop pop
27 15 True [tuck modulus dup 0 >] • loop pop
27 15 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 27 15 • modulus dup 0 > [tuck modulus dup 0 >] loop pop
15 12 • dup 0 > [tuck modulus dup 0 >] loop pop
15 12 12 • 0 > [tuck modulus dup 0 >] loop pop
15 12 12 0 • > [tuck modulus dup 0 >] loop pop
15 12 True • [tuck modulus dup 0 >] loop pop
15 12 True [tuck modulus dup 0 >] • loop pop
15 12 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 15 12 • modulus dup 0 > [tuck modulus dup 0 >] loop pop
12 3 • dup 0 > [tuck modulus dup 0 >] loop pop
12 3 3 • 0 > [tuck modulus dup 0 >] loop pop
12 3 3 0 • > [tuck modulus dup 0 >] loop pop
12 3 True • [tuck modulus dup 0 >] loop pop
12 3 True [tuck modulus dup 0 >] • loop pop
12 3 • tuck modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 12 3 • modulus dup 0 > [tuck modulus dup 0 >] loop pop
3 0 • dup 0 > [tuck modulus dup 0 >] loop pop
3 0 0 • 0 > [tuck modulus dup 0 >] loop pop
3 0 0 0 • > [tuck modulus dup 0 >] loop pop
3 0 False • [tuck modulus dup 0 >] loop pop
3 0 False [tuck modulus dup 0 >] • loop pop
3 0 • pop
3 •

File diff suppressed because it is too large Load Diff

View File

@ -425,16 +425,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 1 [dup] 3 unquoted\n",
" 1 . [dup] 3 unquoted\n",
" 1 [dup] . 3 unquoted\n",
" 1 [dup] 3 . unquoted\n",
" 1 [dup] 3 . [i] dip\n",
"1 [dup] 3 [i] . dip\n",
" 1 [dup] . i 3\n",
" 1 . dup 3\n",
" 1 1 . 3\n",
" 1 1 3 . \n"
" 1 [dup] 3 unquoted\n",
" 1 [dup] 3 unquoted\n",
" 1 [dup] 3 unquoted\n",
" 1 [dup] 3 unquoted\n",
" 1 [dup] 3 [i] dip\n",
"1 [dup] 3 [i] dip\n",
" 1 [dup] i 3\n",
" 1 dup 3\n",
" 1 1 3\n",
" 1 1 3 \n"
]
}
],
@ -1697,11 +1697,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 ?\n",
" 23 . ?\n",
" 23 . dup truthy\n",
" 23 23 . truthy\n",
"23 True . \n"
" 23 ?\n",
" 23 ?\n",
" 23 dup truthy\n",
" 23 23 truthy\n",
"23 True \n"
]
}
],
@ -2273,13 +2273,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 [++] dupdip *\n",
" 23 . [++] dupdip *\n",
"23 [++] . dupdip *\n",
" 23 . ++ 23 *\n",
" 24 . 23 *\n",
" 24 23 . *\n",
" 552 . \n"
" 23 [++] dupdip *\n",
" 23 [++] dupdip *\n",
"23 [++] dupdip *\n",
" 23 ++ 23 *\n",
" 24 23 *\n",
" 24 23 *\n",
" 552 \n"
]
}
],
@ -2351,7 +2351,7 @@
"Primitive recursive functions are those where R2 == i.\n",
"::\n",
"\n",
" P == [I] [T] [R] primrec\n",
" P == [I] [T] [R] tailrec\n",
" == [I] [T] [R [P] i] ifte\n",
" == [I] [T] [R P] ifte\n",
"\n",
@ -2400,14 +2400,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 1 2 3 [+ +] i\n",
" 1 . 2 3 [+ +] i\n",
" 1 2 . 3 [+ +] i\n",
" 1 2 3 . [+ +] i\n",
"1 2 3 [+ +] . i\n",
" 1 2 3 . + +\n",
" 1 5 . +\n",
" 6 . \n"
" 1 2 3 [+ +] i\n",
" 1 2 3 [+ +] i\n",
" 1 2 3 [+ +] i\n",
" 1 2 3 [+ +] i\n",
"1 2 3 [+ +] i\n",
" 1 2 3 + +\n",
" 1 5 +\n",
" 6 \n"
]
}
],
@ -2473,17 +2473,17 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 1 2 3 [4 5 6] [* +] infra\n",
" 1 . 2 3 [4 5 6] [* +] infra\n",
" 1 2 . 3 [4 5 6] [* +] infra\n",
" 1 2 3 . [4 5 6] [* +] infra\n",
" 1 2 3 [4 5 6] . [* +] infra\n",
"1 2 3 [4 5 6] [* +] . infra\n",
" 6 5 4 . * + [3 2 1] swaack\n",
" 6 20 . + [3 2 1] swaack\n",
" 26 . [3 2 1] swaack\n",
" 26 [3 2 1] . swaack\n",
" 1 2 3 [26] . \n"
" 1 2 3 [4 5 6] [* +] infra\n",
" 1 2 3 [4 5 6] [* +] infra\n",
" 1 2 3 [4 5 6] [* +] infra\n",
" 1 2 3 [4 5 6] [* +] infra\n",
" 1 2 3 [4 5 6] [* +] infra\n",
"1 2 3 [4 5 6] [* +] infra\n",
" 6 5 4 * + [3 2 1] swaack\n",
" 6 20 + [3 2 1] swaack\n",
" 26 [3 2 1] swaack\n",
" 26 [3 2 1] swaack\n",
" 1 2 3 [26] \n"
]
}
],
@ -2540,26 +2540,26 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 3 dup [1 - dup] loop\n",
" 3 . dup [1 - dup] loop\n",
" 3 3 . [1 - dup] loop\n",
"3 3 [1 - dup] . loop\n",
" 3 . 1 - dup [1 - dup] loop\n",
" 3 1 . - dup [1 - dup] loop\n",
" 2 . dup [1 - dup] loop\n",
" 2 2 . [1 - dup] loop\n",
"2 2 [1 - dup] . loop\n",
" 2 . 1 - dup [1 - dup] loop\n",
" 2 1 . - dup [1 - dup] loop\n",
" 1 . dup [1 - dup] loop\n",
" 1 1 . [1 - dup] loop\n",
"1 1 [1 - dup] . loop\n",
" 1 . 1 - dup [1 - dup] loop\n",
" 1 1 . - dup [1 - dup] loop\n",
" 0 . dup [1 - dup] loop\n",
" 0 0 . [1 - dup] loop\n",
"0 0 [1 - dup] . loop\n",
" 0 . \n"
" 3 dup [1 - dup] loop\n",
" 3 dup [1 - dup] loop\n",
" 3 3 [1 - dup] loop\n",
"3 3 [1 - dup] loop\n",
" 3 1 - dup [1 - dup] loop\n",
" 3 1 - dup [1 - dup] loop\n",
" 2 dup [1 - dup] loop\n",
" 2 2 [1 - dup] loop\n",
"2 2 [1 - dup] loop\n",
" 2 1 - dup [1 - dup] loop\n",
" 2 1 - dup [1 - dup] loop\n",
" 1 dup [1 - dup] loop\n",
" 1 1 [1 - dup] loop\n",
"1 1 [1 - dup] loop\n",
" 1 1 - dup [1 - dup] loop\n",
" 1 1 - dup [1 - dup] loop\n",
" 0 dup [1 - dup] loop\n",
" 0 0 [1 - dup] loop\n",
"0 0 [1 - dup] loop\n",
" 0 \n"
]
}
],
@ -2744,23 +2744,23 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 0 [1 2 3] [+] step\n",
" 0 . [1 2 3] [+] step\n",
" 0 [1 2 3] . [+] step\n",
"0 [1 2 3] [+] . step\n",
" 0 1 [+] . i [2 3] [+] step\n",
" 0 1 . + [2 3] [+] step\n",
" 1 . [2 3] [+] step\n",
" 1 [2 3] . [+] step\n",
" 1 [2 3] [+] . step\n",
" 1 2 [+] . i [3] [+] step\n",
" 1 2 . + [3] [+] step\n",
" 3 . [3] [+] step\n",
" 3 [3] . [+] step\n",
" 3 [3] [+] . step\n",
" 3 3 [+] . i\n",
" 3 3 . +\n",
" 6 . \n"
" 0 [1 2 3] [+] step\n",
" 0 [1 2 3] [+] step\n",
" 0 [1 2 3] [+] step\n",
"0 [1 2 3] [+] step\n",
" 0 1 [+] i [2 3] [+] step\n",
" 0 1 + [2 3] [+] step\n",
" 1 [2 3] [+] step\n",
" 1 [2 3] [+] step\n",
" 1 [2 3] [+] step\n",
" 1 2 [+] i [3] [+] step\n",
" 1 2 + [3] [+] step\n",
" 3 [3] [+] step\n",
" 3 [3] [+] step\n",
" 3 [3] [+] step\n",
" 3 3 [+] i\n",
" 3 3 +\n",
" 6 \n"
]
}
],
@ -2784,18 +2784,18 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 3 2 1 2 [+] times\n",
" 3 . 2 1 2 [+] times\n",
" 3 2 . 1 2 [+] times\n",
" 3 2 1 . 2 [+] times\n",
" 3 2 1 2 . [+] times\n",
"3 2 1 2 [+] . times\n",
" 3 2 1 . + 1 [+] times\n",
" 3 3 . 1 [+] times\n",
" 3 3 1 . [+] times\n",
" 3 3 1 [+] . times\n",
" 3 3 . +\n",
" 6 . \n"
" 3 2 1 2 [+] times\n",
" 3 2 1 2 [+] times\n",
" 3 2 1 2 [+] times\n",
" 3 2 1 2 [+] times\n",
" 3 2 1 2 [+] times\n",
"3 2 1 2 [+] times\n",
" 3 2 1 + 1 [+] times\n",
" 3 3 1 [+] times\n",
" 3 3 1 [+] times\n",
" 3 3 1 [+] times\n",
" 3 3 +\n",
" 6 \n"
]
}
],
@ -2848,14 +2848,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 1 2 [3] [4] b\n",
" 1 . 2 [3] [4] b\n",
" 1 2 . [3] [4] b\n",
" 1 2 [3] . [4] b\n",
"1 2 [3] [4] . b\n",
" 1 2 . 3 4\n",
" 1 2 3 . 4\n",
" 1 2 3 4 . \n"
" 1 2 [3] [4] b\n",
" 1 2 [3] [4] b\n",
" 1 2 [3] [4] b\n",
" 1 2 [3] [4] b\n",
"1 2 [3] [4] b\n",
" 1 2 3 4\n",
" 1 2 3 4\n",
" 1 2 3 4 \n"
]
}
],
@ -2934,16 +2934,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 1 [2] [i 3] x\n",
" 1 . [2] [i 3] x\n",
" 1 [2] . [i 3] x\n",
"1 [2] [i 3] . x\n",
"1 [2] [i 3] . i 3\n",
" 1 [2] . i 3 3\n",
" 1 . 2 3 3\n",
" 1 2 . 3 3\n",
" 1 2 3 . 3\n",
" 1 2 3 3 . \n"
" 1 [2] [i 3] x\n",
" 1 [2] [i 3] x\n",
" 1 [2] [i 3] x\n",
"1 [2] [i 3] x\n",
"1 [2] [i 3] i 3\n",
" 1 [2] i 3 3\n",
" 1 2 3 3\n",
" 1 2 3 3\n",
" 1 2 3 3\n",
" 1 2 3 3 \n"
]
}
],
@ -3037,14 +3037,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -184,16 +184,16 @@ J('1 [2] 3 unquoted')
V('1 [dup] 3 unquoted') # Unquoting evaluates. Be aware.
```
. 1 [dup] 3 unquoted
1 . [dup] 3 unquoted
1 [dup] . 3 unquoted
1 [dup] 3 . unquoted
1 [dup] 3 . [i] dip
1 [dup] 3 [i] . dip
1 [dup] . i 3
1 . dup 3
1 1 . 3
1 1 3 .
1 [dup] 3 unquoted
1 [dup] 3 unquoted
1 [dup] 3 unquoted
1 [dup] 3 unquoted
1 [dup] 3 [i] dip
1 [dup] 3 [i] dip
1 [dup] i 3
1 dup 3
1 1 3
1 1 3
# List words
@ -730,11 +730,11 @@ J('0 truthy')
V('23 ?')
```
. 23 ?
23 . ?
23 . dup truthy
23 23 . truthy
23 True .
23 ?
23 ?
23 dup truthy
23 23 truthy
23 True
@ -1020,13 +1020,13 @@ Expects a quoted program `[Q]` on the stack and some item under it, `dup` the it
V('23 [++] dupdip *') # N(N + 1)
```
. 23 [++] dupdip *
23 . [++] dupdip *
23 [++] . dupdip *
23 . ++ 23 *
24 . 23 *
24 23 . *
552 .
23 [++] dupdip *
23 [++] dupdip *
23 [++] dupdip *
23 ++ 23 *
24 23 *
24 23 *
552
### `genrec` `primrec`
@ -1084,7 +1084,7 @@ J('[genrec] help')
Primitive recursive functions are those where R2 == i.
::
P == [I] [T] [R] primrec
P == [I] [T] [R] tailrec
== [I] [T] [R [P] i] ifte
== [I] [T] [R P] ifte
@ -1108,14 +1108,14 @@ J('3 [1 <=] [] [dup --] [i *] genrec')
V('1 2 3 [+ +] i')
```
. 1 2 3 [+ +] i
1 . 2 3 [+ +] i
1 2 . 3 [+ +] i
1 2 3 . [+ +] i
1 2 3 [+ +] . i
1 2 3 . + +
1 5 . +
6 .
1 2 3 [+ +] i
1 2 3 [+ +] i
1 2 3 [+ +] i
1 2 3 [+ +] i
1 2 3 [+ +] i
1 2 3 + +
1 5 +
6
### `ifte`
@ -1144,17 +1144,17 @@ J('1 2 [0] [+] [*] ifte')
V('1 2 3 [4 5 6] [* +] infra')
```
. 1 2 3 [4 5 6] [* +] infra
1 . 2 3 [4 5 6] [* +] infra
1 2 . 3 [4 5 6] [* +] infra
1 2 3 . [4 5 6] [* +] infra
1 2 3 [4 5 6] . [* +] infra
1 2 3 [4 5 6] [* +] . infra
6 5 4 . * + [3 2 1] swaack
6 20 . + [3 2 1] swaack
26 . [3 2 1] swaack
26 [3 2 1] . swaack
1 2 3 [26] .
1 2 3 [4 5 6] [* +] infra
1 2 3 [4 5 6] [* +] infra
1 2 3 [4 5 6] [* +] infra
1 2 3 [4 5 6] [* +] infra
1 2 3 [4 5 6] [* +] infra
1 2 3 [4 5 6] [* +] infra
6 5 4 * + [3 2 1] swaack
6 20 + [3 2 1] swaack
26 [3 2 1] swaack
26 [3 2 1] swaack
1 2 3 [26]
### `loop`
@ -1188,26 +1188,26 @@ J('[loop] help')
V('3 dup [1 - dup] loop')
```
. 3 dup [1 - dup] loop
3 . dup [1 - dup] loop
3 3 . [1 - dup] loop
3 3 [1 - dup] . loop
3 . 1 - dup [1 - dup] loop
3 1 . - dup [1 - dup] loop
2 . dup [1 - dup] loop
2 2 . [1 - dup] loop
2 2 [1 - dup] . loop
2 . 1 - dup [1 - dup] loop
2 1 . - dup [1 - dup] loop
1 . dup [1 - dup] loop
1 1 . [1 - dup] loop
1 1 [1 - dup] . loop
1 . 1 - dup [1 - dup] loop
1 1 . - dup [1 - dup] loop
0 . dup [1 - dup] loop
0 0 . [1 - dup] loop
0 0 [1 - dup] . loop
0 .
3 dup [1 - dup] loop
3 dup [1 - dup] loop
3 3 [1 - dup] loop
3 3 [1 - dup] loop
3 1 - dup [1 - dup] loop
3 1 - dup [1 - dup] loop
2 dup [1 - dup] loop
2 2 [1 - dup] loop
2 2 [1 - dup] loop
2 1 - dup [1 - dup] loop
2 1 - dup [1 - dup] loop
1 dup [1 - dup] loop
1 1 [1 - dup] loop
1 1 [1 - dup] loop
1 1 - dup [1 - dup] loop
1 1 - dup [1 - dup] loop
0 dup [1 - dup] loop
0 0 [1 - dup] loop
0 0 [1 - dup] loop
0
### `map` `pam`
@ -1303,23 +1303,23 @@ J('[step] help')
V('0 [1 2 3] [+] step')
```
. 0 [1 2 3] [+] step
0 . [1 2 3] [+] step
0 [1 2 3] . [+] step
0 [1 2 3] [+] . step
0 1 [+] . i [2 3] [+] step
0 1 . + [2 3] [+] step
1 . [2 3] [+] step
1 [2 3] . [+] step
1 [2 3] [+] . step
1 2 [+] . i [3] [+] step
1 2 . + [3] [+] step
3 . [3] [+] step
3 [3] . [+] step
3 [3] [+] . step
3 3 [+] . i
3 3 . +
6 .
0 [1 2 3] [+] step
0 [1 2 3] [+] step
0 [1 2 3] [+] step
0 [1 2 3] [+] step
0 1 [+] i [2 3] [+] step
0 1 + [2 3] [+] step
1 [2 3] [+] step
1 [2 3] [+] step
1 [2 3] [+] step
1 2 [+] i [3] [+] step
1 2 + [3] [+] step
3 [3] [+] step
3 [3] [+] step
3 [3] [+] step
3 3 [+] i
3 3 +
6
### `times`
@ -1329,18 +1329,18 @@ V('0 [1 2 3] [+] step')
V('3 2 1 2 [+] times')
```
. 3 2 1 2 [+] times
3 . 2 1 2 [+] times
3 2 . 1 2 [+] times
3 2 1 . 2 [+] times
3 2 1 2 . [+] times
3 2 1 2 [+] . times
3 2 1 . + 1 [+] times
3 3 . 1 [+] times
3 3 1 . [+] times
3 3 1 [+] . times
3 3 . +
6 .
3 2 1 2 [+] times
3 2 1 2 [+] times
3 2 1 2 [+] times
3 2 1 2 [+] times
3 2 1 2 [+] times
3 2 1 2 [+] times
3 2 1 + 1 [+] times
3 3 1 [+] times
3 3 1 [+] times
3 3 1 [+] times
3 3 +
6
### `b`
@ -1370,14 +1370,14 @@ J('[b] help')
V('1 2 [3] [4] b')
```
. 1 2 [3] [4] b
1 . 2 [3] [4] b
1 2 . [3] [4] b
1 2 [3] . [4] b
1 2 [3] [4] . b
1 2 . 3 4
1 2 3 . 4
1 2 3 4 .
1 2 [3] [4] b
1 2 [3] [4] b
1 2 [3] [4] b
1 2 [3] [4] b
1 2 [3] [4] b
1 2 3 4
1 2 3 4
1 2 3 4
### `while`
@ -1419,16 +1419,16 @@ J('[x] help')
V('1 [2] [i 3] x') # Kind of a pointless example.
```
. 1 [2] [i 3] x
1 . [2] [i 3] x
1 [2] . [i 3] x
1 [2] [i 3] . x
1 [2] [i 3] . i 3
1 [2] . i 3 3
1 . 2 3 3
1 2 . 3 3
1 2 3 . 3
1 2 3 3 .
1 [2] [i 3] x
1 [2] [i 3] x
1 [2] [i 3] x
1 [2] [i 3] x
1 [2] [i 3] i 3
1 [2] i 3 3
1 2 3 3
1 2 3 3
1 2 3 3
1 2 3 3
# `void`

File diff suppressed because it is too large Load Diff

View File

@ -13090,7 +13090,7 @@ Find the sum of all the multiples of 3 or 5 below 1000.</code></pre>
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13111,7 +13111,7 @@ Find the sum of all the multiples of 3 or 5 below 1000.</code></pre>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;P == [3 % not] dupdip 5 % not or&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;P [3 % not] dupdip 5 % not or&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13124,7 +13124,7 @@ Find the sum of all the multiples of 3 or 5 below 1000.</code></pre>
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;80 P&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;80 P&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13141,19 +13141,19 @@ Find the sum of all the multiples of 3 or 5 below 1000.</code></pre>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 80 P
80 . P
80 . [3 % not] dupdip 5 % not or
80 [3 % not] . dupdip 5 % not or
80 . 3 % not 80 5 % not or
80 3 . % not 80 5 % not or
2 . not 80 5 % not or
False . 80 5 % not or
False 80 . 5 % not or
False 80 5 . % not or
False 0 . not or
False True . or
True .
<pre> 80 P
80 P
80 [3 % not] dupdip 5 % not or
80 [3 % not] dupdip 5 % not or
80 3 % not 80 5 % not or
80 3 % not 80 5 % not or
2 not 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 0 not or
False True or
True
</pre>
</div>
</div>
@ -13219,7 +13219,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.1 == + [+] dupdip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.1 + [+] dupdip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13232,7 +13232,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 3 PE1.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 3 PE1.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13249,16 +13249,16 @@ counter to the running sum. This function will do that:</p>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 0 0 3 PE1.1
0 . 0 3 PE1.1
0 0 . 3 PE1.1
0 0 3 . PE1.1
0 0 3 . + [+] dupdip
0 3 . [+] dupdip
0 3 [+] . dupdip
0 3 . + 3
3 . 3
3 3 .
<pre> 0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 + [+] dupdip
0 3 [+] dupdip
0 3 [+] dupdip
0 3 + 3
3 3
3 3
</pre>
</div>
</div>
@ -13272,7 +13272,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 [3 2 1 3 1 2 3] [PE1.1] step&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 [3 2 1 3 1 2 3] [PE1.1] step&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13289,79 +13289,79 @@ counter to the running sum. This function will do that:</p>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 . 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 . [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] . [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] . step
0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step
0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step
3 . 3 [2 1 3 1 2 3] [PE1.1] step
3 3 . [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] . [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] . step
3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step
3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step
3 5 . + 5 [1 3 1 2 3] [PE1.1] step
8 . 5 [1 3 1 2 3] [PE1.1] step
8 5 . [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] . [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] . step
8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step
8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 . [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] . dupdip [3 1 2 3] [PE1.1] step
8 6 . + 6 [3 1 2 3] [PE1.1] step
14 . 6 [3 1 2 3] [PE1.1] step
14 6 . [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] . [PE1.1] step
14 6 [3 1 2 3] [PE1.1] . step
14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step
14 6 3 . PE1.1 [1 2 3] [PE1.1] step
14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step
14 9 . [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] . dupdip [1 2 3] [PE1.1] step
14 9 . + 9 [1 2 3] [PE1.1] step
23 . 9 [1 2 3] [PE1.1] step
23 9 . [1 2 3] [PE1.1] step
23 9 [1 2 3] . [PE1.1] step
23 9 [1 2 3] [PE1.1] . step
23 9 1 [PE1.1] . i [2 3] [PE1.1] step
23 9 1 . PE1.1 [2 3] [PE1.1] step
23 9 1 . + [+] dupdip [2 3] [PE1.1] step
23 10 . [+] dupdip [2 3] [PE1.1] step
23 10 [+] . dupdip [2 3] [PE1.1] step
23 10 . + 10 [2 3] [PE1.1] step
33 . 10 [2 3] [PE1.1] step
33 10 . [2 3] [PE1.1] step
33 10 [2 3] . [PE1.1] step
33 10 [2 3] [PE1.1] . step
33 10 2 [PE1.1] . i [3] [PE1.1] step
33 10 2 . PE1.1 [3] [PE1.1] step
33 10 2 . + [+] dupdip [3] [PE1.1] step
33 12 . [+] dupdip [3] [PE1.1] step
33 12 [+] . dupdip [3] [PE1.1] step
33 12 . + 12 [3] [PE1.1] step
45 . 12 [3] [PE1.1] step
45 12 . [3] [PE1.1] step
45 12 [3] . [PE1.1] step
45 12 [3] [PE1.1] . step
45 12 3 [PE1.1] . i
45 12 3 . PE1.1
45 12 3 . + [+] dupdip
45 15 . [+] dupdip
45 15 [+] . dupdip
45 15 . + 15
60 . 15
60 15 .
<pre> 0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 3 [PE1.1] i [2 1 3 1 2 3] [PE1.1] step
0 0 3 PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 + 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 2 [PE1.1] i [1 3 1 2 3] [PE1.1] step
3 3 2 PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 + 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 1 [PE1.1] i [3 1 2 3] [PE1.1] step
8 5 1 PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 + 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 3 [PE1.1] i [1 2 3] [PE1.1] step
14 6 3 PE1.1 [1 2 3] [PE1.1] step
14 6 3 + [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 + 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 1 [PE1.1] i [2 3] [PE1.1] step
23 9 1 PE1.1 [2 3] [PE1.1] step
23 9 1 + [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 + 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 2 [PE1.1] i [3] [PE1.1] step
33 10 2 PE1.1 [3] [PE1.1] step
33 10 2 + [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 + 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 3 [PE1.1] i
45 12 3 PE1.1
45 12 3 + [+] dupdip
45 15 [+] dupdip
45 15 [+] dupdip
45 15 + 15
60 15
60 15
</pre>
</div>
</div>
@ -13383,7 +13383,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mi">1000</span> <span class="o">/</span> <span class="mi">15</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mi">1000</span> <span class="o">/</span> <span class="mi">15</span>
</pre></div>
</div>
@ -13402,7 +13402,7 @@ counter to the running sum. This function will do that:</p>
<div class="output_text output_subarea output_execute_result">
<pre>66</pre>
<pre>66.66666666666667</pre>
</div>
</div>
@ -13416,7 +13416,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mi">66</span> <span class="o">*</span> <span class="mi">15</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mi">66</span> <span class="o">*</span> <span class="mi">15</span>
</pre></div>
</div>
@ -13449,7 +13449,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mi">1000</span> <span class="o">-</span> <span class="mi">990</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mi">1000</span> <span class="o">-</span> <span class="mi">990</span>
</pre></div>
</div>
@ -13490,7 +13490,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mi">999</span> <span class="o">-</span> <span class="mi">990</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mi">999</span> <span class="o">-</span> <span class="mi">990</span>
</pre></div>
</div>
@ -13531,7 +13531,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13544,7 +13544,7 @@ counter to the running sum. This function will do that:</p>
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13592,7 +13592,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mb">0b11100111011011</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mb">0b11100111011011</span>
</pre></div>
</div>
@ -13625,7 +13625,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.2 == [3 &amp; PE1.1] dupdip 2 &gt;&gt;&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.2 [3 &amp; PE1.1] dupdip 2 &gt;&gt;&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13638,7 +13638,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 14811 PE1.2&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 14811 PE1.2&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13655,24 +13655,24 @@ integer terms from the list.</p>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 0 0 14811 PE1.2
0 . 0 14811 PE1.2
0 0 . 14811 PE1.2
0 0 14811 . PE1.2
0 0 14811 . [3 &amp; PE1.1] dupdip 2 &gt;&gt;
0 0 14811 [3 &amp; PE1.1] . dupdip 2 &gt;&gt;
0 0 14811 . 3 &amp; PE1.1 14811 2 &gt;&gt;
0 0 14811 3 . &amp; PE1.1 14811 2 &gt;&gt;
0 0 3 . PE1.1 14811 2 &gt;&gt;
0 0 3 . + [+] dupdip 14811 2 &gt;&gt;
0 3 . [+] dupdip 14811 2 &gt;&gt;
0 3 [+] . dupdip 14811 2 &gt;&gt;
0 3 . + 3 14811 2 &gt;&gt;
3 . 3 14811 2 &gt;&gt;
3 3 . 14811 2 &gt;&gt;
3 3 14811 . 2 &gt;&gt;
3 3 14811 2 . &gt;&gt;
3 3 3702 .
<pre> 0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 [3 &amp; PE1.1] dupdip 2 &gt;&gt;
0 0 14811 [3 &amp; PE1.1] dupdip 2 &gt;&gt;
0 0 14811 3 &amp; PE1.1 14811 2 &gt;&gt;
0 0 14811 3 &amp; PE1.1 14811 2 &gt;&gt;
0 0 3 PE1.1 14811 2 &gt;&gt;
0 0 3 + [+] dupdip 14811 2 &gt;&gt;
0 3 [+] dupdip 14811 2 &gt;&gt;
0 3 [+] dupdip 14811 2 &gt;&gt;
0 3 + 3 14811 2 &gt;&gt;
3 3 14811 2 &gt;&gt;
3 3 14811 2 &gt;&gt;
3 3 14811 2 &gt;&gt;
3 3 14811 2 &gt;&gt;
3 3 3702
</pre>
</div>
</div>
@ -13686,7 +13686,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;3 3 3702 PE1.2&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;3 3 3702 PE1.2&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13703,24 +13703,24 @@ integer terms from the list.</p>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 3 3 3702 PE1.2
3 . 3 3702 PE1.2
3 3 . 3702 PE1.2
3 3 3702 . PE1.2
3 3 3702 . [3 &amp; PE1.1] dupdip 2 &gt;&gt;
3 3 3702 [3 &amp; PE1.1] . dupdip 2 &gt;&gt;
3 3 3702 . 3 &amp; PE1.1 3702 2 &gt;&gt;
3 3 3702 3 . &amp; PE1.1 3702 2 &gt;&gt;
3 3 2 . PE1.1 3702 2 &gt;&gt;
3 3 2 . + [+] dupdip 3702 2 &gt;&gt;
3 5 . [+] dupdip 3702 2 &gt;&gt;
3 5 [+] . dupdip 3702 2 &gt;&gt;
3 5 . + 5 3702 2 &gt;&gt;
8 . 5 3702 2 &gt;&gt;
8 5 . 3702 2 &gt;&gt;
8 5 3702 . 2 &gt;&gt;
8 5 3702 2 . &gt;&gt;
8 5 925 .
<pre> 3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 [3 &amp; PE1.1] dupdip 2 &gt;&gt;
3 3 3702 [3 &amp; PE1.1] dupdip 2 &gt;&gt;
3 3 3702 3 &amp; PE1.1 3702 2 &gt;&gt;
3 3 3702 3 &amp; PE1.1 3702 2 &gt;&gt;
3 3 2 PE1.1 3702 2 &gt;&gt;
3 3 2 + [+] dupdip 3702 2 &gt;&gt;
3 5 [+] dupdip 3702 2 &gt;&gt;
3 5 [+] dupdip 3702 2 &gt;&gt;
3 5 + 5 3702 2 &gt;&gt;
8 5 3702 2 &gt;&gt;
8 5 3702 2 &gt;&gt;
8 5 3702 2 &gt;&gt;
8 5 3702 2 &gt;&gt;
8 5 925
</pre>
</div>
</div>
@ -13734,7 +13734,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 14811 7 [PE1.2] times pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;0 0 14811 7 [PE1.2] times pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13751,137 +13751,130 @@ integer terms from the list.</p>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 0 0 14811 7 [PE1.2] times pop
0 . 0 14811 7 [PE1.2] times pop
0 0 . 14811 7 [PE1.2] times pop
0 0 14811 . 7 [PE1.2] times pop
0 0 14811 7 . [PE1.2] times pop
0 0 14811 7 [PE1.2] . times pop
0 0 14811 [PE1.2] . i 6 [PE1.2] times pop
0 0 14811 . PE1.2 6 [PE1.2] times pop
0 0 14811 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 . 3 &amp; PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 3 . &amp; PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 3 . PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 3 . + [+] dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 . [+] dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 [+] . dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 . + 3 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 . 3 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 3 . 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 3 14811 . 2 &gt;&gt; 6 [PE1.2] times pop
3 3 14811 2 . &gt;&gt; 6 [PE1.2] times pop
3 3 3702 . 6 [PE1.2] times pop
3 3 3702 6 . [PE1.2] times pop
3 3 3702 6 [PE1.2] . times pop
3 3 3702 [PE1.2] . i 5 [PE1.2] times pop
3 3 3702 . PE1.2 5 [PE1.2] times pop
3 3 3702 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 . 3 &amp; PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 3 . &amp; PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 2 . PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 2 . + [+] dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 . [+] dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 [+] . dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 . + 5 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 . 5 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 5 . 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 5 3702 . 2 &gt;&gt; 5 [PE1.2] times pop
8 5 3702 2 . &gt;&gt; 5 [PE1.2] times pop
8 5 925 . 5 [PE1.2] times pop
8 5 925 5 . [PE1.2] times pop
8 5 925 5 [PE1.2] . times pop
8 5 925 [PE1.2] . i 4 [PE1.2] times pop
8 5 925 . PE1.2 4 [PE1.2] times pop
8 5 925 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 . 3 &amp; PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 3 . &amp; PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 1 . PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 1 . + [+] dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 . [+] dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 [+] . dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 . + 6 925 2 &gt;&gt; 4 [PE1.2] times pop
14 . 6 925 2 &gt;&gt; 4 [PE1.2] times pop
14 6 . 925 2 &gt;&gt; 4 [PE1.2] times pop
14 6 925 . 2 &gt;&gt; 4 [PE1.2] times pop
14 6 925 2 . &gt;&gt; 4 [PE1.2] times pop
14 6 231 . 4 [PE1.2] times pop
14 6 231 4 . [PE1.2] times pop
14 6 231 4 [PE1.2] . times pop
14 6 231 [PE1.2] . i 3 [PE1.2] times pop
14 6 231 . PE1.2 3 [PE1.2] times pop
14 6 231 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 . 3 &amp; PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 3 . &amp; PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 3 . PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 3 . + [+] dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 . [+] dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 [+] . dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 . + 9 231 2 &gt;&gt; 3 [PE1.2] times pop
23 . 9 231 2 &gt;&gt; 3 [PE1.2] times pop
23 9 . 231 2 &gt;&gt; 3 [PE1.2] times pop
23 9 231 . 2 &gt;&gt; 3 [PE1.2] times pop
23 9 231 2 . &gt;&gt; 3 [PE1.2] times pop
23 9 57 . 3 [PE1.2] times pop
23 9 57 3 . [PE1.2] times pop
23 9 57 3 [PE1.2] . times pop
23 9 57 [PE1.2] . i 2 [PE1.2] times pop
23 9 57 . PE1.2 2 [PE1.2] times pop
23 9 57 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 . 3 &amp; PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 3 . &amp; PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 1 . PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 1 . + [+] dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 . [+] dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 [+] . dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 . + 10 57 2 &gt;&gt; 2 [PE1.2] times pop
33 . 10 57 2 &gt;&gt; 2 [PE1.2] times pop
33 10 . 57 2 &gt;&gt; 2 [PE1.2] times pop
33 10 57 . 2 &gt;&gt; 2 [PE1.2] times pop
33 10 57 2 . &gt;&gt; 2 [PE1.2] times pop
33 10 14 . 2 [PE1.2] times pop
33 10 14 2 . [PE1.2] times pop
33 10 14 2 [PE1.2] . times pop
33 10 14 [PE1.2] . i 1 [PE1.2] times pop
33 10 14 . PE1.2 1 [PE1.2] times pop
33 10 14 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 . 3 &amp; PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 3 . &amp; PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 2 . PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 2 . + [+] dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 . [+] dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 [+] . dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 . + 12 14 2 &gt;&gt; 1 [PE1.2] times pop
45 . 12 14 2 &gt;&gt; 1 [PE1.2] times pop
45 12 . 14 2 &gt;&gt; 1 [PE1.2] times pop
45 12 14 . 2 &gt;&gt; 1 [PE1.2] times pop
45 12 14 2 . &gt;&gt; 1 [PE1.2] times pop
45 12 3 . 1 [PE1.2] times pop
45 12 3 1 . [PE1.2] times pop
45 12 3 1 [PE1.2] . times pop
45 12 3 [PE1.2] . i pop
45 12 3 . PE1.2 pop
45 12 3 . [3 &amp; PE1.1] dupdip 2 &gt;&gt; pop
45 12 3 [3 &amp; PE1.1] . dupdip 2 &gt;&gt; pop
45 12 3 . 3 &amp; PE1.1 3 2 &gt;&gt; pop
45 12 3 3 . &amp; PE1.1 3 2 &gt;&gt; pop
45 12 3 . PE1.1 3 2 &gt;&gt; pop
45 12 3 . + [+] dupdip 3 2 &gt;&gt; pop
45 15 . [+] dupdip 3 2 &gt;&gt; pop
45 15 [+] . dupdip 3 2 &gt;&gt; pop
45 15 . + 15 3 2 &gt;&gt; pop
60 . 15 3 2 &gt;&gt; pop
60 15 . 3 2 &gt;&gt; pop
60 15 3 . 2 &gt;&gt; pop
60 15 3 2 . &gt;&gt; pop
60 15 0 . pop
60 15 .
<pre> • 0 0 14811 7 [PE1.2] times pop
0 • 0 14811 7 [PE1.2] times pop
0 0 • 14811 7 [PE1.2] times pop
0 0 14811 • 7 [PE1.2] times pop
0 0 14811 7 • [PE1.2] times pop
0 0 14811 7 [PE1.2] • times pop
0 0 14811 • PE1.2 6 [PE1.2] times pop
0 0 14811 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 • 3 &amp; PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 14811 3 • &amp; PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 3 • PE1.1 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 0 3 • + [+] dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 • [+] dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 [+] • dupdip 14811 2 &gt;&gt; 6 [PE1.2] times pop
0 3 • + 3 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 • 3 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 3 • 14811 2 &gt;&gt; 6 [PE1.2] times pop
3 3 14811 • 2 &gt;&gt; 6 [PE1.2] times pop
3 3 14811 2 • &gt;&gt; 6 [PE1.2] times pop
3 3 3702 • 6 [PE1.2] times pop
3 3 3702 6 • [PE1.2] times pop
3 3 3702 6 [PE1.2] • times pop
3 3 3702 • PE1.2 5 [PE1.2] times pop
3 3 3702 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 • 3 &amp; PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 3702 3 • &amp; PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 2 • PE1.1 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 3 2 • + [+] dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 • [+] dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 [+] • dupdip 3702 2 &gt;&gt; 5 [PE1.2] times pop
3 5 • + 5 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 • 5 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 5 • 3702 2 &gt;&gt; 5 [PE1.2] times pop
8 5 3702 • 2 &gt;&gt; 5 [PE1.2] times pop
8 5 3702 2 • &gt;&gt; 5 [PE1.2] times pop
8 5 925 • 5 [PE1.2] times pop
8 5 925 5 • [PE1.2] times pop
8 5 925 5 [PE1.2] • times pop
8 5 925 • PE1.2 4 [PE1.2] times pop
8 5 925 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 • 3 &amp; PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 925 3 • &amp; PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 1 • PE1.1 925 2 &gt;&gt; 4 [PE1.2] times pop
8 5 1 • + [+] dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 • [+] dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 [+] • dupdip 925 2 &gt;&gt; 4 [PE1.2] times pop
8 6 • + 6 925 2 &gt;&gt; 4 [PE1.2] times pop
14 • 6 925 2 &gt;&gt; 4 [PE1.2] times pop
14 6 • 925 2 &gt;&gt; 4 [PE1.2] times pop
14 6 925 • 2 &gt;&gt; 4 [PE1.2] times pop
14 6 925 2 • &gt;&gt; 4 [PE1.2] times pop
14 6 231 • 4 [PE1.2] times pop
14 6 231 4 • [PE1.2] times pop
14 6 231 4 [PE1.2] • times pop
14 6 231 • PE1.2 3 [PE1.2] times pop
14 6 231 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 • 3 &amp; PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 231 3 • &amp; PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 3 • PE1.1 231 2 &gt;&gt; 3 [PE1.2] times pop
14 6 3 • + [+] dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 • [+] dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 [+] • dupdip 231 2 &gt;&gt; 3 [PE1.2] times pop
14 9 • + 9 231 2 &gt;&gt; 3 [PE1.2] times pop
23 • 9 231 2 &gt;&gt; 3 [PE1.2] times pop
23 9 • 231 2 &gt;&gt; 3 [PE1.2] times pop
23 9 231 • 2 &gt;&gt; 3 [PE1.2] times pop
23 9 231 2 • &gt;&gt; 3 [PE1.2] times pop
23 9 57 • 3 [PE1.2] times pop
23 9 57 3 • [PE1.2] times pop
23 9 57 3 [PE1.2] • times pop
23 9 57 • PE1.2 2 [PE1.2] times pop
23 9 57 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 • 3 &amp; PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 57 3 • &amp; PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 1 • PE1.1 57 2 &gt;&gt; 2 [PE1.2] times pop
23 9 1 • + [+] dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 • [+] dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 [+] • dupdip 57 2 &gt;&gt; 2 [PE1.2] times pop
23 10 • + 10 57 2 &gt;&gt; 2 [PE1.2] times pop
33 • 10 57 2 &gt;&gt; 2 [PE1.2] times pop
33 10 • 57 2 &gt;&gt; 2 [PE1.2] times pop
33 10 57 • 2 &gt;&gt; 2 [PE1.2] times pop
33 10 57 2 • &gt;&gt; 2 [PE1.2] times pop
33 10 14 • 2 [PE1.2] times pop
33 10 14 2 • [PE1.2] times pop
33 10 14 2 [PE1.2] • times pop
33 10 14 • PE1.2 1 [PE1.2] times pop
33 10 14 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 • 3 &amp; PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 14 3 • &amp; PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 2 • PE1.1 14 2 &gt;&gt; 1 [PE1.2] times pop
33 10 2 • + [+] dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 • [+] dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 [+] • dupdip 14 2 &gt;&gt; 1 [PE1.2] times pop
33 12 • + 12 14 2 &gt;&gt; 1 [PE1.2] times pop
45 • 12 14 2 &gt;&gt; 1 [PE1.2] times pop
45 12 • 14 2 &gt;&gt; 1 [PE1.2] times pop
45 12 14 • 2 &gt;&gt; 1 [PE1.2] times pop
45 12 14 2 • &gt;&gt; 1 [PE1.2] times pop
45 12 3 • 1 [PE1.2] times pop
45 12 3 1 • [PE1.2] times pop
45 12 3 1 [PE1.2] • times pop
45 12 3 • PE1.2 pop
45 12 3 • [3 &amp; PE1.1] dupdip 2 &gt;&gt; pop
45 12 3 [3 &amp; PE1.1] • dupdip 2 &gt;&gt; pop
45 12 3 • 3 &amp; PE1.1 3 2 &gt;&gt; pop
45 12 3 3 • &amp; PE1.1 3 2 &gt;&gt; pop
45 12 3 • PE1.1 3 2 &gt;&gt; pop
45 12 3 • + [+] dupdip 3 2 &gt;&gt; pop
45 15 • [+] dupdip 3 2 &gt;&gt; pop
45 15 [+] • dupdip 3 2 &gt;&gt; pop
45 15 • + 15 3 2 &gt;&gt; pop
60 • 15 3 2 &gt;&gt; pop
60 15 • 3 2 &gt;&gt; pop
60 15 3 • 2 &gt;&gt; pop
60 15 3 2 • &gt;&gt; pop
60 15 0 • pop
60 15 •
</pre>
</div>
</div>
@ -13903,7 +13896,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13916,7 +13909,7 @@ integer terms from the list.</p>
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13960,7 +13953,7 @@ n 14811 swap [PE1.2] times pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.3 == 14811 swap [PE1.2] times pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.3 14811 swap [PE1.2] times pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13981,7 +13974,7 @@ n 14811 swap [PE1.2] times pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1 0 0 66 [7 PE1.3] times 4 PE1.3 pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13994,7 +13987,7 @@ n 14811 swap [PE1.2] times pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14046,7 +14039,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[23]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &amp;] dupdip 2 &gt;&gt;] dip rest cons]&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;PE1.terms [0 swap [dup [pop 14811] [] branch [3 &amp;] dupdip 2 &gt;&gt;] dip rest cons]&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14059,7 +14052,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1.terms 21 [x] times&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1.terms 21 [x] times&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14098,7 +14091,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[25]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;7 66 * 4 +&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;7 66 * 4 +&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14136,7 +14129,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[26]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1.terms 466 [x] times pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;PE1.terms 466 [x] times pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14174,7 +14167,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[PE1.terms 466 [x] times pop] run sum&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[PE1.terms 466 [x] times pop] run sum&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14213,7 +14206,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[28]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;0 0 PE1.terms 466 [x [PE1.1] dip] times popop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;0 0 PE1.terms 466 [x [PE1.1] dip] times popop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14252,7 +14245,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[10 9 8 7 6 5 4 3 2 1] sum&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[10 9 8 7 6 5 4 3 2 1] sum&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14306,7 +14299,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;F == dup ++ * 2 floordiv&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;F dup ++ * 2 floordiv&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14319,7 +14312,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[31]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;10 F&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;10 F&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14336,14 +14329,14 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 10 F
10 . F
10 . dup ++ * 2 floordiv
10 10 . ++ * 2 floordiv
10 11 . * 2 floordiv
110 . 2 floordiv
110 2 . floordiv
55 .
<pre> 10 F
10 F
10 dup ++ * 2 floordiv
10 10 ++ * 2 floordiv
10 11 * 2 floordiv
110 2 floordiv
110 2 floordiv
55
</pre>
</div>
</div>
@ -14376,7 +14369,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[32]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14407,7 +14400,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[33]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14446,7 +14439,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[34]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14490,7 +14483,7 @@ PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop</code></pre>
<div class="prompt input_prompt">In&nbsp;[35]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;6945 33 * [993 995 996 999] cons sum&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;6945 33 * [993 995 996 999] cons sum&#39;</span><span class="p">)</span>
</pre></div>
</div>

View File

@ -33,7 +33,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('P == [3 % not] dupdip 5 % not or')"
"define('P [3 % not] dupdip 5 % not or')"
]
},
{
@ -45,19 +45,19 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 80 P\n",
" 80 . P\n",
" 80 . [3 % not] dupdip 5 % not or\n",
"80 [3 % not] . dupdip 5 % not or\n",
" 80 . 3 % not 80 5 % not or\n",
" 80 3 . % not 80 5 % not or\n",
" 2 . not 80 5 % not or\n",
" False . 80 5 % not or\n",
" False 80 . 5 % not or\n",
" False 80 5 . % not or\n",
" False 0 . not or\n",
" False True . or\n",
" True . \n"
" 80 P\n",
" 80 P\n",
" 80 [3 % not] dupdip 5 % not or\n",
"80 [3 % not] dupdip 5 % not or\n",
" 80 3 % not 80 5 % not or\n",
" 80 3 % not 80 5 % not or\n",
" 2 not 80 5 % not or\n",
" False 80 5 % not or\n",
" False 80 5 % not or\n",
" False 80 5 % not or\n",
" False 0 not or\n",
" False True or\n",
" True \n"
]
}
],
@ -124,7 +124,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1.1 == + [+] dupdip')"
"define('PE1.1 + [+] dupdip')"
]
},
{
@ -136,16 +136,16 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 0 0 3 PE1.1\n",
" 0 . 0 3 PE1.1\n",
" 0 0 . 3 PE1.1\n",
" 0 0 3 . PE1.1\n",
" 0 0 3 . + [+] dupdip\n",
" 0 3 . [+] dupdip\n",
"0 3 [+] . dupdip\n",
" 0 3 . + 3\n",
" 3 . 3\n",
" 3 3 . \n"
" 0 0 3 PE1.1\n",
" 0 0 3 PE1.1\n",
" 0 0 3 PE1.1\n",
" 0 0 3 PE1.1\n",
" 0 0 3 + [+] dupdip\n",
" 0 3 [+] dupdip\n",
"0 3 [+] dupdip\n",
" 0 3 + 3\n",
" 3 3\n",
" 3 3 \n"
]
}
],
@ -164,79 +164,79 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 . 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 . [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 [3 2 1 3 1 2 3] . [PE1.1] step\n",
"0 0 [3 2 1 3 1 2 3] [PE1.1] . step\n",
" 0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 . 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 . [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] . [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] [PE1.1] . step\n",
" 3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step\n",
" 3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step\n",
" 3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 . + 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 . 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 . [1 3 1 2 3] [PE1.1] step\n",
" 8 5 [1 3 1 2 3] . [PE1.1] step\n",
" 8 5 [1 3 1 2 3] [PE1.1] . step\n",
" 8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step\n",
" 8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step\n",
" 8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 . [+] dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 [+] . dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 . + 6 [3 1 2 3] [PE1.1] step\n",
" 14 . 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 . [3 1 2 3] [PE1.1] step\n",
" 14 6 [3 1 2 3] . [PE1.1] step\n",
" 14 6 [3 1 2 3] [PE1.1] . step\n",
" 14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step\n",
" 14 6 3 . PE1.1 [1 2 3] [PE1.1] step\n",
" 14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step\n",
" 14 9 . [+] dupdip [1 2 3] [PE1.1] step\n",
" 14 9 [+] . dupdip [1 2 3] [PE1.1] step\n",
" 14 9 . + 9 [1 2 3] [PE1.1] step\n",
" 23 . 9 [1 2 3] [PE1.1] step\n",
" 23 9 . [1 2 3] [PE1.1] step\n",
" 23 9 [1 2 3] . [PE1.1] step\n",
" 23 9 [1 2 3] [PE1.1] . step\n",
" 23 9 1 [PE1.1] . i [2 3] [PE1.1] step\n",
" 23 9 1 . PE1.1 [2 3] [PE1.1] step\n",
" 23 9 1 . + [+] dupdip [2 3] [PE1.1] step\n",
" 23 10 . [+] dupdip [2 3] [PE1.1] step\n",
" 23 10 [+] . dupdip [2 3] [PE1.1] step\n",
" 23 10 . + 10 [2 3] [PE1.1] step\n",
" 33 . 10 [2 3] [PE1.1] step\n",
" 33 10 . [2 3] [PE1.1] step\n",
" 33 10 [2 3] . [PE1.1] step\n",
" 33 10 [2 3] [PE1.1] . step\n",
" 33 10 2 [PE1.1] . i [3] [PE1.1] step\n",
" 33 10 2 . PE1.1 [3] [PE1.1] step\n",
" 33 10 2 . + [+] dupdip [3] [PE1.1] step\n",
" 33 12 . [+] dupdip [3] [PE1.1] step\n",
" 33 12 [+] . dupdip [3] [PE1.1] step\n",
" 33 12 . + 12 [3] [PE1.1] step\n",
" 45 . 12 [3] [PE1.1] step\n",
" 45 12 . [3] [PE1.1] step\n",
" 45 12 [3] . [PE1.1] step\n",
" 45 12 [3] [PE1.1] . step\n",
" 45 12 3 [PE1.1] . i\n",
" 45 12 3 . PE1.1\n",
" 45 12 3 . + [+] dupdip\n",
" 45 15 . [+] dupdip\n",
" 45 15 [+] . dupdip\n",
" 45 15 . + 15\n",
" 60 . 15\n",
" 60 15 . \n"
" 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
"0 0 [3 2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 3 [PE1.1] i [2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 3 PE1.1 [2 1 3 1 2 3] [PE1.1] step\n",
" 0 0 3 + [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step\n",
" 0 3 + 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 [2 1 3 1 2 3] [PE1.1] step\n",
" 3 3 2 [PE1.1] i [1 3 1 2 3] [PE1.1] step\n",
" 3 3 2 PE1.1 [1 3 1 2 3] [PE1.1] step\n",
" 3 3 2 + [+] dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step\n",
" 3 5 + 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 [1 3 1 2 3] [PE1.1] step\n",
" 8 5 1 [PE1.1] i [3 1 2 3] [PE1.1] step\n",
" 8 5 1 PE1.1 [3 1 2 3] [PE1.1] step\n",
" 8 5 1 + [+] dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 [+] dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 [+] dupdip [3 1 2 3] [PE1.1] step\n",
" 8 6 + 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 [3 1 2 3] [PE1.1] step\n",
" 14 6 3 [PE1.1] i [1 2 3] [PE1.1] step\n",
" 14 6 3 PE1.1 [1 2 3] [PE1.1] step\n",
" 14 6 3 + [+] dupdip [1 2 3] [PE1.1] step\n",
" 14 9 [+] dupdip [1 2 3] [PE1.1] step\n",
" 14 9 [+] dupdip [1 2 3] [PE1.1] step\n",
" 14 9 + 9 [1 2 3] [PE1.1] step\n",
" 23 9 [1 2 3] [PE1.1] step\n",
" 23 9 [1 2 3] [PE1.1] step\n",
" 23 9 [1 2 3] [PE1.1] step\n",
" 23 9 [1 2 3] [PE1.1] step\n",
" 23 9 1 [PE1.1] i [2 3] [PE1.1] step\n",
" 23 9 1 PE1.1 [2 3] [PE1.1] step\n",
" 23 9 1 + [+] dupdip [2 3] [PE1.1] step\n",
" 23 10 [+] dupdip [2 3] [PE1.1] step\n",
" 23 10 [+] dupdip [2 3] [PE1.1] step\n",
" 23 10 + 10 [2 3] [PE1.1] step\n",
" 33 10 [2 3] [PE1.1] step\n",
" 33 10 [2 3] [PE1.1] step\n",
" 33 10 [2 3] [PE1.1] step\n",
" 33 10 [2 3] [PE1.1] step\n",
" 33 10 2 [PE1.1] i [3] [PE1.1] step\n",
" 33 10 2 PE1.1 [3] [PE1.1] step\n",
" 33 10 2 + [+] dupdip [3] [PE1.1] step\n",
" 33 12 [+] dupdip [3] [PE1.1] step\n",
" 33 12 [+] dupdip [3] [PE1.1] step\n",
" 33 12 + 12 [3] [PE1.1] step\n",
" 45 12 [3] [PE1.1] step\n",
" 45 12 [3] [PE1.1] step\n",
" 45 12 [3] [PE1.1] step\n",
" 45 12 [3] [PE1.1] step\n",
" 45 12 3 [PE1.1] i\n",
" 45 12 3 PE1.1\n",
" 45 12 3 + [+] dupdip\n",
" 45 15 [+] dupdip\n",
" 45 15 [+] dupdip\n",
" 45 15 + 15\n",
" 60 15\n",
" 60 15 \n"
]
}
],
@ -259,7 +259,7 @@
{
"data": {
"text/plain": [
"66"
"66.66666666666667"
]
},
"execution_count": 7,
@ -351,7 +351,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')"
"define('PE1 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')"
]
},
{
@ -413,7 +413,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1.2 == [3 & PE1.1] dupdip 2 >>')"
"define('PE1.2 [3 & PE1.1] dupdip 2 >>')"
]
},
{
@ -425,24 +425,24 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 0 0 14811 PE1.2\n",
" 0 . 0 14811 PE1.2\n",
" 0 0 . 14811 PE1.2\n",
" 0 0 14811 . PE1.2\n",
" 0 0 14811 . [3 & PE1.1] dupdip 2 >>\n",
"0 0 14811 [3 & PE1.1] . dupdip 2 >>\n",
" 0 0 14811 . 3 & PE1.1 14811 2 >>\n",
" 0 0 14811 3 . & PE1.1 14811 2 >>\n",
" 0 0 3 . PE1.1 14811 2 >>\n",
" 0 0 3 . + [+] dupdip 14811 2 >>\n",
" 0 3 . [+] dupdip 14811 2 >>\n",
" 0 3 [+] . dupdip 14811 2 >>\n",
" 0 3 . + 3 14811 2 >>\n",
" 3 . 3 14811 2 >>\n",
" 3 3 . 14811 2 >>\n",
" 3 3 14811 . 2 >>\n",
" 3 3 14811 2 . >>\n",
" 3 3 3702 . \n"
" 0 0 14811 PE1.2\n",
" 0 0 14811 PE1.2\n",
" 0 0 14811 PE1.2\n",
" 0 0 14811 PE1.2\n",
" 0 0 14811 [3 & PE1.1] dupdip 2 >>\n",
"0 0 14811 [3 & PE1.1] dupdip 2 >>\n",
" 0 0 14811 3 & PE1.1 14811 2 >>\n",
" 0 0 14811 3 & PE1.1 14811 2 >>\n",
" 0 0 3 PE1.1 14811 2 >>\n",
" 0 0 3 + [+] dupdip 14811 2 >>\n",
" 0 3 [+] dupdip 14811 2 >>\n",
" 0 3 [+] dupdip 14811 2 >>\n",
" 0 3 + 3 14811 2 >>\n",
" 3 3 14811 2 >>\n",
" 3 3 14811 2 >>\n",
" 3 3 14811 2 >>\n",
" 3 3 14811 2 >>\n",
" 3 3 3702 \n"
]
}
],
@ -459,24 +459,24 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 3 3 3702 PE1.2\n",
" 3 . 3 3702 PE1.2\n",
" 3 3 . 3702 PE1.2\n",
" 3 3 3702 . PE1.2\n",
" 3 3 3702 . [3 & PE1.1] dupdip 2 >>\n",
"3 3 3702 [3 & PE1.1] . dupdip 2 >>\n",
" 3 3 3702 . 3 & PE1.1 3702 2 >>\n",
" 3 3 3702 3 . & PE1.1 3702 2 >>\n",
" 3 3 2 . PE1.1 3702 2 >>\n",
" 3 3 2 . + [+] dupdip 3702 2 >>\n",
" 3 5 . [+] dupdip 3702 2 >>\n",
" 3 5 [+] . dupdip 3702 2 >>\n",
" 3 5 . + 5 3702 2 >>\n",
" 8 . 5 3702 2 >>\n",
" 8 5 . 3702 2 >>\n",
" 8 5 3702 . 2 >>\n",
" 8 5 3702 2 . >>\n",
" 8 5 925 . \n"
" 3 3 3702 PE1.2\n",
" 3 3 3702 PE1.2\n",
" 3 3 3702 PE1.2\n",
" 3 3 3702 PE1.2\n",
" 3 3 3702 [3 & PE1.1] dupdip 2 >>\n",
"3 3 3702 [3 & PE1.1] dupdip 2 >>\n",
" 3 3 3702 3 & PE1.1 3702 2 >>\n",
" 3 3 3702 3 & PE1.1 3702 2 >>\n",
" 3 3 2 PE1.1 3702 2 >>\n",
" 3 3 2 + [+] dupdip 3702 2 >>\n",
" 3 5 [+] dupdip 3702 2 >>\n",
" 3 5 [+] dupdip 3702 2 >>\n",
" 3 5 + 5 3702 2 >>\n",
" 8 5 3702 2 >>\n",
" 8 5 3702 2 >>\n",
" 8 5 3702 2 >>\n",
" 8 5 3702 2 >>\n",
" 8 5 925 \n"
]
}
],
@ -495,137 +495,130 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 0 0 14811 7 [PE1.2] times pop\n",
" 0 . 0 14811 7 [PE1.2] times pop\n",
" 0 0 . 14811 7 [PE1.2] times pop\n",
" 0 0 14811 . 7 [PE1.2] times pop\n",
" 0 0 14811 7 . [PE1.2] times pop\n",
" 0 0 14811 7 [PE1.2] . times pop\n",
" 0 0 14811 [PE1.2] . i 6 [PE1.2] times pop\n",
" 0 0 14811 . PE1.2 6 [PE1.2] times pop\n",
" 0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop\n",
"0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop\n",
" 0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 . + 3 14811 2 >> 6 [PE1.2] times pop\n",
" 3 . 3 14811 2 >> 6 [PE1.2] times pop\n",
" 3 3 . 14811 2 >> 6 [PE1.2] times pop\n",
" 3 3 14811 . 2 >> 6 [PE1.2] times pop\n",
" 3 3 14811 2 . >> 6 [PE1.2] times pop\n",
" 3 3 3702 . 6 [PE1.2] times pop\n",
" 3 3 3702 6 . [PE1.2] times pop\n",
" 3 3 3702 6 [PE1.2] . times pop\n",
" 3 3 3702 [PE1.2] . i 5 [PE1.2] times pop\n",
" 3 3 3702 . PE1.2 5 [PE1.2] times pop\n",
" 3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 . + 5 3702 2 >> 5 [PE1.2] times pop\n",
" 8 . 5 3702 2 >> 5 [PE1.2] times pop\n",
" 8 5 . 3702 2 >> 5 [PE1.2] times pop\n",
" 8 5 3702 . 2 >> 5 [PE1.2] times pop\n",
" 8 5 3702 2 . >> 5 [PE1.2] times pop\n",
" 8 5 925 . 5 [PE1.2] times pop\n",
" 8 5 925 5 . [PE1.2] times pop\n",
" 8 5 925 5 [PE1.2] . times pop\n",
" 8 5 925 [PE1.2] . i 4 [PE1.2] times pop\n",
" 8 5 925 . PE1.2 4 [PE1.2] times pop\n",
" 8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 . + 6 925 2 >> 4 [PE1.2] times pop\n",
" 14 . 6 925 2 >> 4 [PE1.2] times pop\n",
" 14 6 . 925 2 >> 4 [PE1.2] times pop\n",
" 14 6 925 . 2 >> 4 [PE1.2] times pop\n",
" 14 6 925 2 . >> 4 [PE1.2] times pop\n",
" 14 6 231 . 4 [PE1.2] times pop\n",
" 14 6 231 4 . [PE1.2] times pop\n",
" 14 6 231 4 [PE1.2] . times pop\n",
" 14 6 231 [PE1.2] . i 3 [PE1.2] times pop\n",
" 14 6 231 . PE1.2 3 [PE1.2] times pop\n",
" 14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 . + 9 231 2 >> 3 [PE1.2] times pop\n",
" 23 . 9 231 2 >> 3 [PE1.2] times pop\n",
" 23 9 . 231 2 >> 3 [PE1.2] times pop\n",
" 23 9 231 . 2 >> 3 [PE1.2] times pop\n",
" 23 9 231 2 . >> 3 [PE1.2] times pop\n",
" 23 9 57 . 3 [PE1.2] times pop\n",
" 23 9 57 3 . [PE1.2] times pop\n",
" 23 9 57 3 [PE1.2] . times pop\n",
" 23 9 57 [PE1.2] . i 2 [PE1.2] times pop\n",
" 23 9 57 . PE1.2 2 [PE1.2] times pop\n",
" 23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 . + 10 57 2 >> 2 [PE1.2] times pop\n",
" 33 . 10 57 2 >> 2 [PE1.2] times pop\n",
" 33 10 . 57 2 >> 2 [PE1.2] times pop\n",
" 33 10 57 . 2 >> 2 [PE1.2] times pop\n",
" 33 10 57 2 . >> 2 [PE1.2] times pop\n",
" 33 10 14 . 2 [PE1.2] times pop\n",
" 33 10 14 2 . [PE1.2] times pop\n",
" 33 10 14 2 [PE1.2] . times pop\n",
" 33 10 14 [PE1.2] . i 1 [PE1.2] times pop\n",
" 33 10 14 . PE1.2 1 [PE1.2] times pop\n",
" 33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 . + 12 14 2 >> 1 [PE1.2] times pop\n",
" 45 . 12 14 2 >> 1 [PE1.2] times pop\n",
" 45 12 . 14 2 >> 1 [PE1.2] times pop\n",
" 45 12 14 . 2 >> 1 [PE1.2] times pop\n",
" 45 12 14 2 . >> 1 [PE1.2] times pop\n",
" 45 12 3 . 1 [PE1.2] times pop\n",
" 45 12 3 1 . [PE1.2] times pop\n",
" 45 12 3 1 [PE1.2] . times pop\n",
" 45 12 3 [PE1.2] . i pop\n",
" 45 12 3 . PE1.2 pop\n",
" 45 12 3 . [3 & PE1.1] dupdip 2 >> pop\n",
" 45 12 3 [3 & PE1.1] . dupdip 2 >> pop\n",
" 45 12 3 . 3 & PE1.1 3 2 >> pop\n",
" 45 12 3 3 . & PE1.1 3 2 >> pop\n",
" 45 12 3 . PE1.1 3 2 >> pop\n",
" 45 12 3 . + [+] dupdip 3 2 >> pop\n",
" 45 15 . [+] dupdip 3 2 >> pop\n",
" 45 15 [+] . dupdip 3 2 >> pop\n",
" 45 15 . + 15 3 2 >> pop\n",
" 60 . 15 3 2 >> pop\n",
" 60 15 . 3 2 >> pop\n",
" 60 15 3 . 2 >> pop\n",
" 60 15 3 2 . >> pop\n",
" 60 15 0 . pop\n",
" 60 15 . \n"
" • 0 0 14811 7 [PE1.2] times pop\n",
" 0 • 0 14811 7 [PE1.2] times pop\n",
" 0 0 • 14811 7 [PE1.2] times pop\n",
" 0 0 14811 • 7 [PE1.2] times pop\n",
" 0 0 14811 7 • [PE1.2] times pop\n",
" 0 0 14811 7 [PE1.2] • times pop\n",
" 0 0 14811 • PE1.2 6 [PE1.2] times pop\n",
" 0 0 14811 • [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop\n",
"0 0 14811 [3 & PE1.1] • dupdip 2 >> 6 [PE1.2] times pop\n",
" 0 0 14811 • 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 14811 3 • & PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 3 • PE1.1 14811 2 >> 6 [PE1.2] times pop\n",
" 0 0 3 • + [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 • [+] dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 [+] • dupdip 14811 2 >> 6 [PE1.2] times pop\n",
" 0 3 • + 3 14811 2 >> 6 [PE1.2] times pop\n",
" 3 • 3 14811 2 >> 6 [PE1.2] times pop\n",
" 3 3 • 14811 2 >> 6 [PE1.2] times pop\n",
" 3 3 14811 • 2 >> 6 [PE1.2] times pop\n",
" 3 3 14811 2 • >> 6 [PE1.2] times pop\n",
" 3 3 3702 • 6 [PE1.2] times pop\n",
" 3 3 3702 6 • [PE1.2] times pop\n",
" 3 3 3702 6 [PE1.2] • times pop\n",
" 3 3 3702 • PE1.2 5 [PE1.2] times pop\n",
" 3 3 3702 • [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 [3 & PE1.1] • dupdip 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 • 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 3702 3 • & PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 2 • PE1.1 3702 2 >> 5 [PE1.2] times pop\n",
" 3 3 2 • + [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 • [+] dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 [+] • dupdip 3702 2 >> 5 [PE1.2] times pop\n",
" 3 5 • + 5 3702 2 >> 5 [PE1.2] times pop\n",
" 8 • 5 3702 2 >> 5 [PE1.2] times pop\n",
" 8 5 • 3702 2 >> 5 [PE1.2] times pop\n",
" 8 5 3702 • 2 >> 5 [PE1.2] times pop\n",
" 8 5 3702 2 • >> 5 [PE1.2] times pop\n",
" 8 5 925 • 5 [PE1.2] times pop\n",
" 8 5 925 5 • [PE1.2] times pop\n",
" 8 5 925 5 [PE1.2] • times pop\n",
" 8 5 925 • PE1.2 4 [PE1.2] times pop\n",
" 8 5 925 • [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 [3 & PE1.1] • dupdip 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 • 3 & PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 925 3 • & PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 1 • PE1.1 925 2 >> 4 [PE1.2] times pop\n",
" 8 5 1 • + [+] dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 • [+] dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 [+] • dupdip 925 2 >> 4 [PE1.2] times pop\n",
" 8 6 • + 6 925 2 >> 4 [PE1.2] times pop\n",
" 14 • 6 925 2 >> 4 [PE1.2] times pop\n",
" 14 6 • 925 2 >> 4 [PE1.2] times pop\n",
" 14 6 925 • 2 >> 4 [PE1.2] times pop\n",
" 14 6 925 2 • >> 4 [PE1.2] times pop\n",
" 14 6 231 • 4 [PE1.2] times pop\n",
" 14 6 231 4 • [PE1.2] times pop\n",
" 14 6 231 4 [PE1.2] • times pop\n",
" 14 6 231 • PE1.2 3 [PE1.2] times pop\n",
" 14 6 231 • [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 [3 & PE1.1] • dupdip 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 • 3 & PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 231 3 • & PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 3 • PE1.1 231 2 >> 3 [PE1.2] times pop\n",
" 14 6 3 • + [+] dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 • [+] dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 [+] • dupdip 231 2 >> 3 [PE1.2] times pop\n",
" 14 9 • + 9 231 2 >> 3 [PE1.2] times pop\n",
" 23 • 9 231 2 >> 3 [PE1.2] times pop\n",
" 23 9 • 231 2 >> 3 [PE1.2] times pop\n",
" 23 9 231 • 2 >> 3 [PE1.2] times pop\n",
" 23 9 231 2 • >> 3 [PE1.2] times pop\n",
" 23 9 57 • 3 [PE1.2] times pop\n",
" 23 9 57 3 • [PE1.2] times pop\n",
" 23 9 57 3 [PE1.2] • times pop\n",
" 23 9 57 • PE1.2 2 [PE1.2] times pop\n",
" 23 9 57 • [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 [3 & PE1.1] • dupdip 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 • 3 & PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 57 3 • & PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 1 • PE1.1 57 2 >> 2 [PE1.2] times pop\n",
" 23 9 1 • + [+] dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 • [+] dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 [+] • dupdip 57 2 >> 2 [PE1.2] times pop\n",
" 23 10 • + 10 57 2 >> 2 [PE1.2] times pop\n",
" 33 • 10 57 2 >> 2 [PE1.2] times pop\n",
" 33 10 • 57 2 >> 2 [PE1.2] times pop\n",
" 33 10 57 • 2 >> 2 [PE1.2] times pop\n",
" 33 10 57 2 • >> 2 [PE1.2] times pop\n",
" 33 10 14 • 2 [PE1.2] times pop\n",
" 33 10 14 2 • [PE1.2] times pop\n",
" 33 10 14 2 [PE1.2] • times pop\n",
" 33 10 14 • PE1.2 1 [PE1.2] times pop\n",
" 33 10 14 • [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 [3 & PE1.1] • dupdip 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 • 3 & PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 14 3 • & PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 2 • PE1.1 14 2 >> 1 [PE1.2] times pop\n",
" 33 10 2 • + [+] dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 • [+] dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 [+] • dupdip 14 2 >> 1 [PE1.2] times pop\n",
" 33 12 • + 12 14 2 >> 1 [PE1.2] times pop\n",
" 45 • 12 14 2 >> 1 [PE1.2] times pop\n",
" 45 12 • 14 2 >> 1 [PE1.2] times pop\n",
" 45 12 14 • 2 >> 1 [PE1.2] times pop\n",
" 45 12 14 2 • >> 1 [PE1.2] times pop\n",
" 45 12 3 • 1 [PE1.2] times pop\n",
" 45 12 3 1 • [PE1.2] times pop\n",
" 45 12 3 1 [PE1.2] • times pop\n",
" 45 12 3 • PE1.2 pop\n",
" 45 12 3 • [3 & PE1.1] dupdip 2 >> pop\n",
" 45 12 3 [3 & PE1.1] • dupdip 2 >> pop\n",
" 45 12 3 • 3 & PE1.1 3 2 >> pop\n",
" 45 12 3 3 • & PE1.1 3 2 >> pop\n",
" 45 12 3 • PE1.1 3 2 >> pop\n",
" 45 12 3 • + [+] dupdip 3 2 >> pop\n",
" 45 15 • [+] dupdip 3 2 >> pop\n",
" 45 15 [+] • dupdip 3 2 >> pop\n",
" 45 15 • + 15 3 2 >> pop\n",
" 60 • 15 3 2 >> pop\n",
" 60 15 • 3 2 >> pop\n",
" 60 15 3 • 2 >> pop\n",
" 60 15 3 2 • >> pop\n",
" 60 15 0 • pop\n",
" 60 15 • \n"
]
}
],
@ -646,7 +639,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')"
"define('PE1 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')"
]
},
{
@ -686,7 +679,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1.3 == 14811 swap [PE1.2] times pop')"
"define('PE1.3 14811 swap [PE1.2] times pop')"
]
},
{
@ -702,7 +695,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop')"
"define('PE1 0 0 66 [7 PE1.3] times 4 PE1.3 pop')"
]
},
{
@ -750,7 +743,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')"
"define('PE1.terms [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')"
]
},
{
@ -925,7 +918,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('F == dup ++ * 2 floordiv')"
"define('F dup ++ * 2 floordiv')"
]
},
{
@ -937,14 +930,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 10 F\n",
" 10 . F\n",
" 10 . dup ++ * 2 floordiv\n",
"10 10 . ++ * 2 floordiv\n",
"10 11 . * 2 floordiv\n",
" 110 . 2 floordiv\n",
"110 2 . floordiv\n",
" 55 . \n"
" 10 F\n",
" 10 F\n",
" 10 dup ++ * 2 floordiv\n",
"10 10 ++ * 2 floordiv\n",
"10 11 * 2 floordiv\n",
" 110 2 floordiv\n",
"110 2 floordiv\n",
" 55 \n"
]
}
],
@ -1106,14 +1099,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -13,7 +13,7 @@ Let's create a predicate that returns `True` if a number is a multiple of 3 or 5
```python
define('P == [3 % not] dupdip 5 % not or')
define('P [3 % not] dupdip 5 % not or')
```
@ -21,19 +21,19 @@ define('P == [3 % not] dupdip 5 % not or')
V('80 P')
```
. 80 P
80 . P
80 . [3 % not] dupdip 5 % not or
80 [3 % not] . dupdip 5 % not or
80 . 3 % not 80 5 % not or
80 3 . % not 80 5 % not or
2 . not 80 5 % not or
False . 80 5 % not or
False 80 . 5 % not or
False 80 5 . % not or
False 0 . not or
False True . or
True .
80 P
80 P
80 [3 % not] dupdip 5 % not or
80 [3 % not] dupdip 5 % not or
80 3 % not 80 5 % not or
80 3 % not 80 5 % not or
2 not 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 0 not or
False True or
True
Given the predicate function `P` a suitable program is:
@ -86,7 +86,7 @@ counter to the running sum. This function will do that:
```python
define('PE1.1 == + [+] dupdip')
define('PE1.1 + [+] dupdip')
```
@ -94,16 +94,16 @@ define('PE1.1 == + [+] dupdip')
V('0 0 3 PE1.1')
```
. 0 0 3 PE1.1
0 . 0 3 PE1.1
0 0 . 3 PE1.1
0 0 3 . PE1.1
0 0 3 . + [+] dupdip
0 3 . [+] dupdip
0 3 [+] . dupdip
0 3 . + 3
3 . 3
3 3 .
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 + [+] dupdip
0 3 [+] dupdip
0 3 [+] dupdip
0 3 + 3
3 3
3 3
@ -111,79 +111,79 @@ V('0 0 3 PE1.1')
V('0 0 [3 2 1 3 1 2 3] [PE1.1] step')
```
. 0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 . 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 . [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] . [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] . step
0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step
0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step
3 . 3 [2 1 3 1 2 3] [PE1.1] step
3 3 . [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] . [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] . step
3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step
3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step
3 5 . + 5 [1 3 1 2 3] [PE1.1] step
8 . 5 [1 3 1 2 3] [PE1.1] step
8 5 . [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] . [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] . step
8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step
8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 . [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] . dupdip [3 1 2 3] [PE1.1] step
8 6 . + 6 [3 1 2 3] [PE1.1] step
14 . 6 [3 1 2 3] [PE1.1] step
14 6 . [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] . [PE1.1] step
14 6 [3 1 2 3] [PE1.1] . step
14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step
14 6 3 . PE1.1 [1 2 3] [PE1.1] step
14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step
14 9 . [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] . dupdip [1 2 3] [PE1.1] step
14 9 . + 9 [1 2 3] [PE1.1] step
23 . 9 [1 2 3] [PE1.1] step
23 9 . [1 2 3] [PE1.1] step
23 9 [1 2 3] . [PE1.1] step
23 9 [1 2 3] [PE1.1] . step
23 9 1 [PE1.1] . i [2 3] [PE1.1] step
23 9 1 . PE1.1 [2 3] [PE1.1] step
23 9 1 . + [+] dupdip [2 3] [PE1.1] step
23 10 . [+] dupdip [2 3] [PE1.1] step
23 10 [+] . dupdip [2 3] [PE1.1] step
23 10 . + 10 [2 3] [PE1.1] step
33 . 10 [2 3] [PE1.1] step
33 10 . [2 3] [PE1.1] step
33 10 [2 3] . [PE1.1] step
33 10 [2 3] [PE1.1] . step
33 10 2 [PE1.1] . i [3] [PE1.1] step
33 10 2 . PE1.1 [3] [PE1.1] step
33 10 2 . + [+] dupdip [3] [PE1.1] step
33 12 . [+] dupdip [3] [PE1.1] step
33 12 [+] . dupdip [3] [PE1.1] step
33 12 . + 12 [3] [PE1.1] step
45 . 12 [3] [PE1.1] step
45 12 . [3] [PE1.1] step
45 12 [3] . [PE1.1] step
45 12 [3] [PE1.1] . step
45 12 3 [PE1.1] . i
45 12 3 . PE1.1
45 12 3 . + [+] dupdip
45 15 . [+] dupdip
45 15 [+] . dupdip
45 15 . + 15
60 . 15
60 15 .
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 3 [PE1.1] i [2 1 3 1 2 3] [PE1.1] step
0 0 3 PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 + 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 2 [PE1.1] i [1 3 1 2 3] [PE1.1] step
3 3 2 PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 + 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 1 [PE1.1] i [3 1 2 3] [PE1.1] step
8 5 1 PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 + 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 3 [PE1.1] i [1 2 3] [PE1.1] step
14 6 3 PE1.1 [1 2 3] [PE1.1] step
14 6 3 + [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 + 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 1 [PE1.1] i [2 3] [PE1.1] step
23 9 1 PE1.1 [2 3] [PE1.1] step
23 9 1 + [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 + 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 2 [PE1.1] i [3] [PE1.1] step
33 10 2 PE1.1 [3] [PE1.1] step
33 10 2 + [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 + 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 3 [PE1.1] i
45 12 3 PE1.1
45 12 3 + [+] dupdip
45 15 [+] dupdip
45 15 [+] dupdip
45 15 + 15
60 15
60 15
So one `step` through all seven terms brings the counter to 15 and the total to 60.
@ -196,7 +196,7 @@ So one `step` through all seven terms brings the counter to 15 and the total to
66
66.66666666666667
@ -242,7 +242,7 @@ That means we want to run the full list of numbers sixty-six times to get to 990
```python
define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')
define('PE1 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')
```
@ -278,7 +278,7 @@ integer terms from the list.
```python
define('PE1.2 == [3 & PE1.1] dupdip 2 >>')
define('PE1.2 [3 & PE1.1] dupdip 2 >>')
```
@ -286,24 +286,24 @@ define('PE1.2 == [3 & PE1.1] dupdip 2 >>')
V('0 0 14811 PE1.2')
```
. 0 0 14811 PE1.2
0 . 0 14811 PE1.2
0 0 . 14811 PE1.2
0 0 14811 . PE1.2
0 0 14811 . [3 & PE1.1] dupdip 2 >>
0 0 14811 [3 & PE1.1] . dupdip 2 >>
0 0 14811 . 3 & PE1.1 14811 2 >>
0 0 14811 3 . & PE1.1 14811 2 >>
0 0 3 . PE1.1 14811 2 >>
0 0 3 . + [+] dupdip 14811 2 >>
0 3 . [+] dupdip 14811 2 >>
0 3 [+] . dupdip 14811 2 >>
0 3 . + 3 14811 2 >>
3 . 3 14811 2 >>
3 3 . 14811 2 >>
3 3 14811 . 2 >>
3 3 14811 2 . >>
3 3 3702 .
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 [3 & PE1.1] dupdip 2 >>
0 0 14811 [3 & PE1.1] dupdip 2 >>
0 0 14811 3 & PE1.1 14811 2 >>
0 0 14811 3 & PE1.1 14811 2 >>
0 0 3 PE1.1 14811 2 >>
0 0 3 + [+] dupdip 14811 2 >>
0 3 [+] dupdip 14811 2 >>
0 3 [+] dupdip 14811 2 >>
0 3 + 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 3702
@ -311,24 +311,24 @@ V('0 0 14811 PE1.2')
V('3 3 3702 PE1.2')
```
. 3 3 3702 PE1.2
3 . 3 3702 PE1.2
3 3 . 3702 PE1.2
3 3 3702 . PE1.2
3 3 3702 . [3 & PE1.1] dupdip 2 >>
3 3 3702 [3 & PE1.1] . dupdip 2 >>
3 3 3702 . 3 & PE1.1 3702 2 >>
3 3 3702 3 . & PE1.1 3702 2 >>
3 3 2 . PE1.1 3702 2 >>
3 3 2 . + [+] dupdip 3702 2 >>
3 5 . [+] dupdip 3702 2 >>
3 5 [+] . dupdip 3702 2 >>
3 5 . + 5 3702 2 >>
8 . 5 3702 2 >>
8 5 . 3702 2 >>
8 5 3702 . 2 >>
8 5 3702 2 . >>
8 5 925 .
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 [3 & PE1.1] dupdip 2 >>
3 3 3702 [3 & PE1.1] dupdip 2 >>
3 3 3702 3 & PE1.1 3702 2 >>
3 3 3702 3 & PE1.1 3702 2 >>
3 3 2 PE1.1 3702 2 >>
3 3 2 + [+] dupdip 3702 2 >>
3 5 [+] dupdip 3702 2 >>
3 5 [+] dupdip 3702 2 >>
3 5 + 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 925
@ -336,144 +336,137 @@ V('3 3 3702 PE1.2')
V('0 0 14811 7 [PE1.2] times pop')
```
. 0 0 14811 7 [PE1.2] times pop
0 . 0 14811 7 [PE1.2] times pop
0 0 . 14811 7 [PE1.2] times pop
0 0 14811 . 7 [PE1.2] times pop
0 0 14811 7 . [PE1.2] times pop
0 0 14811 7 [PE1.2] . times pop
0 0 14811 [PE1.2] . i 6 [PE1.2] times pop
0 0 14811 . PE1.2 6 [PE1.2] times pop
0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 . + 3 14811 2 >> 6 [PE1.2] times pop
3 . 3 14811 2 >> 6 [PE1.2] times pop
3 3 . 14811 2 >> 6 [PE1.2] times pop
3 3 14811 . 2 >> 6 [PE1.2] times pop
3 3 14811 2 . >> 6 [PE1.2] times pop
3 3 3702 . 6 [PE1.2] times pop
3 3 3702 6 . [PE1.2] times pop
3 3 3702 6 [PE1.2] . times pop
3 3 3702 [PE1.2] . i 5 [PE1.2] times pop
3 3 3702 . PE1.2 5 [PE1.2] times pop
3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 . + 5 3702 2 >> 5 [PE1.2] times pop
8 . 5 3702 2 >> 5 [PE1.2] times pop
8 5 . 3702 2 >> 5 [PE1.2] times pop
8 5 3702 . 2 >> 5 [PE1.2] times pop
8 5 3702 2 . >> 5 [PE1.2] times pop
8 5 925 . 5 [PE1.2] times pop
8 5 925 5 . [PE1.2] times pop
8 5 925 5 [PE1.2] . times pop
8 5 925 [PE1.2] . i 4 [PE1.2] times pop
8 5 925 . PE1.2 4 [PE1.2] times pop
8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop
8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop
8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop
8 6 . + 6 925 2 >> 4 [PE1.2] times pop
14 . 6 925 2 >> 4 [PE1.2] times pop
14 6 . 925 2 >> 4 [PE1.2] times pop
14 6 925 . 2 >> 4 [PE1.2] times pop
14 6 925 2 . >> 4 [PE1.2] times pop
14 6 231 . 4 [PE1.2] times pop
14 6 231 4 . [PE1.2] times pop
14 6 231 4 [PE1.2] . times pop
14 6 231 [PE1.2] . i 3 [PE1.2] times pop
14 6 231 . PE1.2 3 [PE1.2] times pop
14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop
14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop
14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop
14 9 . + 9 231 2 >> 3 [PE1.2] times pop
23 . 9 231 2 >> 3 [PE1.2] times pop
23 9 . 231 2 >> 3 [PE1.2] times pop
23 9 231 . 2 >> 3 [PE1.2] times pop
23 9 231 2 . >> 3 [PE1.2] times pop
23 9 57 . 3 [PE1.2] times pop
23 9 57 3 . [PE1.2] times pop
23 9 57 3 [PE1.2] . times pop
23 9 57 [PE1.2] . i 2 [PE1.2] times pop
23 9 57 . PE1.2 2 [PE1.2] times pop
23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop
23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop
23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop
23 10 . + 10 57 2 >> 2 [PE1.2] times pop
33 . 10 57 2 >> 2 [PE1.2] times pop
33 10 . 57 2 >> 2 [PE1.2] times pop
33 10 57 . 2 >> 2 [PE1.2] times pop
33 10 57 2 . >> 2 [PE1.2] times pop
33 10 14 . 2 [PE1.2] times pop
33 10 14 2 . [PE1.2] times pop
33 10 14 2 [PE1.2] . times pop
33 10 14 [PE1.2] . i 1 [PE1.2] times pop
33 10 14 . PE1.2 1 [PE1.2] times pop
33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop
33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop
33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop
33 12 . + 12 14 2 >> 1 [PE1.2] times pop
45 . 12 14 2 >> 1 [PE1.2] times pop
45 12 . 14 2 >> 1 [PE1.2] times pop
45 12 14 . 2 >> 1 [PE1.2] times pop
45 12 14 2 . >> 1 [PE1.2] times pop
45 12 3 . 1 [PE1.2] times pop
45 12 3 1 . [PE1.2] times pop
45 12 3 1 [PE1.2] . times pop
45 12 3 [PE1.2] . i pop
45 12 3 . PE1.2 pop
45 12 3 . [3 & PE1.1] dupdip 2 >> pop
45 12 3 [3 & PE1.1] . dupdip 2 >> pop
45 12 3 . 3 & PE1.1 3 2 >> pop
45 12 3 3 . & PE1.1 3 2 >> pop
45 12 3 . PE1.1 3 2 >> pop
45 12 3 . + [+] dupdip 3 2 >> pop
45 15 . [+] dupdip 3 2 >> pop
45 15 [+] . dupdip 3 2 >> pop
45 15 . + 15 3 2 >> pop
60 . 15 3 2 >> pop
60 15 . 3 2 >> pop
60 15 3 . 2 >> pop
60 15 3 2 . >> pop
60 15 0 . pop
60 15 .
• 0 0 14811 7 [PE1.2] times pop
0 • 0 14811 7 [PE1.2] times pop
0 0 • 14811 7 [PE1.2] times pop
0 0 14811 • 7 [PE1.2] times pop
0 0 14811 7 • [PE1.2] times pop
0 0 14811 7 [PE1.2] • times pop
0 0 14811 • PE1.2 6 [PE1.2] times pop
0 0 14811 • [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 [3 & PE1.1] • dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 • 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 14811 3 • & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 • PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 • + [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 • [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 [+] • dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 • + 3 14811 2 >> 6 [PE1.2] times pop
3 • 3 14811 2 >> 6 [PE1.2] times pop
3 3 • 14811 2 >> 6 [PE1.2] times pop
3 3 14811 • 2 >> 6 [PE1.2] times pop
3 3 14811 2 • >> 6 [PE1.2] times pop
3 3 3702 • 6 [PE1.2] times pop
3 3 3702 6 • [PE1.2] times pop
3 3 3702 6 [PE1.2] • times pop
3 3 3702 • PE1.2 5 [PE1.2] times pop
3 3 3702 • [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 [3 & PE1.1] • dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 • 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 3702 3 • & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 • PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 • + [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 • [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 [+] • dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 • + 5 3702 2 >> 5 [PE1.2] times pop
8 • 5 3702 2 >> 5 [PE1.2] times pop
8 5 • 3702 2 >> 5 [PE1.2] times pop
8 5 3702 • 2 >> 5 [PE1.2] times pop
8 5 3702 2 • >> 5 [PE1.2] times pop
8 5 925 • 5 [PE1.2] times pop
8 5 925 5 • [PE1.2] times pop
8 5 925 5 [PE1.2] • times pop
8 5 925 • PE1.2 4 [PE1.2] times pop
8 5 925 • [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop
8 5 925 [3 & PE1.1] • dupdip 2 >> 4 [PE1.2] times pop
8 5 925 • 3 & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 925 3 • & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 • PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 • + [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 • [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 [+] • dupdip 925 2 >> 4 [PE1.2] times pop
8 6 • + 6 925 2 >> 4 [PE1.2] times pop
14 • 6 925 2 >> 4 [PE1.2] times pop
14 6 • 925 2 >> 4 [PE1.2] times pop
14 6 925 • 2 >> 4 [PE1.2] times pop
14 6 925 2 • >> 4 [PE1.2] times pop
14 6 231 • 4 [PE1.2] times pop
14 6 231 4 • [PE1.2] times pop
14 6 231 4 [PE1.2] • times pop
14 6 231 • PE1.2 3 [PE1.2] times pop
14 6 231 • [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop
14 6 231 [3 & PE1.1] • dupdip 2 >> 3 [PE1.2] times pop
14 6 231 • 3 & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 231 3 • & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 • PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 • + [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 • [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 [+] • dupdip 231 2 >> 3 [PE1.2] times pop
14 9 • + 9 231 2 >> 3 [PE1.2] times pop
23 • 9 231 2 >> 3 [PE1.2] times pop
23 9 • 231 2 >> 3 [PE1.2] times pop
23 9 231 • 2 >> 3 [PE1.2] times pop
23 9 231 2 • >> 3 [PE1.2] times pop
23 9 57 • 3 [PE1.2] times pop
23 9 57 3 • [PE1.2] times pop
23 9 57 3 [PE1.2] • times pop
23 9 57 • PE1.2 2 [PE1.2] times pop
23 9 57 • [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop
23 9 57 [3 & PE1.1] • dupdip 2 >> 2 [PE1.2] times pop
23 9 57 • 3 & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 57 3 • & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 • PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 • + [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 • [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 [+] • dupdip 57 2 >> 2 [PE1.2] times pop
23 10 • + 10 57 2 >> 2 [PE1.2] times pop
33 • 10 57 2 >> 2 [PE1.2] times pop
33 10 • 57 2 >> 2 [PE1.2] times pop
33 10 57 • 2 >> 2 [PE1.2] times pop
33 10 57 2 • >> 2 [PE1.2] times pop
33 10 14 • 2 [PE1.2] times pop
33 10 14 2 • [PE1.2] times pop
33 10 14 2 [PE1.2] • times pop
33 10 14 • PE1.2 1 [PE1.2] times pop
33 10 14 • [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop
33 10 14 [3 & PE1.1] • dupdip 2 >> 1 [PE1.2] times pop
33 10 14 • 3 & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 14 3 • & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 • PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 • + [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 • [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 [+] • dupdip 14 2 >> 1 [PE1.2] times pop
33 12 • + 12 14 2 >> 1 [PE1.2] times pop
45 • 12 14 2 >> 1 [PE1.2] times pop
45 12 • 14 2 >> 1 [PE1.2] times pop
45 12 14 • 2 >> 1 [PE1.2] times pop
45 12 14 2 • >> 1 [PE1.2] times pop
45 12 3 • 1 [PE1.2] times pop
45 12 3 1 • [PE1.2] times pop
45 12 3 1 [PE1.2] • times pop
45 12 3 • PE1.2 pop
45 12 3 • [3 & PE1.1] dupdip 2 >> pop
45 12 3 [3 & PE1.1] • dupdip 2 >> pop
45 12 3 • 3 & PE1.1 3 2 >> pop
45 12 3 3 • & PE1.1 3 2 >> pop
45 12 3 • PE1.1 3 2 >> pop
45 12 3 • + [+] dupdip 3 2 >> pop
45 15 • [+] dupdip 3 2 >> pop
45 15 [+] • dupdip 3 2 >> pop
45 15 • + 15 3 2 >> pop
60 • 15 3 2 >> pop
60 15 • 3 2 >> pop
60 15 3 • 2 >> pop
60 15 3 2 • >> pop
60 15 0 • pop
60 15 •
And so we have at last:
```python
define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')
define('PE1 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')
```
@ -493,14 +486,14 @@ Let's refactor.
```python
define('PE1.3 == 14811 swap [PE1.2] times pop')
define('PE1.3 14811 swap [PE1.2] times pop')
```
Now we can simplify the definition above:
```python
define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop')
define('PE1 0 0 66 [7 PE1.3] times 4 PE1.3 pop')
```
@ -523,7 +516,7 @@ It's a little clunky iterating sixty-six times though the seven numbers then fou
```python
define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')
define('PE1.terms [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')
```
@ -602,7 +595,7 @@ From the above example we can deduce that the sum of the first N positive intege
```python
define('F == dup ++ * 2 floordiv')
define('F dup ++ * 2 floordiv')
```
@ -610,14 +603,14 @@ define('F == dup ++ * 2 floordiv')
V('10 F')
```
. 10 F
10 . F
10 . dup ++ * 2 floordiv
10 10 . ++ * 2 floordiv
10 11 . * 2 floordiv
110 . 2 floordiv
110 2 . floordiv
55 .
10 F
10 F
10 dup ++ * 2 floordiv
10 10 ++ * 2 floordiv
10 11 * 2 floordiv
110 2 floordiv
110 2 floordiv
55
## Generalizing to Blocks of Terms

View File

@ -1,4 +1,4 @@
`Project Euler, first problem: “Multiples of 3 and 5” <https://projecteuler.net/problem=1>`__
`Project Euler, first problem: "Multiples of 3 and 5" <https://projecteuler.net/problem=1>`__
=============================================================================================
::
@ -7,37 +7,37 @@
Find the sum of all the multiples of 3 or 5 below 1000.
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
Lets create a predicate that returns ``True`` if a number is a multiple
Let's create a predicate that returns ``True`` if a number is a multiple
of 3 or 5 and ``False`` otherwise.
.. code:: ipython2
.. code:: ipython3
define('P == [3 % not] dupdip 5 % not or')
define('P [3 % not] dupdip 5 % not or')
.. code:: ipython2
.. code:: ipython3
V('80 P')
.. parsed-literal::
. 80 P
80 . P
80 . [3 % not] dupdip 5 % not or
80 [3 % not] . dupdip 5 % not or
80 . 3 % not 80 5 % not or
80 3 . % not 80 5 % not or
2 . not 80 5 % not or
False . 80 5 % not or
False 80 . 5 % not or
False 80 5 . % not or
False 0 . not or
False True . or
True .
80 P
80 P
80 [3 % not] dupdip 5 % not or
80 [3 % not] dupdip 5 % not or
80 3 % not 80 5 % not or
80 3 % not 80 5 % not or
2 not 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 80 5 % not or
False 0 not or
False True or
True
Given the predicate function ``P`` a suitable program is:
@ -97,115 +97,115 @@ the counter to the running sum. This function will do that:
PE1.1 == + [+] dupdip
.. code:: ipython2
.. code:: ipython3
define('PE1.1 == + [+] dupdip')
define('PE1.1 + [+] dupdip')
.. code:: ipython2
.. code:: ipython3
V('0 0 3 PE1.1')
.. parsed-literal::
. 0 0 3 PE1.1
0 . 0 3 PE1.1
0 0 . 3 PE1.1
0 0 3 . PE1.1
0 0 3 . + [+] dupdip
0 3 . [+] dupdip
0 3 [+] . dupdip
0 3 . + 3
3 . 3
3 3 .
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 PE1.1
0 0 3 + [+] dupdip
0 3 [+] dupdip
0 3 [+] dupdip
0 3 + 3
3 3
3 3
.. code:: ipython2
.. code:: ipython3
V('0 0 [3 2 1 3 1 2 3] [PE1.1] step')
.. parsed-literal::
. 0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 . 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 . [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] . [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] . step
0 0 3 [PE1.1] . i [2 1 3 1 2 3] [PE1.1] step
0 0 3 . PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 . + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] . dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 . + 3 [2 1 3 1 2 3] [PE1.1] step
3 . 3 [2 1 3 1 2 3] [PE1.1] step
3 3 . [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] . [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] . step
3 3 2 [PE1.1] . i [1 3 1 2 3] [PE1.1] step
3 3 2 . PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 . + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 . [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] . dupdip [1 3 1 2 3] [PE1.1] step
3 5 . + 5 [1 3 1 2 3] [PE1.1] step
8 . 5 [1 3 1 2 3] [PE1.1] step
8 5 . [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] . [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] . step
8 5 1 [PE1.1] . i [3 1 2 3] [PE1.1] step
8 5 1 . PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 . + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 . [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] . dupdip [3 1 2 3] [PE1.1] step
8 6 . + 6 [3 1 2 3] [PE1.1] step
14 . 6 [3 1 2 3] [PE1.1] step
14 6 . [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] . [PE1.1] step
14 6 [3 1 2 3] [PE1.1] . step
14 6 3 [PE1.1] . i [1 2 3] [PE1.1] step
14 6 3 . PE1.1 [1 2 3] [PE1.1] step
14 6 3 . + [+] dupdip [1 2 3] [PE1.1] step
14 9 . [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] . dupdip [1 2 3] [PE1.1] step
14 9 . + 9 [1 2 3] [PE1.1] step
23 . 9 [1 2 3] [PE1.1] step
23 9 . [1 2 3] [PE1.1] step
23 9 [1 2 3] . [PE1.1] step
23 9 [1 2 3] [PE1.1] . step
23 9 1 [PE1.1] . i [2 3] [PE1.1] step
23 9 1 . PE1.1 [2 3] [PE1.1] step
23 9 1 . + [+] dupdip [2 3] [PE1.1] step
23 10 . [+] dupdip [2 3] [PE1.1] step
23 10 [+] . dupdip [2 3] [PE1.1] step
23 10 . + 10 [2 3] [PE1.1] step
33 . 10 [2 3] [PE1.1] step
33 10 . [2 3] [PE1.1] step
33 10 [2 3] . [PE1.1] step
33 10 [2 3] [PE1.1] . step
33 10 2 [PE1.1] . i [3] [PE1.1] step
33 10 2 . PE1.1 [3] [PE1.1] step
33 10 2 . + [+] dupdip [3] [PE1.1] step
33 12 . [+] dupdip [3] [PE1.1] step
33 12 [+] . dupdip [3] [PE1.1] step
33 12 . + 12 [3] [PE1.1] step
45 . 12 [3] [PE1.1] step
45 12 . [3] [PE1.1] step
45 12 [3] . [PE1.1] step
45 12 [3] [PE1.1] . step
45 12 3 [PE1.1] . i
45 12 3 . PE1.1
45 12 3 . + [+] dupdip
45 15 . [+] dupdip
45 15 [+] . dupdip
45 15 . + 15
60 . 15
60 15 .
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 [3 2 1 3 1 2 3] [PE1.1] step
0 0 3 [PE1.1] i [2 1 3 1 2 3] [PE1.1] step
0 0 3 PE1.1 [2 1 3 1 2 3] [PE1.1] step
0 0 3 + [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 [+] dupdip [2 1 3 1 2 3] [PE1.1] step
0 3 + 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 [2 1 3 1 2 3] [PE1.1] step
3 3 2 [PE1.1] i [1 3 1 2 3] [PE1.1] step
3 3 2 PE1.1 [1 3 1 2 3] [PE1.1] step
3 3 2 + [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 [+] dupdip [1 3 1 2 3] [PE1.1] step
3 5 + 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 [1 3 1 2 3] [PE1.1] step
8 5 1 [PE1.1] i [3 1 2 3] [PE1.1] step
8 5 1 PE1.1 [3 1 2 3] [PE1.1] step
8 5 1 + [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 [+] dupdip [3 1 2 3] [PE1.1] step
8 6 + 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 [3 1 2 3] [PE1.1] step
14 6 3 [PE1.1] i [1 2 3] [PE1.1] step
14 6 3 PE1.1 [1 2 3] [PE1.1] step
14 6 3 + [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 [+] dupdip [1 2 3] [PE1.1] step
14 9 + 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 [1 2 3] [PE1.1] step
23 9 1 [PE1.1] i [2 3] [PE1.1] step
23 9 1 PE1.1 [2 3] [PE1.1] step
23 9 1 + [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 [+] dupdip [2 3] [PE1.1] step
23 10 + 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 [2 3] [PE1.1] step
33 10 2 [PE1.1] i [3] [PE1.1] step
33 10 2 PE1.1 [3] [PE1.1] step
33 10 2 + [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 [+] dupdip [3] [PE1.1] step
33 12 + 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 [3] [PE1.1] step
45 12 3 [PE1.1] i
45 12 3 PE1.1
45 12 3 + [+] dupdip
45 15 [+] dupdip
45 15 [+] dupdip
45 15 + 15
60 15
60 15
So one ``step`` through all seven terms brings the counter to 15 and the
total to 60.
.. code:: ipython2
.. code:: ipython3
1000 / 15
@ -214,11 +214,11 @@ total to 60.
.. parsed-literal::
66
66.66666666666667
.. code:: ipython2
.. code:: ipython3
66 * 15
@ -231,7 +231,7 @@ total to 60.
.. code:: ipython2
.. code:: ipython3
1000 - 990
@ -246,7 +246,7 @@ total to 60.
We only want the terms *less than* 1000.
.. code:: ipython2
.. code:: ipython3
999 - 990
@ -262,11 +262,11 @@ We only want the terms *less than* 1000.
That means we want to run the full list of numbers sixty-six times to
get to 990 and then the first four numbers 3 2 1 3 to get to 999.
.. code:: ipython2
.. code:: ipython3
define('PE1 == 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')
define('PE1 0 0 66 [[3 2 1 3 1 2 3] [PE1.1] step] times [3 2 1 3] [PE1.1] step pop')
.. code:: ipython2
.. code:: ipython3
J('PE1')
@ -276,8 +276,8 @@ get to 990 and then the first four numbers 3 2 1 3 to get to 999.
233168
This form uses no extra storage and produces no unused summands. Its
good but theres one more trick we can apply. The list of seven terms
This form uses no extra storage and produces no unused summands. It's
good but there's one more trick we can apply. The list of seven terms
takes up at least seven bytes. But notice that all of the terms are less
than four, and so each can fit in just two bits. We could store all
seven terms in just fourteen bits and use masking and shifts to pick out
@ -289,7 +289,7 @@ integer terms from the list.
3 2 1 3 1 2 3
0b 11 10 01 11 01 10 11 == 14811
.. code:: ipython2
.. code:: ipython3
0b11100111011011
@ -302,211 +302,204 @@ integer terms from the list.
.. code:: ipython2
.. code:: ipython3
define('PE1.2 == [3 & PE1.1] dupdip 2 >>')
define('PE1.2 [3 & PE1.1] dupdip 2 >>')
.. code:: ipython2
.. code:: ipython3
V('0 0 14811 PE1.2')
.. parsed-literal::
. 0 0 14811 PE1.2
0 . 0 14811 PE1.2
0 0 . 14811 PE1.2
0 0 14811 . PE1.2
0 0 14811 . [3 & PE1.1] dupdip 2 >>
0 0 14811 [3 & PE1.1] . dupdip 2 >>
0 0 14811 . 3 & PE1.1 14811 2 >>
0 0 14811 3 . & PE1.1 14811 2 >>
0 0 3 . PE1.1 14811 2 >>
0 0 3 . + [+] dupdip 14811 2 >>
0 3 . [+] dupdip 14811 2 >>
0 3 [+] . dupdip 14811 2 >>
0 3 . + 3 14811 2 >>
3 . 3 14811 2 >>
3 3 . 14811 2 >>
3 3 14811 . 2 >>
3 3 14811 2 . >>
3 3 3702 .
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 PE1.2
0 0 14811 [3 & PE1.1] dupdip 2 >>
0 0 14811 [3 & PE1.1] dupdip 2 >>
0 0 14811 3 & PE1.1 14811 2 >>
0 0 14811 3 & PE1.1 14811 2 >>
0 0 3 PE1.1 14811 2 >>
0 0 3 + [+] dupdip 14811 2 >>
0 3 [+] dupdip 14811 2 >>
0 3 [+] dupdip 14811 2 >>
0 3 + 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 14811 2 >>
3 3 3702
.. code:: ipython2
.. code:: ipython3
V('3 3 3702 PE1.2')
.. parsed-literal::
. 3 3 3702 PE1.2
3 . 3 3702 PE1.2
3 3 . 3702 PE1.2
3 3 3702 . PE1.2
3 3 3702 . [3 & PE1.1] dupdip 2 >>
3 3 3702 [3 & PE1.1] . dupdip 2 >>
3 3 3702 . 3 & PE1.1 3702 2 >>
3 3 3702 3 . & PE1.1 3702 2 >>
3 3 2 . PE1.1 3702 2 >>
3 3 2 . + [+] dupdip 3702 2 >>
3 5 . [+] dupdip 3702 2 >>
3 5 [+] . dupdip 3702 2 >>
3 5 . + 5 3702 2 >>
8 . 5 3702 2 >>
8 5 . 3702 2 >>
8 5 3702 . 2 >>
8 5 3702 2 . >>
8 5 925 .
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 PE1.2
3 3 3702 [3 & PE1.1] dupdip 2 >>
3 3 3702 [3 & PE1.1] dupdip 2 >>
3 3 3702 3 & PE1.1 3702 2 >>
3 3 3702 3 & PE1.1 3702 2 >>
3 3 2 PE1.1 3702 2 >>
3 3 2 + [+] dupdip 3702 2 >>
3 5 [+] dupdip 3702 2 >>
3 5 [+] dupdip 3702 2 >>
3 5 + 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 3702 2 >>
8 5 925
.. code:: ipython2
.. code:: ipython3
V('0 0 14811 7 [PE1.2] times pop')
.. parsed-literal::
. 0 0 14811 7 [PE1.2] times pop
0 . 0 14811 7 [PE1.2] times pop
0 0 . 14811 7 [PE1.2] times pop
0 0 14811 . 7 [PE1.2] times pop
0 0 14811 7 . [PE1.2] times pop
0 0 14811 7 [PE1.2] . times pop
0 0 14811 [PE1.2] . i 6 [PE1.2] times pop
0 0 14811 . PE1.2 6 [PE1.2] times pop
0 0 14811 . [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 [3 & PE1.1] . dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 . 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 14811 3 . & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 . PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 . + [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 . [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 [+] . dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 . + 3 14811 2 >> 6 [PE1.2] times pop
3 . 3 14811 2 >> 6 [PE1.2] times pop
3 3 . 14811 2 >> 6 [PE1.2] times pop
3 3 14811 . 2 >> 6 [PE1.2] times pop
3 3 14811 2 . >> 6 [PE1.2] times pop
3 3 3702 . 6 [PE1.2] times pop
3 3 3702 6 . [PE1.2] times pop
3 3 3702 6 [PE1.2] . times pop
3 3 3702 [PE1.2] . i 5 [PE1.2] times pop
3 3 3702 . PE1.2 5 [PE1.2] times pop
3 3 3702 . [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 [3 & PE1.1] . dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 . 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 3702 3 . & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 . PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 . + [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 . [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 [+] . dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 . + 5 3702 2 >> 5 [PE1.2] times pop
8 . 5 3702 2 >> 5 [PE1.2] times pop
8 5 . 3702 2 >> 5 [PE1.2] times pop
8 5 3702 . 2 >> 5 [PE1.2] times pop
8 5 3702 2 . >> 5 [PE1.2] times pop
8 5 925 . 5 [PE1.2] times pop
8 5 925 5 . [PE1.2] times pop
8 5 925 5 [PE1.2] . times pop
8 5 925 [PE1.2] . i 4 [PE1.2] times pop
8 5 925 . PE1.2 4 [PE1.2] times pop
8 5 925 . [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop
8 5 925 [3 & PE1.1] . dupdip 2 >> 4 [PE1.2] times pop
8 5 925 . 3 & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 925 3 . & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 . PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 . + [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 . [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 [+] . dupdip 925 2 >> 4 [PE1.2] times pop
8 6 . + 6 925 2 >> 4 [PE1.2] times pop
14 . 6 925 2 >> 4 [PE1.2] times pop
14 6 . 925 2 >> 4 [PE1.2] times pop
14 6 925 . 2 >> 4 [PE1.2] times pop
14 6 925 2 . >> 4 [PE1.2] times pop
14 6 231 . 4 [PE1.2] times pop
14 6 231 4 . [PE1.2] times pop
14 6 231 4 [PE1.2] . times pop
14 6 231 [PE1.2] . i 3 [PE1.2] times pop
14 6 231 . PE1.2 3 [PE1.2] times pop
14 6 231 . [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop
14 6 231 [3 & PE1.1] . dupdip 2 >> 3 [PE1.2] times pop
14 6 231 . 3 & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 231 3 . & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 . PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 . + [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 . [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 [+] . dupdip 231 2 >> 3 [PE1.2] times pop
14 9 . + 9 231 2 >> 3 [PE1.2] times pop
23 . 9 231 2 >> 3 [PE1.2] times pop
23 9 . 231 2 >> 3 [PE1.2] times pop
23 9 231 . 2 >> 3 [PE1.2] times pop
23 9 231 2 . >> 3 [PE1.2] times pop
23 9 57 . 3 [PE1.2] times pop
23 9 57 3 . [PE1.2] times pop
23 9 57 3 [PE1.2] . times pop
23 9 57 [PE1.2] . i 2 [PE1.2] times pop
23 9 57 . PE1.2 2 [PE1.2] times pop
23 9 57 . [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop
23 9 57 [3 & PE1.1] . dupdip 2 >> 2 [PE1.2] times pop
23 9 57 . 3 & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 57 3 . & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 . PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 . + [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 . [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 [+] . dupdip 57 2 >> 2 [PE1.2] times pop
23 10 . + 10 57 2 >> 2 [PE1.2] times pop
33 . 10 57 2 >> 2 [PE1.2] times pop
33 10 . 57 2 >> 2 [PE1.2] times pop
33 10 57 . 2 >> 2 [PE1.2] times pop
33 10 57 2 . >> 2 [PE1.2] times pop
33 10 14 . 2 [PE1.2] times pop
33 10 14 2 . [PE1.2] times pop
33 10 14 2 [PE1.2] . times pop
33 10 14 [PE1.2] . i 1 [PE1.2] times pop
33 10 14 . PE1.2 1 [PE1.2] times pop
33 10 14 . [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop
33 10 14 [3 & PE1.1] . dupdip 2 >> 1 [PE1.2] times pop
33 10 14 . 3 & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 14 3 . & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 . PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 . + [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 . [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 [+] . dupdip 14 2 >> 1 [PE1.2] times pop
33 12 . + 12 14 2 >> 1 [PE1.2] times pop
45 . 12 14 2 >> 1 [PE1.2] times pop
45 12 . 14 2 >> 1 [PE1.2] times pop
45 12 14 . 2 >> 1 [PE1.2] times pop
45 12 14 2 . >> 1 [PE1.2] times pop
45 12 3 . 1 [PE1.2] times pop
45 12 3 1 . [PE1.2] times pop
45 12 3 1 [PE1.2] . times pop
45 12 3 [PE1.2] . i pop
45 12 3 . PE1.2 pop
45 12 3 . [3 & PE1.1] dupdip 2 >> pop
45 12 3 [3 & PE1.1] . dupdip 2 >> pop
45 12 3 . 3 & PE1.1 3 2 >> pop
45 12 3 3 . & PE1.1 3 2 >> pop
45 12 3 . PE1.1 3 2 >> pop
45 12 3 . + [+] dupdip 3 2 >> pop
45 15 . [+] dupdip 3 2 >> pop
45 15 [+] . dupdip 3 2 >> pop
45 15 . + 15 3 2 >> pop
60 . 15 3 2 >> pop
60 15 . 3 2 >> pop
60 15 3 . 2 >> pop
60 15 3 2 . >> pop
60 15 0 . pop
60 15 .
• 0 0 14811 7 [PE1.2] times pop
0 • 0 14811 7 [PE1.2] times pop
0 0 • 14811 7 [PE1.2] times pop
0 0 14811 • 7 [PE1.2] times pop
0 0 14811 7 • [PE1.2] times pop
0 0 14811 7 [PE1.2] • times pop
0 0 14811 • PE1.2 6 [PE1.2] times pop
0 0 14811 • [3 & PE1.1] dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 [3 & PE1.1] • dupdip 2 >> 6 [PE1.2] times pop
0 0 14811 • 3 & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 14811 3 • & PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 • PE1.1 14811 2 >> 6 [PE1.2] times pop
0 0 3 • + [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 • [+] dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 [+] • dupdip 14811 2 >> 6 [PE1.2] times pop
0 3 • + 3 14811 2 >> 6 [PE1.2] times pop
3 • 3 14811 2 >> 6 [PE1.2] times pop
3 3 • 14811 2 >> 6 [PE1.2] times pop
3 3 14811 • 2 >> 6 [PE1.2] times pop
3 3 14811 2 • >> 6 [PE1.2] times pop
3 3 3702 • 6 [PE1.2] times pop
3 3 3702 6 • [PE1.2] times pop
3 3 3702 6 [PE1.2] • times pop
3 3 3702 • PE1.2 5 [PE1.2] times pop
3 3 3702 • [3 & PE1.1] dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 [3 & PE1.1] • dupdip 2 >> 5 [PE1.2] times pop
3 3 3702 • 3 & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 3702 3 • & PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 • PE1.1 3702 2 >> 5 [PE1.2] times pop
3 3 2 • + [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 • [+] dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 [+] • dupdip 3702 2 >> 5 [PE1.2] times pop
3 5 • + 5 3702 2 >> 5 [PE1.2] times pop
8 • 5 3702 2 >> 5 [PE1.2] times pop
8 5 • 3702 2 >> 5 [PE1.2] times pop
8 5 3702 • 2 >> 5 [PE1.2] times pop
8 5 3702 2 • >> 5 [PE1.2] times pop
8 5 925 • 5 [PE1.2] times pop
8 5 925 5 • [PE1.2] times pop
8 5 925 5 [PE1.2] • times pop
8 5 925 • PE1.2 4 [PE1.2] times pop
8 5 925 • [3 & PE1.1] dupdip 2 >> 4 [PE1.2] times pop
8 5 925 [3 & PE1.1] • dupdip 2 >> 4 [PE1.2] times pop
8 5 925 • 3 & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 925 3 • & PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 • PE1.1 925 2 >> 4 [PE1.2] times pop
8 5 1 • + [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 • [+] dupdip 925 2 >> 4 [PE1.2] times pop
8 6 [+] • dupdip 925 2 >> 4 [PE1.2] times pop
8 6 • + 6 925 2 >> 4 [PE1.2] times pop
14 • 6 925 2 >> 4 [PE1.2] times pop
14 6 • 925 2 >> 4 [PE1.2] times pop
14 6 925 • 2 >> 4 [PE1.2] times pop
14 6 925 2 • >> 4 [PE1.2] times pop
14 6 231 • 4 [PE1.2] times pop
14 6 231 4 • [PE1.2] times pop
14 6 231 4 [PE1.2] • times pop
14 6 231 • PE1.2 3 [PE1.2] times pop
14 6 231 • [3 & PE1.1] dupdip 2 >> 3 [PE1.2] times pop
14 6 231 [3 & PE1.1] • dupdip 2 >> 3 [PE1.2] times pop
14 6 231 • 3 & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 231 3 • & PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 • PE1.1 231 2 >> 3 [PE1.2] times pop
14 6 3 • + [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 • [+] dupdip 231 2 >> 3 [PE1.2] times pop
14 9 [+] • dupdip 231 2 >> 3 [PE1.2] times pop
14 9 • + 9 231 2 >> 3 [PE1.2] times pop
23 • 9 231 2 >> 3 [PE1.2] times pop
23 9 • 231 2 >> 3 [PE1.2] times pop
23 9 231 • 2 >> 3 [PE1.2] times pop
23 9 231 2 • >> 3 [PE1.2] times pop
23 9 57 • 3 [PE1.2] times pop
23 9 57 3 • [PE1.2] times pop
23 9 57 3 [PE1.2] • times pop
23 9 57 • PE1.2 2 [PE1.2] times pop
23 9 57 • [3 & PE1.1] dupdip 2 >> 2 [PE1.2] times pop
23 9 57 [3 & PE1.1] • dupdip 2 >> 2 [PE1.2] times pop
23 9 57 • 3 & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 57 3 • & PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 • PE1.1 57 2 >> 2 [PE1.2] times pop
23 9 1 • + [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 • [+] dupdip 57 2 >> 2 [PE1.2] times pop
23 10 [+] • dupdip 57 2 >> 2 [PE1.2] times pop
23 10 • + 10 57 2 >> 2 [PE1.2] times pop
33 • 10 57 2 >> 2 [PE1.2] times pop
33 10 • 57 2 >> 2 [PE1.2] times pop
33 10 57 • 2 >> 2 [PE1.2] times pop
33 10 57 2 • >> 2 [PE1.2] times pop
33 10 14 • 2 [PE1.2] times pop
33 10 14 2 • [PE1.2] times pop
33 10 14 2 [PE1.2] • times pop
33 10 14 • PE1.2 1 [PE1.2] times pop
33 10 14 • [3 & PE1.1] dupdip 2 >> 1 [PE1.2] times pop
33 10 14 [3 & PE1.1] • dupdip 2 >> 1 [PE1.2] times pop
33 10 14 • 3 & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 14 3 • & PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 • PE1.1 14 2 >> 1 [PE1.2] times pop
33 10 2 • + [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 • [+] dupdip 14 2 >> 1 [PE1.2] times pop
33 12 [+] • dupdip 14 2 >> 1 [PE1.2] times pop
33 12 • + 12 14 2 >> 1 [PE1.2] times pop
45 • 12 14 2 >> 1 [PE1.2] times pop
45 12 • 14 2 >> 1 [PE1.2] times pop
45 12 14 • 2 >> 1 [PE1.2] times pop
45 12 14 2 • >> 1 [PE1.2] times pop
45 12 3 • 1 [PE1.2] times pop
45 12 3 1 • [PE1.2] times pop
45 12 3 1 [PE1.2] • times pop
45 12 3 • PE1.2 pop
45 12 3 • [3 & PE1.1] dupdip 2 >> pop
45 12 3 [3 & PE1.1] • dupdip 2 >> pop
45 12 3 • 3 & PE1.1 3 2 >> pop
45 12 3 3 • & PE1.1 3 2 >> pop
45 12 3 • PE1.1 3 2 >> pop
45 12 3 • + [+] dupdip 3 2 >> pop
45 15 • [+] dupdip 3 2 >> pop
45 15 [+] • dupdip 3 2 >> pop
45 15 • + 15 3 2 >> pop
60 • 15 3 2 >> pop
60 15 • 3 2 >> pop
60 15 3 • 2 >> pop
60 15 3 2 • >> pop
60 15 0 • pop
60 15 •
And so we have at last:
.. code:: ipython2
.. code:: ipython3
define('PE1 == 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')
define('PE1 0 0 66 [14811 7 [PE1.2] times pop] times 14811 4 [PE1.2] times popop')
.. code:: ipython2
.. code:: ipython3
J('PE1')
@ -516,7 +509,7 @@ And so we have at last:
233168
Lets refactor.
Let's refactor.
::
@ -525,17 +518,17 @@ Lets refactor.
14811 n [PE1.2] times pop
n 14811 swap [PE1.2] times pop
.. code:: ipython2
.. code:: ipython3
define('PE1.3 == 14811 swap [PE1.2] times pop')
define('PE1.3 14811 swap [PE1.2] times pop')
Now we can simplify the definition above:
.. code:: ipython2
.. code:: ipython3
define('PE1 == 0 0 66 [7 PE1.3] times 4 PE1.3 pop')
define('PE1 0 0 66 [7 PE1.3] times 4 PE1.3 pop')
.. code:: ipython2
.. code:: ipython3
J('PE1')
@ -545,9 +538,9 @@ Now we can simplify the definition above:
233168
Heres our joy program all in one place. It doesnt make so much sense,
Here's our joy program all in one place. It doesn't make so much sense,
but if you have read through the above description of how it was derived
I hope its clear.
I hope it's clear.
::
@ -559,16 +552,16 @@ I hope its clear.
Generator Version
=================
Its a little clunky iterating sixty-six times though the seven numbers
It's a little clunky iterating sixty-six times though the seven numbers
then four more. In the *Generator Programs* notebook we derive a
generator that can be repeatedly driven by the ``x`` combinator to
produce a stream of the seven numbers repeating over and over again.
.. code:: ipython2
.. code:: ipython3
define('PE1.terms == [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')
define('PE1.terms [0 swap [dup [pop 14811] [] branch [3 &] dupdip 2 >>] dip rest cons]')
.. code:: ipython2
.. code:: ipython3
J('PE1.terms 21 [x] times')
@ -581,7 +574,7 @@ produce a stream of the seven numbers repeating over and over again.
We know from above that we need sixty-six times seven then four more
terms to reach up to but not over one thousand.
.. code:: ipython2
.. code:: ipython3
J('7 66 * 4 +')
@ -591,10 +584,10 @@ terms to reach up to but not over one thousand.
466
Here they are
~~~~~~~~~~~~~~
Here they are...
~~~~~~~~~~~~~~~~
.. code:: ipython2
.. code:: ipython3
J('PE1.terms 466 [x] times pop')
@ -604,10 +597,10 @@ Here they are…
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
…and they do sum to 999.
~~~~~~~~~~~~~~~~~~~~~~~~
...and they do sum to 999.
~~~~~~~~~~~~~~~~~~~~~~~~~~
.. code:: ipython2
.. code:: ipython3
J('[PE1.terms 466 [x] times pop] run sum')
@ -618,10 +611,10 @@ Here they are…
Now we can use ``PE1.1`` to accumulate the terms as we go, and then
``pop`` the generator and the counter from the stack when were done,
``pop`` the generator and the counter from the stack when we're done,
leaving just the sum.
.. code:: ipython2
.. code:: ipython3
J('0 0 PE1.terms 466 [x [PE1.1] dip] times popop')
@ -637,7 +630,7 @@ A little further analysis renders iteration unnecessary.
Consider finding the sum of the positive integers less than or equal to
ten.
.. code:: ipython2
.. code:: ipython3
J('[10 9 8 7 6 5 4 3 2 1] sum')
@ -666,28 +659,28 @@ positive integers is:
(N + 1) * N / 2
(The formula also works for odd values of N, Ill leave that to you if
(The formula also works for odd values of N, I'll leave that to you if
you want to work it out or you can take my word for it.)
.. code:: ipython2
.. code:: ipython3
define('F == dup ++ * 2 floordiv')
define('F dup ++ * 2 floordiv')
.. code:: ipython2
.. code:: ipython3
V('10 F')
.. parsed-literal::
. 10 F
10 . F
10 . dup ++ * 2 floordiv
10 10 . ++ * 2 floordiv
10 11 . * 2 floordiv
110 . 2 floordiv
110 2 . floordiv
55 .
10 F
10 F
10 dup ++ * 2 floordiv
10 10 ++ * 2 floordiv
10 11 * 2 floordiv
110 2 floordiv
110 2 floordiv
55
Generalizing to Blocks of Terms
@ -695,7 +688,7 @@ Generalizing to Blocks of Terms
We can apply the same reasoning to the PE1 problem.
Between 0 and 990 inclusive there are sixty-six “blocks” of seven terms
Between 0 and 990 inclusive there are sixty-six "blocks" of seven terms
each, starting with:
::
@ -708,9 +701,9 @@ And ending with:
[978 980 981 984 985 987 990]
If we reverse one of these two blocks and sum pairs
If we reverse one of these two blocks and sum pairs...
.. code:: ipython2
.. code:: ipython3
J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip')
@ -720,7 +713,7 @@ If we reverse one of these two blocks and sum pairs…
[[978 15] [980 12] [981 10] [984 9] [985 6] [987 5] [990 3]]
.. code:: ipython2
.. code:: ipython3
J('[3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map')
@ -733,7 +726,7 @@ If we reverse one of these two blocks and sum pairs…
(Interesting that the sequence of seven numbers appears again in the
rightmost digit of each term.)
.. code:: ipython2
.. code:: ipython3
J('[ 3 5 6 9 10 12 15] reverse [978 980 981 984 985 987 990] zip [sum] map sum')
@ -751,10 +744,10 @@ additional unpaired terms between 990 and 1000:
993 995 996 999
So we can give the “sum of all the multiples of 3 or 5 below 1000” like
So we can give the "sum of all the multiples of 3 or 5 below 1000" like
so:
.. code:: ipython2
.. code:: ipython3
J('6945 33 * [993 995 996 999] cons sum')
@ -764,7 +757,7 @@ so:
233168
Its worth noting, I think, that this same reasoning holds for any two
It's worth noting, I think, that this same reasoning holds for any two
numbers :math:`n` and :math:`m` the multiples of which we hope to sum.
The multiples would have a cycle of differences of length :math:`k` and
so we could compute the sum of :math:`Nk` multiples as above.
@ -781,7 +774,7 @@ Here we have 4 and 7, and you can read off the sequence of differences
directly from the diagram: 4 3 1 4 2 2 4 1 3 4.
Geometrically, the actual values of :math:`n` and :math:`m` and their
*lcm* dont matter, the pattern they make will always be symmetrical
*lcm* don't matter, the pattern they make will always be symmetrical
around its midpoint. The same reasoning holds for multiples of more than
two numbers.

View File

@ -13094,7 +13094,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13139,7 +13139,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;pair_up == dup uncons swap unit concat zip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;pair_up dup uncons swap unit concat zip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13152,7 +13152,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] pair_up&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] pair_up&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13183,7 +13183,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 3] pair_up&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 3] pair_up&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13251,7 +13251,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;total_matches == 0 swap [i [=] [pop +] [popop] ifte] step&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;total_matches 0 swap [i [=] [pop +] [popop] ifte] step&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13264,7 +13264,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] pair_up total_matches&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] pair_up total_matches&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13295,7 +13295,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 3] pair_up total_matches&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 3] pair_up total_matches&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13334,7 +13334,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.1 == pair_up total_matches&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.1 pair_up total_matches&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13347,7 +13347,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 2 2] AoC2017.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 2 2] AoC2017.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13378,7 +13378,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 1 1] AoC2017.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 1 1] AoC2017.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13409,7 +13409,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] AoC2017.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] AoC2017.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13440,7 +13440,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 1 2 1 2 1 2 9] AoC2017.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 1 2 1 2 1 2 9] AoC2017.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13471,7 +13471,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 1 2 1 2 1 2 9] AoC2017.1&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 1 2 1 2 1 2 9] AoC2017.1&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13514,7 +13514,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
<div class=" highlight hl-ipython3"><pre><span></span>
</pre></div>
</div>
@ -13537,7 +13537,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13576,7 +13576,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13607,7 +13607,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13620,7 +13620,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 1 2] AoC2017.1.extra&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 1 2] AoC2017.1.extra&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13651,7 +13651,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 1] AoC2017.1.extra&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 2 1] AoC2017.1.extra&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13682,7 +13682,7 @@ total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4 2 5] AoC2017.1.extra&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4 2 5] AoC2017.1.extra&#39;</span><span class="p">)</span>
</pre></div>
</div>

View File

@ -63,7 +63,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('pair_up == dup uncons swap unit concat zip')"
"define('pair_up dup uncons swap unit concat zip')"
]
},
{
@ -139,7 +139,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')"
"define('total_matches 0 swap [i [=] [pop +] [popop] ifte] step')"
]
},
{
@ -189,7 +189,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('AoC2017.1 == pair_up total_matches')"
"define('AoC2017.1 pair_up total_matches')"
]
},
{
@ -354,7 +354,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')"
"define('AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')"
]
},
{
@ -440,14 +440,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -39,7 +39,7 @@ Straightforward (although the order of each pair is reversed, due to the way `zi
```python
define('pair_up == dup uncons swap unit concat zip')
define('pair_up dup uncons swap unit concat zip')
```
@ -86,7 +86,7 @@ And thus:
```python
define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')
define('total_matches 0 swap [i [=] [pop +] [popop] ifte] step')
```
@ -109,7 +109,7 @@ Now we can define our main program and evaluate it on the examples.
```python
define('AoC2017.1 == pair_up total_matches')
define('AoC2017.1 pair_up total_matches')
```
@ -186,7 +186,7 @@ J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip')
```python
define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
define('AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
```

View File

@ -17,11 +17,11 @@ For example:
- 91212129 produces 9 because the only digit that matches the next one
is the last digit, 9.
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
Ill assume the input is a Joy sequence of integers (as opposed to a
I'll assume the input is a Joy sequence of integers (as opposed to a
string or something else.)
We might proceed by creating a word that makes a copy of the sequence
@ -33,7 +33,7 @@ a total if the pair matches.
AoC2017.1 == pair_up total_matches
Lets derive ``pair_up``:
Let's derive ``pair_up``:
::
@ -42,7 +42,7 @@ Lets derive ``pair_up``:
[[a b] [b c] [c a]]
Straightforward (although the order of each pair is reversed, due to the
way ``zip`` works, but it doesnt matter for this program):
way ``zip`` works, but it doesn't matter for this program):
::
@ -52,11 +52,11 @@ way ``zip`` works, but it doesnt matter for this program):
[a b c] [b c a] zip
[[b a] [c b] [a c]]
.. code:: ipython2
.. code:: ipython3
define('pair_up == dup uncons swap unit concat zip')
define('pair_up dup uncons swap unit concat zip')
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] pair_up')
@ -66,7 +66,7 @@ way ``zip`` works, but it doesnt matter for this program):
[[2 1] [3 2] [1 3]]
.. code:: ipython2
.. code:: ipython3
J('[1 2 2 3] pair_up')
@ -115,11 +115,11 @@ And thus:
total_matches == 0 swap [i [=] [pop +] [popop] ifte] step
.. code:: ipython2
.. code:: ipython3
define('total_matches == 0 swap [i [=] [pop +] [popop] ifte] step')
define('total_matches 0 swap [i [=] [pop +] [popop] ifte] step')
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] pair_up total_matches')
@ -129,7 +129,7 @@ And thus:
0
.. code:: ipython2
.. code:: ipython3
J('[1 2 2 3] pair_up total_matches')
@ -141,11 +141,11 @@ And thus:
Now we can define our main program and evaluate it on the examples.
.. code:: ipython2
.. code:: ipython3
define('AoC2017.1 == pair_up total_matches')
define('AoC2017.1 pair_up total_matches')
.. code:: ipython2
.. code:: ipython3
J('[1 1 2 2] AoC2017.1')
@ -155,7 +155,7 @@ Now we can define our main program and evaluate it on the examples.
3
.. code:: ipython2
.. code:: ipython3
J('[1 1 1 1] AoC2017.1')
@ -165,7 +165,7 @@ Now we can define our main program and evaluate it on the examples.
4
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4] AoC2017.1')
@ -175,7 +175,7 @@ Now we can define our main program and evaluate it on the examples.
0
.. code:: ipython2
.. code:: ipython3
J('[9 1 2 1 2 1 2 9] AoC2017.1')
@ -185,7 +185,7 @@ Now we can define our main program and evaluate it on the examples.
9
.. code:: ipython2
.. code:: ipython3
J('[9 1 2 1 2 1 2 9] AoC2017.1')
@ -203,13 +203,13 @@ Now we can define our main program and evaluate it on the examples.
AoC2017.1 == pair_up total_matches
Now the paired digit is “halfway” round.
Now the paired digit is "halfway" round.
::
[a b c d] dup size 2 / [drop] [take reverse] cleave concat zip
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave concat zip')
@ -219,9 +219,9 @@ Now the paired digit is “halfway” round.
[[3 1] [4 2] [1 3] [2 4]]
I realized that each pair is repeated
I realized that each pair is repeated...
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4] dup size 2 / [drop] [take reverse] cleave zip')
@ -231,11 +231,11 @@ I realized that each pair is repeated…
[1 2 3 4] [[1 3] [2 4]]
.. code:: ipython2
.. code:: ipython3
define('AoC2017.1.extra == dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
define('AoC2017.1.extra dup size 2 / [drop] [take reverse] cleave zip swap pop total_matches 2 *')
.. code:: ipython2
.. code:: ipython3
J('[1 2 1 2] AoC2017.1.extra')
@ -245,7 +245,7 @@ I realized that each pair is repeated…
6
.. code:: ipython2
.. code:: ipython3
J('[1 2 2 1] AoC2017.1.extra')
@ -255,7 +255,7 @@ I realized that each pair is repeated…
0
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4 2 5] AoC2017.1.extra')
@ -270,7 +270,7 @@ Refactor FTW
With Joy a great deal of the heuristics from Forth programming carry
over nicely. For example, refactoring into small, well-scoped commands
with mnemonic names
with mnemonic names...
::

View File

@ -13100,7 +13100,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13138,7 +13138,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;maxmin == [max] [min] cleave&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;maxmin [max] [min] cleave&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13151,7 +13151,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] maxmin&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] maxmin&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13195,7 +13195,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.2 == [maxmin - +] step_zero&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.2 [maxmin - +] step_zero&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13208,7 +13208,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1">[[5 1 9 5]</span>
<span class="s1"> [7 5 3]</span>
@ -13267,7 +13267,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[5 9 2 8] sort reverse&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[5 9 2 8] sort reverse&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13298,7 +13298,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 8 5 2] uncons [swap [divmod] cons] dupdip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[9 8 5 2] uncons [swap [divmod] cons] dupdip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13339,7 +13339,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13356,22 +13356,22 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre> . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
[8 5 2] . uncons swap [9 divmod] dup [i not] dip
8 [5 2] . swap [9 divmod] dup [i not] dip
[5 2] 8 . [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] . dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] . [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] . dip
[5 2] 8 [9 divmod] . i not [9 divmod]
[5 2] 8 . 9 divmod not [9 divmod]
[5 2] 8 9 . divmod not [9 divmod]
[5 2] 1 1 . not [9 divmod]
[5 2] 1 False . [9 divmod]
[5 2] 1 False [9 divmod] .
<pre> [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] uncons swap [9 divmod] dup [i not] dip
8 [5 2] swap [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] i not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 1 1 not [9 divmod]
[5 2] 1 False [9 divmod]
[5 2] 1 False [9 divmod]
</pre>
</div>
</div>
@ -13471,7 +13471,7 @@ uncons tuck uncons roll&gt; Q</code></pre>
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13604,7 +13604,7 @@ n [...] [not] [popop 0] [G] ifte
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;G [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13624,9 +13624,9 @@ n [...] [not] [popop 0] [G] ifte
</code></pre>
<p>It applies <code>G</code> using <code>nullary</code> because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by <code>G</code>.)</p>
<pre><code>find-result == [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] primrec
<pre><code>find-result == [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] tailrec
n [...] p [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] primrec
n [...] p [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] tailrec
</code></pre>
<p>The base-case is trivial, return the (non-zero) result. The recursive branch...</p>
@ -13648,7 +13648,7 @@ m [..] p find-result
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;find-result == [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] primrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;find-result [0 &gt;] [roll&gt; popop] [roll&lt; popop uncons [G] nullary] tailrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13658,10 +13658,10 @@ m [..] p find-result
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[11 9 8 7 3 2] 0 tuck find-result&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[11 9 8 7 3 2] 0 tuck find-result&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13697,10 +13697,10 @@ m [..] p find-result
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;prep-row == sort reverse 0 tuck&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;prep-row sort reverse 0 tuck&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13718,10 +13718,10 @@ m [..] p find-result
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC20017.2.extra == [prep-row find-result +] step_zero&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC20017.2.extra [prep-row find-result +] step_zero&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13734,7 +13734,7 @@ m [..] p find-result
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1">[[5 9 2 8]</span>
<span class="s1"> [9 4 7 3]</span>

View File

@ -60,7 +60,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('maxmin == [max] [min] cleave')"
"define('maxmin [max] [min] cleave')"
]
},
{
@ -97,7 +97,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('AoC2017.2 == [maxmin - +] step_zero')"
"define('AoC2017.2 [maxmin - +] step_zero')"
]
},
{
@ -197,22 +197,22 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip\n",
" [8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip\n",
" [8 5 2] . uncons swap [9 divmod] dup [i not] dip\n",
" 8 [5 2] . swap [9 divmod] dup [i not] dip\n",
" [5 2] 8 . [9 divmod] dup [i not] dip\n",
" [5 2] 8 [9 divmod] . dup [i not] dip\n",
" [5 2] 8 [9 divmod] [9 divmod] . [i not] dip\n",
"[5 2] 8 [9 divmod] [9 divmod] [i not] . dip\n",
" [5 2] 8 [9 divmod] . i not [9 divmod]\n",
" [5 2] 8 . 9 divmod not [9 divmod]\n",
" [5 2] 8 9 . divmod not [9 divmod]\n",
" [5 2] 1 1 . not [9 divmod]\n",
" [5 2] 1 False . [9 divmod]\n",
" [5 2] 1 False [9 divmod] . \n"
" [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip\n",
" [8 5 2] uncons swap [9 divmod] dup [i not] dip\n",
" 8 [5 2] swap [9 divmod] dup [i not] dip\n",
" [5 2] 8 [9 divmod] dup [i not] dip\n",
" [5 2] 8 [9 divmod] dup [i not] dip\n",
" [5 2] 8 [9 divmod] [9 divmod] [i not] dip\n",
"[5 2] 8 [9 divmod] [9 divmod] [i not] dip\n",
" [5 2] 8 [9 divmod] i not [9 divmod]\n",
" [5 2] 8 9 divmod not [9 divmod]\n",
" [5 2] 8 9 divmod not [9 divmod]\n",
" [5 2] 1 1 not [9 divmod]\n",
" [5 2] 1 False [9 divmod]\n",
" [5 2] 1 False [9 divmod] \n"
]
}
],
@ -418,7 +418,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')"
"define('G [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')"
]
},
{
@ -433,9 +433,9 @@
"\n",
"It applies `G` using `nullary` because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by `G`.)\n",
"\n",
" find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec\n",
" find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec\n",
"\n",
" n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec\n",
" n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec\n",
"\n",
"The base-case is trivial, return the (non-zero) result. The recursive branch...\n",
"\n",
@ -454,12 +454,12 @@
"metadata": {},
"outputs": [],
"source": [
"define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')"
"define('find-result [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec')"
]
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@ -483,11 +483,11 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"define('prep-row == sort reverse 0 tuck')"
"define('prep-row sort reverse 0 tuck')"
]
},
{
@ -499,11 +499,11 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"define('AoC20017.2.extra == [prep-row find-result +] step_zero')"
"define('AoC20017.2.extra [prep-row find-result +] step_zero')"
]
},
{
@ -539,14 +539,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -35,7 +35,7 @@ This function `F` must get the `max` and `min` of a row of numbers and subtract.
```python
define('maxmin == [max] [min] cleave')
define('maxmin [max] [min] cleave')
```
@ -54,7 +54,7 @@ So:
```python
define('AoC2017.2 == [maxmin - +] step_zero')
define('AoC2017.2 [maxmin - +] step_zero')
```
@ -114,22 +114,22 @@ J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')
V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')
```
. [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
[8 5 2] . uncons swap [9 divmod] dup [i not] dip
8 [5 2] . swap [9 divmod] dup [i not] dip
[5 2] 8 . [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] . dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] . [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] . dip
[5 2] 8 [9 divmod] . i not [9 divmod]
[5 2] 8 . 9 divmod not [9 divmod]
[5 2] 8 9 . divmod not [9 divmod]
[5 2] 1 1 . not [9 divmod]
[5 2] 1 False . [9 divmod]
[5 2] 1 False [9 divmod] .
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] uncons swap [9 divmod] dup [i not] dip
8 [5 2] swap [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] i not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 1 1 not [9 divmod]
[5 2] 1 False [9 divmod]
[5 2] 1 False [9 divmod]
## Tricky
@ -293,7 +293,7 @@ This `ifte` guards against empty sequences and returns zero in that case, otherw
```python
define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
define('G [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
```
Now we need a word that uses `G` on each (head, tail) pair of a sequence until it finds a (non-zero) result. It's going to be designed to work on a stack that has some candidate `n`, a sequence of possible divisors, and a result that is zero to signal to continue (a non-zero value implies that it is the discovered result):
@ -304,9 +304,9 @@ Now we need a word that uses `G` on each (head, tail) pair of a sequence until i
It applies `G` using `nullary` because if it fails with one candidate it needs the list to get the next one (the list is otherwise consumed by `G`.)
find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec
n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec
The base-case is trivial, return the (non-zero) result. The recursive branch...
@ -320,7 +320,7 @@ The puzzle states that the input is well-formed, meaning that we can expect a re
```python
define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')
define('find-result [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec')
```
@ -335,14 +335,14 @@ In order to get the thing started, we need to `sort` the list in descending orde
```python
define('prep-row == sort reverse 0 tuck')
define('prep-row sort reverse 0 tuck')
```
Now we can define our program.
```python
define('AoC20017.2.extra == [prep-row find-result +] step_zero')
define('AoC20017.2.extra [prep-row find-result +] step_zero')
```

View File

@ -15,19 +15,19 @@ For example, given the following spreadsheet:
7 5 3
2 4 6 8
- The first rows largest and smallest values are 9 and 1, and their
- The first row's largest and smallest values are 9 and 1, and their
difference is 8.
- The second rows largest and smallest values are 7 and 3, and their
- The second row's largest and smallest values are 7 and 3, and their
difference is 4.
- The third rows difference is 6.
- The third row's difference is 6.
In this example, the spreadsheets checksum would be 8 + 4 + 6 = 18.
In this example, the spreadsheet's checksum would be 8 + 4 + 6 = 18.
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
Ill assume the input is a Joy sequence of sequences of integers.
I'll assume the input is a Joy sequence of sequences of integers.
::
@ -45,11 +45,11 @@ This function ``F`` must get the ``max`` and ``min`` of a row of numbers
and subtract. We can define a helper function ``maxmin`` which does
this:
.. code:: ipython2
.. code:: ipython3
define('maxmin == [max] [min] cleave')
define('maxmin [max] [min] cleave')
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] maxmin')
@ -67,11 +67,11 @@ Then ``F`` just does that then subtracts the min from the max:
So:
.. code:: ipython2
.. code:: ipython3
define('AoC2017.2 == [maxmin - +] step_zero')
define('AoC2017.2 [maxmin - +] step_zero')
.. code:: ipython2
.. code:: ipython3
J('''
@ -87,10 +87,10 @@ So:
18
find the only two numbers in each row where one evenly divides the
...find the only two numbers in each row where one evenly divides the
other - that is, where the result of the division operation is a whole
number. They would like you to find those numbers on each line, divide
them, and add up each lines result.
them, and add up each line's result.
For example, given the following spreadsheet:
@ -107,9 +107,9 @@ For example, given the following spreadsheet:
In this example, the sum of the results would be 4 + 3 + 2 = 9.
What is the sum of each rows result in your puzzle input?
What is the sum of each row's result in your puzzle input?
.. code:: ipython2
.. code:: ipython3
J('[5 9 2 8] sort reverse')
@ -119,7 +119,7 @@ What is the sum of each rows result in your puzzle input?
[9 8 5 2]
.. code:: ipython2
.. code:: ipython3
J('[9 8 5 2] uncons [swap [divmod] cons] dupdip')
@ -134,35 +134,35 @@ What is the sum of each rows result in your puzzle input?
[9 8 5 2] uncons [swap [divmod] cons F] dupdip G
[8 5 2] [9 divmod] F [8 5 2] G
.. code:: ipython2
.. code:: ipython3
V('[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip')
.. parsed-literal::
. [8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] . [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] . [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] . dip dup [i not] dip
[8 5 2] . uncons swap [9 divmod] dup [i not] dip
8 [5 2] . swap [9 divmod] dup [i not] dip
[5 2] 8 . [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] . dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] . [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] . dip
[5 2] 8 [9 divmod] . i not [9 divmod]
[5 2] 8 . 9 divmod not [9 divmod]
[5 2] 8 9 . divmod not [9 divmod]
[5 2] 1 1 . not [9 divmod]
[5 2] 1 False . [9 divmod]
[5 2] 1 False [9 divmod] .
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] [9 divmod] [uncons swap] dip dup [i not] dip
[8 5 2] uncons swap [9 divmod] dup [i not] dip
8 [5 2] swap [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] dup [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] [9 divmod] [i not] dip
[5 2] 8 [9 divmod] i not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 8 9 divmod not [9 divmod]
[5 2] 1 1 not [9 divmod]
[5 2] 1 False [9 divmod]
[5 2] 1 False [9 divmod]
Tricky
------
Lets think.
Let's think.
Given a *sorted* sequence (from highest to lowest) we want to \* for
head, tail in sequence \* for term in tail: \* check if the head % term
@ -196,7 +196,7 @@ This suggests that ``Q`` should start with:
[a b c d] uncons dup roll<
[b c d] [b c d] a
Now we just have to ``pop`` it if we dont need it.
Now we just have to ``pop`` it if we don't need it.
::
@ -230,7 +230,7 @@ Now we just have to ``pop`` it if we dont need it.
uncons tuck uncons roll> Q
.. code:: ipython2
.. code:: ipython3
J('[8 5 3 2] 9 [swap] [% not] [cons] app2 popdd')
@ -310,7 +310,7 @@ candidates and return the result or zero:
---------------
0
Its a recursive function that conditionally executes the recursive part
It's a recursive function that conditionally executes the recursive part
of its recursive branch
::
@ -345,12 +345,12 @@ Otherwise, we have:
This ``ifte`` guards against empty sequences and returns zero in that
case, otherwise it executes ``G``.
.. code:: ipython2
.. code:: ipython3
define('G == [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
define('G [first % not] [first /] [rest [not] [popop 0]] [ifte] genrec')
Now we need a word that uses ``G`` on each (head, tail) pair of a
sequence until it finds a (non-zero) result. Its going to be designed
sequence until it finds a (non-zero) result. It's going to be designed
to work on a stack that has some candidate ``n``, a sequence of possible
divisors, and a result that is zero to signal to continue (a non-zero
value implies that it is the discovered result):
@ -367,12 +367,12 @@ consumed by ``G``.)
::
find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec
n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec
n [...] p [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec
The base-case is trivial, return the (non-zero) result. The recursive
branch
branch...
::
@ -386,11 +386,11 @@ The puzzle states that the input is well-formed, meaning that we can
expect a result before the row sequence empties and so do not need to
guard the ``uncons``.
.. code:: ipython2
.. code:: ipython3
define('find-result == [0 >] [roll> popop] [roll< popop uncons [G] nullary] primrec')
define('find-result [0 >] [roll> popop] [roll< popop uncons [G] nullary] tailrec')
.. code:: ipython2
.. code:: ipython3
J('[11 9 8 7 3 2] 0 tuck find-result')
@ -402,19 +402,19 @@ guard the ``uncons``.
In order to get the thing started, we need to ``sort`` the list in
descending order, then prime the ``find-result`` function with a dummy
candidate value and zero (“continue”) flag.
candidate value and zero ("continue") flag.
.. code:: ipython2
.. code:: ipython3
define('prep-row == sort reverse 0 tuck')
define('prep-row sort reverse 0 tuck')
Now we can define our program.
.. code:: ipython2
.. code:: ipython3
define('AoC20017.2.extra == [prep-row find-result +] step_zero')
define('AoC20017.2.extra [prep-row find-result +] step_zero')
.. code:: ipython2
.. code:: ipython3
J('''

View File

@ -13194,7 +13194,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="mi">4</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="mi">4</span>
</pre></div>
</div>
@ -13215,8 +13215,8 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span> <span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">k</span><span class="p">),</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="n">k</span><span class="p">),)</span>
</pre></div>
</div>
@ -13233,7 +13233,14 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre>4 3 2 1 0 1 2 3
<pre>4
3
2
1
0
1
2
3
</pre>
</div>
</div>
@ -13255,8 +13262,8 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span> <span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)),</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)),</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13273,8 +13280,7 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre>3 2 1 0 1 2 3 4
</pre>
<pre>3 2 1 0 1 2 3 4 </pre>
</div>
</div>
@ -13295,8 +13301,8 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span> <span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="n">k</span><span class="p">,</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="n">k</span><span class="p">,</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13313,8 +13319,7 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre>7 6 5 4 5 6 7 8
</pre>
<pre>7 6 5 4 5 6 7 8 </pre>
</div>
</div>
@ -13335,7 +13340,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="n">i</span> <span class="o">%=</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">)</span> <span class="c1"># wrap the index at the row boundary.</span>
<span class="k">return</span> <span class="nb">abs</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="n">k</span>
</pre></div>
@ -13350,9 +13355,9 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="mi">5</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="mi">5</span>
<span class="k">for</span> <span class="n">i</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">),</span>
<span class="nb">print</span><span class="p">(</span><span class="n">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">),</span> <span class="n">end</span><span class="o">=</span><span class="s1">&#39; &#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13369,8 +13374,7 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre>9 8 7 6 5 6 7 8 9 10
</pre>
<pre>9 8 7 6 5 6 7 8 9 10 </pre>
</div>
</div>
@ -13402,7 +13406,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">rank_and_offset</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">rank_and_offset</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="k">assert</span> <span class="n">n</span> <span class="o">&gt;=</span> <span class="mi">2</span> <span class="c1"># Guard the domain.</span>
<span class="n">n</span> <span class="o">-=</span> <span class="mi">2</span> <span class="c1"># Subtract two,</span>
<span class="c1"># one for the initial square,</span>
@ -13426,8 +13430,8 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">51</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">n</span><span class="p">,</span> <span class="n">rank_and_offset</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">51</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">rank_and_offset</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
</pre></div>
</div>
@ -13506,9 +13510,9 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">51</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="nb">range</span><span class="p">(</span><span class="mi">2</span><span class="p">,</span> <span class="mi">51</span><span class="p">):</span>
<span class="n">k</span><span class="p">,</span> <span class="n">i</span> <span class="o">=</span> <span class="n">rank_and_offset</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<span class="nb">print</span> <span class="n">n</span><span class="p">,</span> <span class="n">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">)</span>
<span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">))</span>
</pre></div>
</div>
@ -13594,7 +13598,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">row_value</span><span class="p">(</span><span class="n">k</span><span class="p">,</span> <span class="n">i</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">abs</span><span class="p">(</span><span class="n">i</span> <span class="o">-</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">+</span> <span class="n">k</span>
@ -13628,7 +13632,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</pre></div>
</div>
@ -13661,7 +13665,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000</span><span class="p">)</span>
</pre></div>
</div>
@ -13694,7 +13698,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">)</span>
</pre></div>
</div>
@ -13735,7 +13739,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">floor</span><span class="p">,</span> <span class="n">lambdify</span><span class="p">,</span> <span class="n">solve</span><span class="p">,</span> <span class="n">symbols</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">floor</span><span class="p">,</span> <span class="n">lambdify</span><span class="p">,</span> <span class="n">solve</span><span class="p">,</span> <span class="n">symbols</span>
<span class="kn">from</span> <span class="nn">sympy</span> <span class="kn">import</span> <span class="n">init_printing</span>
<span class="n">init_printing</span><span class="p">()</span>
</pre></div>
@ -13750,7 +13754,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">k</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;k&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13775,7 +13779,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">E</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1"># For the reason for adding 2 see above.</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">E</span> <span class="o">=</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">8</span> <span class="o">*</span> <span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="c1"># For the reason for adding 2 see above.</span>
<span class="n">E</span>
</pre></div>
@ -13796,7 +13800,7 @@ div#notebook {
<div class="output_latex output_subarea output_execute_result">
$$4 k \left(k + 1\right) + 2$$
$\displaystyle 4 k \left(k + 1\right) + 2$
</div>
</div>
@ -13818,7 +13822,7 @@ $$4 k \left(k + 1\right) + 2$$
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="k">return</span> <span class="n">floor</span><span class="p">(</span><span class="nb">max</span><span class="p">(</span><span class="n">solve</span><span class="p">(</span><span class="n">E</span> <span class="o">-</span> <span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">)))</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
@ -13841,8 +13845,8 @@ $$4 k \left(k + 1\right) + 2$$
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">26</span><span class="p">,</span> <span class="mi">49</span><span class="p">,</span> <span class="mi">50</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">n</span><span class="p">,</span> <span class="n">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">26</span><span class="p">,</span> <span class="mi">49</span><span class="p">,</span> <span class="mi">50</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
</pre></div>
</div>
@ -13886,7 +13890,7 @@ $$4 k \left(k + 1\right) + 2$$
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">time</span> rank_of(23000000000000) # Compare runtime with rank_and_offset()!
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">time</span> rank_of(23000000000000) # Compare runtime with rank_and_offset()!
</pre></div>
</div>
@ -13903,8 +13907,8 @@ $$4 k \left(k + 1\right) + 2$$
<div class="output_subarea output_stream output_stdout output_text">
<pre>CPU times: user 68 ms, sys: 8 ms, total: 76 ms
Wall time: 73.8 ms
<pre>CPU times: user 27.8 ms, sys: 5 µs, total: 27.8 ms
Wall time: 27.3 ms
</pre>
</div>
</div>
@ -13917,7 +13921,7 @@ Wall time: 73.8 ms
<div class="output_latex output_subarea output_execute_result">
$$2397916$$
$\displaystyle 2397916$
</div>
</div>
@ -13931,7 +13935,7 @@ $$2397916$$
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">time</span> rank_and_offset(23000000000000)
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">time</span> rank_and_offset(23000000000000)
</pre></div>
</div>
@ -13948,8 +13952,8 @@ $$2397916$$
<div class="output_subarea output_stream output_stdout output_text">
<pre>CPU times: user 308 ms, sys: 0 ns, total: 308 ms
Wall time: 306 ms
<pre>CPU times: user 216 ms, sys: 89 µs, total: 216 ms
Wall time: 215 ms
</pre>
</div>
</div>
@ -13962,7 +13966,7 @@ Wall time: 306 ms
<div class="output_latex output_subarea output_execute_result">
$$\left ( 2397916, \quad 223606\right )$$
$\displaystyle \left( 2397916, \ 223606\right)$
</div>
</div>
@ -13993,7 +13997,7 @@ $$\left ( 2397916, \quad 223606\right )$$
<div class="prompt input_prompt">In&nbsp;[21]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">y</span> <span class="o">=</span> <span class="n">symbols</span><span class="p">(</span><span class="s1">&#39;y&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14006,7 +14010,7 @@ $$\left ( 2397916, \quad 223606\right )$$
<div class="prompt input_prompt">In&nbsp;[22]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">g</span><span class="p">,</span> <span class="n">f</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">E</span> <span class="o">-</span> <span class="n">y</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">g</span><span class="p">,</span> <span class="n">f</span> <span class="o">=</span> <span class="n">solve</span><span class="p">(</span><span class="n">E</span> <span class="o">-</span> <span class="n">y</span><span class="p">,</span> <span class="n">k</span><span class="p">)</span>
</pre></div>
</div>
@ -14027,7 +14031,7 @@ $$\left ( 2397916, \quad 223606\right )$$
<div class="prompt input_prompt">In&nbsp;[23]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">g</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">g</span>
</pre></div>
</div>
@ -14046,7 +14050,7 @@ $$\left ( 2397916, \quad 223606\right )$$
<div class="output_latex output_subarea output_execute_result">
$$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
$\displaystyle - \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
</div>
</div>
@ -14060,7 +14064,7 @@ $$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
<div class="prompt input_prompt">In&nbsp;[24]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">f</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">f</span>
</pre></div>
</div>
@ -14079,7 +14083,7 @@ $$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
<div class="output_latex output_subarea output_execute_result">
$$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
$\displaystyle \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
</div>
</div>
@ -14101,7 +14105,7 @@ $$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
<div class="prompt input_prompt">In&nbsp;[25]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">floor</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">floor</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span>
</pre></div>
</div>
@ -14120,7 +14124,7 @@ $$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
<div class="output_latex output_subarea output_execute_result">
$$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
$\displaystyle \left\lfloor{\frac{\sqrt{y - 1}}{2} - \frac{1}{2}}\right\rfloor + 1$
</div>
</div>
@ -14134,7 +14138,7 @@ $$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
<div class="prompt input_prompt">In&nbsp;[26]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">F</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">floor</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">F</span> <span class="o">=</span> <span class="n">lambdify</span><span class="p">(</span><span class="n">y</span><span class="p">,</span> <span class="n">floor</span><span class="p">(</span><span class="n">f</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
@ -14147,8 +14151,8 @@ $$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
<div class="prompt input_prompt">In&nbsp;[27]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">26</span><span class="p">,</span> <span class="mi">49</span><span class="p">,</span> <span class="mi">50</span><span class="p">):</span>
<span class="nb">print</span> <span class="n">n</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">F</span><span class="p">(</span><span class="n">n</span><span class="p">))</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">for</span> <span class="n">n</span> <span class="ow">in</span> <span class="p">(</span><span class="mi">9</span><span class="p">,</span> <span class="mi">10</span><span class="p">,</span> <span class="mi">25</span><span class="p">,</span> <span class="mi">26</span><span class="p">,</span> <span class="mi">49</span><span class="p">,</span> <span class="mi">50</span><span class="p">):</span>
<span class="nb">print</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="nb">int</span><span class="p">(</span><span class="n">F</span><span class="p">(</span><span class="n">n</span><span class="p">)))</span>
</pre></div>
</div>
@ -14192,7 +14196,7 @@ $$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
<div class="prompt input_prompt">In&nbsp;[28]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">time</span> int(F(23000000000000)) # The clear winner.
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">time</span> int(F(23000000000000)) # The clear winner.
</pre></div>
</div>
@ -14209,8 +14213,8 @@ $$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
<div class="output_subarea output_stream output_stdout output_text">
<pre>CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.9 µs
<pre>CPU times: user 60 µs, sys: 4 µs, total: 64 µs
Wall time: 67 µs
</pre>
</div>
</div>
@ -14223,7 +14227,7 @@ Wall time: 11.9 µs
<div class="output_latex output_subarea output_execute_result">
$$2397916$$
$\displaystyle 2397916$
</div>
</div>
@ -14245,10 +14249,10 @@ $$2397916$$
<div class="prompt input_prompt">In&nbsp;[29]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">floor</span> <span class="k">as</span> <span class="n">mfloor</span><span class="p">,</span> <span class="n">sqrt</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">floor</span> <span class="k">as</span> <span class="n">mfloor</span><span class="p">,</span> <span class="n">sqrt</span>
<span class="k">def</span> <span class="nf">mrank_of</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">mfloor</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="mi">23000000000000</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">mfloor</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
</pre></div>
</div>
@ -14261,7 +14265,7 @@ $$2397916$$
<div class="prompt input_prompt">In&nbsp;[30]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">time</span> mrank_of(23000000000000)
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">time</span> mrank_of(23000000000000)
</pre></div>
</div>
@ -14278,8 +14282,8 @@ $$2397916$$
<div class="output_subarea output_stream output_stdout output_text">
<pre>CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 12.9 µs
<pre>CPU times: user 7 µs, sys: 1 µs, total: 8 µs
Wall time: 10 µs
</pre>
</div>
</div>
@ -14292,7 +14296,7 @@ Wall time: 12.9 µs
<div class="output_latex output_subarea output_execute_result">
$$2397916$$
$\displaystyle 2397916$
</div>
</div>
@ -14314,7 +14318,7 @@ $$2397916$$
<div class="prompt input_prompt">In&nbsp;[31]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">offset_of</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">offset_of</span><span class="p">(</span><span class="n">n</span><span class="p">,</span> <span class="n">k</span><span class="p">):</span>
<span class="k">return</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">2</span> <span class="o">+</span> <span class="mi">4</span> <span class="o">*</span> <span class="n">k</span> <span class="o">*</span> <span class="p">(</span><span class="n">k</span> <span class="o">-</span> <span class="mi">1</span><span class="p">))</span> <span class="o">%</span> <span class="p">(</span><span class="mi">2</span> <span class="o">*</span> <span class="n">k</span><span class="p">)</span>
</pre></div>
@ -14336,7 +14340,7 @@ $$2397916$$
<div class="prompt input_prompt">In&nbsp;[32]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">offset_of</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">,</span> <span class="mi">2397916</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">offset_of</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">,</span> <span class="mi">2397916</span><span class="p">)</span>
</pre></div>
</div>
@ -14355,7 +14359,7 @@ $$2397916$$
<div class="output_latex output_subarea output_execute_result">
$$223606$$
$\displaystyle 223606$
</div>
</div>
@ -14377,7 +14381,7 @@ $$223606$$
<div class="prompt input_prompt">In&nbsp;[33]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="k">def</span> <span class="nf">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="k">def</span> <span class="nf">rank_of</span><span class="p">(</span><span class="n">n</span><span class="p">):</span>
<span class="k">return</span> <span class="nb">int</span><span class="p">(</span><span class="n">mfloor</span><span class="p">(</span><span class="n">sqrt</span><span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="o">/</span> <span class="mi">2</span> <span class="o">-</span> <span class="mf">0.5</span><span class="p">)</span> <span class="o">+</span> <span class="mi">1</span><span class="p">)</span>
@ -14405,7 +14409,7 @@ $$223606$$
<div class="prompt input_prompt">In&nbsp;[34]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</pre></div>
</div>
@ -14424,7 +14428,7 @@ $$223606$$
<div class="output_latex output_subarea output_execute_result">
$$2$$
$\displaystyle 2$
</div>
</div>
@ -14438,7 +14442,7 @@ $$2$$
<div class="prompt input_prompt">In&nbsp;[35]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000</span><span class="p">)</span>
</pre></div>
</div>
@ -14457,7 +14461,7 @@ $$2$$
<div class="output_latex output_subarea output_execute_result">
$$105$$
$\displaystyle 105$
</div>
</div>
@ -14471,7 +14475,7 @@ $$105$$
<div class="prompt input_prompt">In&nbsp;[36]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">aoc20173</span><span class="p">(</span><span class="mi">23000000000000</span><span class="p">)</span>
</pre></div>
</div>
@ -14490,7 +14494,7 @@ $$105$$
<div class="output_latex output_subarea output_execute_result">
$$4572225$$
$\displaystyle 4572225$
</div>
</div>
@ -14504,7 +14508,7 @@ $$4572225$$
<div class="prompt input_prompt">In&nbsp;[37]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="o">%</span><span class="k">time</span> aoc20173(23000000000000000000000000) # Fast for large values.
<div class=" highlight hl-ipython3"><pre><span></span><span class="o">%</span><span class="k">time</span> aoc20173(23000000000000000000000000) # Fast for large values.
</pre></div>
</div>
@ -14521,8 +14525,8 @@ $$4572225$$
<div class="output_subarea output_stream output_stdout output_text">
<pre>CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 20 µs
<pre>CPU times: user 22 µs, sys: 2 µs, total: 24 µs
Wall time: 26.7 µs
</pre>
</div>
</div>
@ -14535,7 +14539,7 @@ Wall time: 20 µs
<div class="output_latex output_subarea output_execute_result">
$$2690062495969$$
$\displaystyle 2690062495969$
</div>
</div>
@ -14557,7 +14561,7 @@ $$2690062495969$$
<div class="prompt input_prompt">In&nbsp;[38]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -14588,7 +14592,7 @@ rank_of == -- sqrt 2 / 0.5 - floor ++</code></pre>
<div class="prompt input_prompt">In&nbsp;[39]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;rank_of == -- sqrt 2 / 0.5 - floor ++&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;rank_of -- sqrt 2 / 0.5 - floor ++&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14637,7 +14641,7 @@ n-k*k-1*4+2%k*2
<div class="prompt input_prompt">In&nbsp;[40]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;offset_of dup 2 * [dup -- 4 * * 2 + -] dip %&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14670,7 +14674,7 @@ k+|i-k-1|</code></pre>
<div class="prompt input_prompt">In&nbsp;[41]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;row_value == over -- - abs +&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;row_value over -- - abs +&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14701,7 +14705,7 @@ m</code></pre>
<div class="prompt input_prompt">In&nbsp;[42]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;aoc2017.3 dup rank_of [offset_of] dupdip swap row_value&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14714,7 +14718,7 @@ m</code></pre>
<div class="prompt input_prompt">In&nbsp;[43]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 aoc2017.3&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 aoc2017.3&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14745,7 +14749,7 @@ m</code></pre>
<div class="prompt input_prompt">In&nbsp;[44]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23000 aoc2017.3&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23000 aoc2017.3&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14776,7 +14780,7 @@ m</code></pre>
<div class="prompt input_prompt">In&nbsp;[45]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;23000000000000 aoc2017.3&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">V</span><span class="p">(</span><span class="s1">&#39;23000000000000 aoc2017.3&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -14793,45 +14797,45 @@ m</code></pre>
<div class="output_subarea output_stream output_stdout output_text">
<pre> . 23000000000000 aoc2017.3
23000000000000 . aoc2017.3
23000000000000 . dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 . ++ [offset_of] dupdip swap row_value
23000000000000 2397916 . [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] . dupdip swap row_value
23000000000000 2397916 . offset_of 2397916 swap row_value
23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value
23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value
5019438 . 4795832 % 2397916 swap row_value
5019438 4795832 . % 2397916 swap row_value
223606 . 2397916 swap row_value
223606 2397916 . swap row_value
2397916 223606 . row_value
2397916 223606 . over -- - abs +
2397916 223606 2397916 . -- - abs +
2397916 223606 2397915 . - abs +
2397916 -2174309 . abs +
2397916 2174309 . +
4572225 .
<pre> 23000000000000 aoc2017.3
23000000000000 aoc2017.3
23000000000000 dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 ++ [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 offset_of 2397916 swap row_value
23000000000000 2397916 dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 - 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
223606 2397916 swap row_value
223606 2397916 swap row_value
2397916 223606 row_value
2397916 223606 over -- - abs +
2397916 223606 2397916 -- - abs +
2397916 223606 2397915 - abs +
2397916 -2174309 abs +
2397916 2174309 +
4572225
</pre>
</div>
</div>

View File

@ -147,13 +147,20 @@
"name": "stdout",
"output_type": "stream",
"text": [
"4 3 2 1 0 1 2 3\n"
"4\n",
"3\n",
"2\n",
"1\n",
"0\n",
"1\n",
"2\n",
"3\n"
]
}
],
"source": [
"for n in range(2 * k):\n",
" print abs(n - k),"
" print(abs(n - k),)"
]
},
{
@ -172,13 +179,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"3 2 1 0 1 2 3 4\n"
"3 2 1 0 1 2 3 4 "
]
}
],
"source": [
"for n in range(2 * k):\n",
" print abs(n - (k - 1)),"
" print(abs(n - (k - 1)), end=' ')"
]
},
{
@ -197,13 +204,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"7 6 5 4 5 6 7 8\n"
"7 6 5 4 5 6 7 8 "
]
}
],
"source": [
"for n in range(2 * k):\n",
" print abs(n - (k - 1)) + k,"
" print(abs(n - (k - 1)) + k, end=' ')"
]
},
{
@ -233,14 +240,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"9 8 7 6 5 6 7 8 9 10\n"
"9 8 7 6 5 6 7 8 9 10 "
]
}
],
"source": [
"k = 5\n",
"for i in range(2 * k):\n",
" print row_value(k, i),"
" print(row_value(k, i), end=' ')"
]
},
{
@ -349,7 +356,7 @@
],
"source": [
"for n in range(2, 51):\n",
" print n, rank_and_offset(n)"
" print(n, rank_and_offset(n))"
]
},
{
@ -418,7 +425,7 @@
"source": [
"for n in range(2, 51):\n",
" k, i = rank_and_offset(n)\n",
" print n, row_value(k, i)"
" print(n, row_value(k, i))"
]
},
{
@ -575,9 +582,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAHwAAAAUBAMAAAC9l0S6AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBU\nq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACAElEQVQ4EW1UzWsTQRx9NB+bzSZt6D/gHvRS\nKOZQWhAK6UHrsRShhyIW2rvBiznp0ltPxdJLDi2h9aIXc7C0emlA6k38wKKXYsA/IFZEhITG95vZ\nj5klD3bmvff7vczsMBvARsOWiSrUE26zSeD1uracGrw7Vbscqm9p99H1BWXNcNzRRZdruL7mxvi+\nDbwxtFDvE151Oec2OAz4EJt8JjrCBGd68r78YLy4pFXkltvIr9Fq7ALjfV2b49Rjr0YYBx7Q8tZs\nN19F+ZJWi/FiVdUcmQ7Dtnh1Hcd+6Ic/6vZVvFBn3PVL9yt8D58tOzj7rDut1XFgx6ky3PMRGD/t\nrPQCIMvHG+Se/IOCHd/SZvTuVL0W9y7xj0ftE+pMByj1V72AnLDj77SZuJhnd0XiN31Vy3eBsd+6\n7UWzOdts7ikhR4drQk13zAfeQuLz95akKPHM+W2hAnt1FTfdZYoPFxd/z70r50r6ZfOn3add4YQd\n/6nN2C35eCzWBgq/vEGRLBsA2zzHFhTseProeNdXpO0PyjVcSrzoA18xUWmRE3Z8SpuR69x6OS3X\n5PnwezbA8irpOPVd5G7ISRFRfPrhXgA8U17susPhUOIm5NImiOLKUTdSmOUmzcLkk0lwnFDjk7Fc\ns4NnV7e1odIfrFGKqVOLaZospo1RujHKFG/0n9V/fdOG1o/ONcUAAAAASUVORK5CYII=\n",
"text/latex": [
"$$4 k \\left(k + 1\\right) + 2$$"
"$\\displaystyle 4 k \\left(k + 1\\right) + 2$"
],
"text/plain": [
"4⋅k⋅(k + 1) + 2"
@ -640,7 +646,7 @@
],
"source": [
"for n in (9, 10, 25, 26, 49, 50):\n",
" print n, rank_of(n)"
" print(n, rank_of(n))"
]
},
{
@ -659,15 +665,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 68 ms, sys: 8 ms, total: 76 ms\n",
"Wall time: 73.8 ms\n"
"CPU times: user 27.8 ms, sys: 5 µs, total: 27.8 ms\n",
"Wall time: 27.3 ms\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n",
"text/latex": [
"$$2397916$$"
"$\\displaystyle 2397916$"
],
"text/plain": [
"2397916"
@ -691,15 +696,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 308 ms, sys: 0 ns, total: 308 ms\n",
"Wall time: 306 ms\n"
"CPU times: user 216 ms, sys: 89 µs, total: 216 ms\n",
"Wall time: 215 ms\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAALAAAAAUBAMAAADfFKqVAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIma7zZnddlTvRIky\nEKtZsEGBAAAACXBIWXMAAA7EAAAOxAGVKw4bAAADLElEQVQ4Ea2VT2hcVRjFf28yfzNzZ6ZIC9LN\nQwpSXDQahCpiYzdCqWagnYUGw4h/EAo1QkMXin0b3biYqYqkwcJsrLiQBHQMMmJm46qLeUJUumlS\nQaG0UhvHWGrT6bn3PYszuBih3+Lcf+ee993zvncfeD73PBJlSd4HO488Abvb++GH9iUKx2fabczM\nt3bVRfvNTty723jVyQ4RtL8IIL/yjbY8ZHmWfUntU5gNVptehbpv3uf+cqLf79/mgu+dhuxZMdaa\nqSk1A3GR/E0c7CS7iTnEUZKBeTFmr0GiQjEk18jcoFQrNihWUsp0g8vwCxdntyX3KGMTA6oa/AZn\nIjgVsMVYhUn2wpcxOz9HukyuRvHW+CLrU6Up8r/ndbAOPViFjITHrw+ravwRXOs4+KxpeixZ256E\nehixTYNdkNyWsBbq/voEGdv7FPMHnOo44bGGZoZjOpSwA/m1yWN2/TYsBTH7EPNuS1qZmWPklPFN\nTejkHyrjphMuHV553pGG4PVQExZ+XKZ3fr5p/pTwXMxu8YijLy3jffWCfCC5KSd858O074TXD5IL\nHGsAxv/W0MLuV0LT81nw1O8ux+w9vObYxy1+3eRp3rsB6xok57KrQSS8TcoWx1CkNzThIL9o+iHP\nfa+MJRyx32XR8lMVi7mzJE6el8cn7OjBt1Yjj0s1EtafoajasQPOhFuw73MJy4qI3Y2E58ErM6Zk\nKcrtlyKNa6HLOD1FQv4MRcbmYmEHHAhelrCvl1cPYnbXWZGtsKO0HQknNzBi2HhWO/UYFfd/ZPyO\nTcWCXDgQqOr3NScFYcw+517ed/BGboL09cRpumUKOhN7ywXVrxUuyLWGe9K/oFAhU3bwgUqj3JXH\n9gM5+g97Dy25cKw9W0v41Ocyz5iDqksrfCJc8yNhrnAh0EcwELvarV9x8ADZvygumwXSgfkYx4YZ\nzqkAdDnU+KT6OLSqTT1oQRr5qoo3dbn3s+1ehVfDAeHpfn8LB+PVI9rTmu1gVt7uxGwO2096pMiO\nyIvEPNVGZSRddIP8j9AlxMOj8X8ajRaz1tTqoh8l/FFIdzk2W88X3OPQr+kOAx0PXfbg7EwAAAAA\nSUVORK5CYII=\n",
"text/latex": [
"$$\\left ( 2397916, \\quad 223606\\right )$$"
"$\\displaystyle \\left( 2397916, \\ 223606\\right)$"
],
"text/plain": [
"(2397916, 223606)"
@ -763,9 +767,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAIkAAAAqBAMAAAB1gmW6AAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEM3dMlTvq5l2ZiK7\niUTiBfEGAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB6klEQVRIDe2Uv0vDQBTHv9eiTU2pRQdxsqLo\n4CI6CEKxo5tObqKOThYHEUEUlyKIOklFwf4J4iQ42EEQt86C6Ca62IogqKh3lzQ/nkm8BEdvuL73\nve/ny/WlDfDXayoTJdFNseW1CCk/qaMIKQClaK/2/ShF+/8UYwLuuex8eS86LTdFnpleoXafPjAl\ncedDUTkw5YS6/XqSUlzoL9vWbbsMrNwUsbIsESK12qQX1jTnpfpr7V5HHcW6l+yvXXseJcOlsMG/\nSEmWgc5N6GQOIe8S51epTiFZc19JPYUJcB/QJzeQqiimtA2L1QvI/14Nek6AfCwM82jd5bXlAIy7\nsB6BDOWF0aRE6VyPfbxrqfIt/Y6JvPOokeLWvLuZMhDP8DMtiz1iUZ9L8yAwLehUAZeRU5KfQLeg\ntUr6LXIK+0DTuqDZaundnaItPa+4lZ/d6daFFHeOY8fGKZ/Mr6tBmUZWwO2dqM+r91JaRJfsZeO3\nWZRpSGTQPCvqVG1ASqO4kp+Bm0WZLv5sEi+iTr8WpPRQysvPwM2iTFesbqZgTIFuRNtUQ0HceH0c\nWoJSYVKW96lqlSEKSo2EYG0robR1+0i9olRJHXU4CcV/92eOU8WSUuPAgSLqsBFKz90U+Ush5KJU\njL/8wqcY1Dcp15UsZrvPlwAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$- \\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}$$"
"$\\displaystyle - \\frac{\\sqrt{y - 1}}{2} - \\frac{1}{2}$"
],
"text/plain": [
" _______ \n",
@ -790,9 +793,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAHgAAAAqBAMAAACKDIIdAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWYiuzKJ\nRN0MreaOAAAACXBIWXMAAA7EAAAOxAGVKw4bAAAB9ElEQVRIDe2Vu0vDUBTGvzRNSrSNILgX3TVD\nwU2LD3DRVqgIOthF0akdXETQbi4OVXDQoTg46aCLexedCvY/aEHBRVQQfOCj3nOrmCa3ubGuHmjP\nyXe+3z1tb24K/C06rVZ4pYdR6miiFTiWeuAjM63A0P/h322Y8AfTauK4d6wthPccpmaXQni8mduh\ni2Aj6zA1u6zDqb7l3I9DK//UXpU29DTm6ve7lF8Ik0Kvv4OnVgWw34MXzglgwN/BGxCyPuFF9nSY\nT2PbalzE3+QqEFaiyDSy8sk0TE8DW1oWhz5hfgzugUKRASZ7WYE0HllW125ZXJ+w8mtyPwm3qyR0\ncIwqishGlL1vUlnJt79StoXsOyvPzHxJwDT0KGVbyGD0AkqRgAO0xSnbQgpnygjxHZpBJWcDqZTC\ng/vo4kzsfC7fCDcePHtvZ+aIX2ovWPnSp+19cW1MHbJ9UOO4KJPBeIvEKQez6jtl74ghzFxBC4Eo\nNx6H+Kc1c21pb5C6w0A3EKgiWN/WyghnIguzcpb2JnGC0MM3bDpvDa811i2CWZj1Pzx938vt7t1Z\npFWK7o5cibxxz5XcKXCYcRK1pKAll6a4ZV5uFDh0PlJJYlfQlEldMPLAKbAkc7r77UnoeRg3pVTV\n3ZQphdLZBBBiD5QW4PVa7QOfZ0qXehvwRMIAAAAASUVORK5CYII=\n",
"text/latex": [
"$$\\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}$$"
"$\\displaystyle \\frac{\\sqrt{y - 1}}{2} - \\frac{1}{2}$"
],
"text/plain": [
" _______ \n",
@ -824,9 +826,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAK0AAAAqBAMAAAAzNMYQAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAEGYiq+/dVJl2zbsy\niUT7WiApAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACUUlEQVRIDe2Uv2/TQBTHv1YgsdMmRCUwdCEz\nElIHCANLqAAx0TLASpEgAwxMSGXrUImqVCJigimRqBgipHZg60D+BEZAQup/EH5ISCBKuPfsc8/2\nw2fHYuOk5L177/v9+Hw+G/ino3J9GrzV1Vz/MgU3g8ubhgu7y66Q7sfusiv+c6Ud8Gv23Ysq3Ik8\nPscuEXXFmjyNKi5IEqEWdQmC2Em8JkmEWhp3jvURRWkgMKRS4PKk3lkqupd/Lh423f3DPC3Trqok\nYm60MR+dWmdZuY9F0sOGWFbFBJekyfU6KwLAubpk5T7yfb40ya21BC7Qz8r1pUnuSRFbnPsMcJ73\ncC62vsLrXQFqlWX0Y8suwqUlej3gvDvAXnHuMf7MAO13ilVWv8bRHn6o6LzqqnFvpNJgf+ep0H1N\nBe3yz9nH4fDucLhNDZYaz61+elkVz1BnvDD7i6IxCuxD5avi3CbWJjy6hjkKcHETqNBe4A1mOiZU\n5UW4/X1UG8R7gnGLojGKcE/tYpVRzQ9PFwymStdvbbeilXCmvw/Be+xL+bmVNvZGJHO/4WUg3wxi\nStCuOJctzG2i9ptmpYN6h+KRgcNzyv8+tEtzj5tS5l4EbnDxbZVvv9ya6ZkiOdcuzY2omKvOwdKI\nyuNL3Ky/2OKY/qddIvdE9w6w0wi45fjbkEb2Xe3u/TTRAz5f3m6aJtnzXcl6WKkfhGmOxO4qd3Lg\nQqndtRFq8yRWl7eWB6e1dtcqSnxwtSNbtLpm1+Dl59pd7U/vr2Rboqmyu3Ymk++mI1ue5voDRu6s\n5n/E50UAAAAASUVORK5CYII=\n",
"text/latex": [
"$$\\lfloor{\\frac{1}{2} \\sqrt{y - 1} - \\frac{1}{2}}\\rfloor + 1$$"
"$\\displaystyle \\left\\lfloor{\\frac{\\sqrt{y - 1}}{2} - \\frac{1}{2}}\\right\\rfloor + 1$"
],
"text/plain": [
"⎢ _______ ⎥ \n",
@ -873,7 +874,7 @@
],
"source": [
"for n in (9, 10, 25, 26, 49, 50):\n",
" print n, int(F(n))"
" print(n, int(F(n)))"
]
},
{
@ -894,15 +895,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n",
"Wall time: 11.9 µs\n"
"CPU times: user 60 µs, sys: 4 µs, total: 64 µs\n",
"Wall time: 67 µs\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n",
"text/latex": [
"$$2397916$$"
"$\\displaystyle 2397916$"
],
"text/plain": [
"2397916"
@ -933,7 +933,7 @@
"from math import floor as mfloor, sqrt\n",
"\n",
"def mrank_of(n):\n",
" return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1)"
" return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)"
]
},
{
@ -947,15 +947,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n",
"Wall time: 12.9 µs\n"
"CPU times: user 7 µs, sys: 1 µs, total: 8 µs\n",
"Wall time: 10 µs\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEcAAAAPBAMAAABElc8tAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABkUlEQVQoFY2QPUscURSGn4l7d4y7q6ONxGqZ\ngEbSCFZWTmfpYCHEFK6CRdIoJmQaYacNBBJJ4wcqKNhYaKNYBFeCnYKLjdhNK4j5NrKbOHlnxx+Q\nAzP33nOf+77nHCy338MKXlUxPW8cMps9QcBIMMF92HOvYRT7lg4KdzyLrHla4zi+Mas8cuDCB7PP\nU5iCRU6r1HgBkzzQZSn7gWzRTE4LairSD0sw7b0NTZ06lLHB9tp2sL/CqaD3egQVXxCyMz+U1o53\njPeR/5lCA0o0YlsvxmZYllKoRB8PpXSbQvWhz0mO5t/Que7Li0oktyjxyt01IFOPWBFDS0k/e4Hc\nYaFchXGdPnH+N4Vin14Z4epTiz5XR2UPjnVoPRm6r6kGX0LIF6EdBiVC0vSGVsj+SmvaEhTBGZYj\n0Qa0p+ndNKBcKYU0PClliuSdj7DtXDqZb5D5Lrd5hp0UGlZN0BXMvuSawh+O/ecSLgjK75oD6SXD\nzM4YdVeJ4xrN7uMQ232iGyvpeNYNoXttL9K221PiP+If4oN7f8ioQFsAAAAASUVORK5CYII=\n",
"text/latex": [
"$$2397916$$"
"$\\displaystyle 2397916$"
],
"text/plain": [
"2397916"
@ -1003,9 +1002,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAADsAAAAOBAMAAABjvHmeAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABIElEQVQYGTWQoU8DMRSHvwuULWNLDgwJ6jIS\nCJlcQoLaOeSKQaAwCDAsg2WCiTMI1AQIRjIBFoHFjSCwNPwDmyMYSBBAdoTjtR2iX76+X/r6WoJy\nNcajfWgg1zlCrbzE3tgi9+0xT+kXdUeFWaOuvLELPY8nw5ipiCqvcOyNSziIHU4TldINgTUYamcM\ntMQO2ObrkvIJXePM7m71BNsN0o2HRH1IfG/NpvmvCRautUpH9AMp1FvWbFzY+UfuQmWa1U05XW9Z\ns23Lsjzo6TG8n7jm1hIoRpJazEHN3EhxJKMNvcEzQegg3Wpmz56pCrQzpiOKocOZvCGsy432Wyo4\nY7Hd3Pd4o/TDTEP1KRh17o1Blo098uUlGaW5HKM6j7G3P7xTb/ft54xWAAAAAElFTkSuQmCC\n",
"text/latex": [
"$$223606$$"
"$\\displaystyle 223606$"
],
"text/plain": [
"223606"
@ -1060,9 +1058,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAAkAAAAOBAMAAAAPuiubAAAALVBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADAOrOgAAAADnRSTlMAIpm7MhCriUTv3c12VLge\nopIAAAAJcEhZcwAADsQAAA7EAZUrDhsAAABOSURBVAgdY2BUMnZgYAhjYH/BwJDKwDCTgWEWA0Oe\nA8O+ABAJBOsCgATHcxCTKwFEKoEIHgUQeYmBUYCBRYGBR4BBqrwoi4Fh37t3rxgAK5QOlzv7snYA\nAAAASUVORK5CYII=\n",
"text/latex": [
"$$2$$"
"$\\displaystyle 2$"
],
"text/plain": [
"2"
@ -1086,9 +1083,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAB0AAAAPBAMAAADqo9msAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAVO8Qq5l2zWaJMt0i\nu0SCRuA9AAAACXBIWXMAAA7EAAAOxAGVKw4bAAAArElEQVQIHWNgAAHmyM4FDOzJXIFANqMyAwO7\nAPMeBqb//ycwMJiEfGZgaGJgmM7APiUHpJYNyL/CwCBvwALiQfhfGBjeCyD4zF+B/ASWjtQFEHme\nnwwM6yfwGvD8g/KB8uuBZjPchvAh6oHs+ANw8+QF3BkY5j+A8O8yMPQbqAPlDSB8oHvCGQIYGLZD\n9DNwCzBrMRxl4NBhYGB1+u7BwDwtZQEDT6QrUDkaAADf+TCFzC1FdQAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$105$$"
"$\\displaystyle 105$"
],
"text/plain": [
"105"
@ -1110,9 +1106,8 @@
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAEgAAAAPBAMAAAC1npSgAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAMpndu3bvImbNiRBU\nq0Qb3U6NAAAACXBIWXMAAA7EAAAOxAGVKw4bAAABGklEQVQoFYWQsUrDUBRAT6I1pVYJ/YLYURcH\nV9HBzS9wtjgJPnRx00XnDLq4tIhTQOjiIojiLghSKEjRxR9QUajU5715hfc2Qzi83HMgjwvQgNNu\n4y5ani8KkuZaGqA00rAEO3ZI1Vo74obab4DSSFNpwVnPEBt45Bm2ApRGov0TlVCTN2UTXlKP0ojs\njCM5vkG7K5HHOKoaifpHc9KwqmClG8CZKyRa5+BV/nYoltlhCGc6GsHkItzqgQm9oIeaeuqi+Bs2\nyqip9EDMNRLN5LodXZhsJAvhzMNg8NWbyol/mB5pdE9iPJyRcYtYLpETvctHlFExHs7I/JMk49hQ\n12ivOH8K4Axc2D67lwuQbEvUtvYjgDMy//f5A0ICeXTNG40LAAAAAElFTkSuQmCC\n",
"text/latex": [
"$$4572225$$"
"$\\displaystyle 4572225$"
],
"text/plain": [
"4572225"
@ -1136,15 +1131,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"CPU times: user 0 ns, sys: 0 ns, total: 0 ns\n",
"Wall time: 20 µs\n"
"CPU times: user 22 µs, sys: 2 µs, total: 24 µs\n",
"Wall time: 26.7 µs\n"
]
},
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAAIUAAAAPBAMAAAA43xGxAAAAMFBMVEX///8AAAAAAAAAAAAAAAAA\nAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAv3aB7AAAAD3RSTlMAIpm7MhCriUTv3c12\nVGZoascqAAAACXBIWXMAAA7EAAAOxAGVKw4bAAACa0lEQVQoFaXTT0gUYRjH8e/+mZ11d50duhWE\nw9JfvAxJQaeGIDw6eCjMg1uUUUQuInpIci6dXaGCCsvOEe2hfwfTJaKIhLYIOrqeQhLUcl3RdHve\neTfo3h6GnefzPj/e99l3ieQ6PDBHhzAODtvy+C6v3TeGgJH3HpqJO5qZ9k9W6B451+Swh9OYGxiv\naKenGpkgXTEewWvO2vQG0ZJmSLmaedioYzxgd5PDHi7CXWIOHVyG8yzCdawiZokjxFzN8MHVzMur\nNokiCUdz2MM9GPDGbdnbFoxxDOb9WJ7WWnJVapqJ/HA1k5datoS5ojnsYdaXjOMixi/45K3DeCWb\nJ7kdK0pRM2ba1Rxm9Llk1kJuFmXdU3+r803AfdnHzZ+SUe5zSP7OPhs9pFKEWUi7IQdcW9pHi+xj\nQ7PqCWRNsm5sVZmUgzC7UIeuQluBeL1vhpZKyBhlyVBc5ShtgblCekezLsrM80bD57CfLreOfZZ9\nSIajMmpEb0tGKo+JZChWmbEppvm2rflvMQebsByw9Hbs1D9nmcLakB7hrypDsSfv0VWsuc61rGZd\nzDjwWDKq4gO+zHRezbR1O1XC2gFhoxBmKE6oUcjCRK3JqghfiNjyM8s+4IVcE5Z9uRdWTW6B2ofw\n3v7+gTvlkGWc0Zp8S+ebrHrULc7YXTIPFu34qrpj7VhFoqW4zKOoGVpczVGZT8maoMvWHPawZ2Tw\nComCMclHv7dKqmLcgif0eFyip6JZrpWrOeJIVua5MYPmsIfZRkMmOnjAw8zJfTBG33lwZu6C/A9z\n8tBsnlivhsyu4f2yOhc0WRflcP/7+QN1gvGXxRfSRAAAAABJRU5ErkJggg==\n",
"text/latex": [
"$$2690062495969$$"
"$\\displaystyle 2690062495969$"
],
"text/plain": [
"2690062495969"
@ -1199,7 +1193,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('rank_of == -- sqrt 2 / 0.5 - floor ++')"
"define('rank_of -- sqrt 2 / 0.5 - floor ++')"
]
},
{
@ -1242,7 +1236,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')"
"define('offset_of dup 2 * [dup -- 4 * * 2 + -] dip %')"
]
},
{
@ -1271,7 +1265,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('row_value == over -- - abs +')"
"define('row_value over -- - abs +')"
]
},
{
@ -1298,7 +1292,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value')"
"define('aoc2017.3 dup rank_of [offset_of] dupdip swap row_value')"
]
},
{
@ -1344,45 +1338,45 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23000000000000 aoc2017.3\n",
" 23000000000000 . aoc2017.3\n",
" 23000000000000 . dup rank_of [offset_of] dupdip swap row_value\n",
" 23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value\n",
" 23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915 . ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397916 . [offset_of] dupdip swap row_value\n",
" 23000000000000 2397916 [offset_of] . dupdip swap row_value\n",
" 23000000000000 2397916 . offset_of 2397916 swap row_value\n",
" 23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
"23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value\n",
" 23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value\n",
" 5019438 . 4795832 % 2397916 swap row_value\n",
" 5019438 4795832 . % 2397916 swap row_value\n",
" 223606 . 2397916 swap row_value\n",
" 223606 2397916 . swap row_value\n",
" 2397916 223606 . row_value\n",
" 2397916 223606 . over -- - abs +\n",
" 2397916 223606 2397916 . -- - abs +\n",
" 2397916 223606 2397915 . - abs +\n",
" 2397916 -2174309 . abs +\n",
" 2397916 2174309 . +\n",
" 4572225 . \n"
" 23000000000000 aoc2017.3\n",
" 23000000000000 aoc2017.3\n",
" 23000000000000 dup rank_of [offset_of] dupdip swap row_value\n",
" 23000000000000 23000000000000 rank_of [offset_of] dupdip swap row_value\n",
" 23000000000000 23000000000000 -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 22999999999999 sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915.2616563076 floor ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397915 ++ [offset_of] dupdip swap row_value\n",
" 23000000000000 2397916 [offset_of] dupdip swap row_value\n",
" 23000000000000 2397916 [offset_of] dupdip swap row_value\n",
" 23000000000000 2397916 offset_of 2397916 swap row_value\n",
" 23000000000000 2397916 dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
"23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value\n",
" 23000000000000 2397916 dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397916 -- 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 2397916 9591660 * 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value\n",
" 23000000000000 22999994980562 - 4795832 % 2397916 swap row_value\n",
" 5019438 4795832 % 2397916 swap row_value\n",
" 5019438 4795832 % 2397916 swap row_value\n",
" 223606 2397916 swap row_value\n",
" 223606 2397916 swap row_value\n",
" 2397916 223606 row_value\n",
" 2397916 223606 over -- - abs +\n",
" 2397916 223606 2397916 -- - abs +\n",
" 2397916 223606 2397915 - abs +\n",
" 2397916 -2174309 abs +\n",
" 2397916 2174309 +\n",
" 4572225 \n"
]
}
],
@ -1411,14 +1405,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -105,10 +105,17 @@ Subtract $k$ from the index and take the absolute value:
```python
for n in range(2 * k):
print abs(n - k),
print(abs(n - k),)
```
4 3 2 1 0 1 2 3
4
3
2
1
0
1
2
3
Not quite. Subtract $k - 1$ from the index and take the absolute value:
@ -116,23 +123,21 @@ Not quite. Subtract $k - 1$ from the index and take the absolute value:
```python
for n in range(2 * k):
print abs(n - (k - 1)),
print(abs(n - (k - 1)), end=' ')
```
3 2 1 0 1 2 3 4
Great, now add $k$...
```python
for n in range(2 * k):
print abs(n - (k - 1)) + k,
print(abs(n - (k - 1)) + k, end=' ')
```
7 6 5 4 5 6 7 8
So to write a function that can give us the value of a row at a given index:
@ -146,12 +151,11 @@ def row_value(k, i):
```python
k = 5
for i in range(2 * k):
print row_value(k, i),
print(row_value(k, i), end=' ')
```
9 8 7 6 5 6 7 8 9 10
(I'm leaving out details of how I figured this all out and just giving the relevent bits. It took a little while to zero in of the aspects of the pattern that were important for the task.)
### Finding the rank and offset of a number.
@ -182,7 +186,7 @@ def rank_and_offset(n):
```python
for n in range(2, 51):
print n, rank_and_offset(n)
print(n, rank_and_offset(n))
```
2 (1, 0)
@ -240,7 +244,7 @@ for n in range(2, 51):
```python
for n in range(2, 51):
k, i = rank_and_offset(n)
print n, row_value(k, i)
print(n, row_value(k, i))
```
2 1
@ -394,7 +398,7 @@ E
$$4 k \left(k + 1\right) + 2$$
$\displaystyle 4 k \left(k + 1\right) + 2$
@ -413,7 +417,7 @@ It gives correct answers:
```python
for n in (9, 10, 25, 26, 49, 50):
print n, rank_of(n)
print(n, rank_of(n))
```
9 1
@ -431,14 +435,14 @@ And it runs much faster (at least for large numbers):
%time rank_of(23000000000000) # Compare runtime with rank_and_offset()!
```
CPU times: user 68 ms, sys: 8 ms, total: 76 ms
Wall time: 73.8 ms
CPU times: user 27.8 ms, sys: 5 µs, total: 27.8 ms
Wall time: 27.3 ms
$$2397916$$
$\displaystyle 2397916$
@ -447,14 +451,14 @@ $$2397916$$
%time rank_and_offset(23000000000000)
```
CPU times: user 308 ms, sys: 0 ns, total: 308 ms
Wall time: 306 ms
CPU times: user 216 ms, sys: 89 µs, total: 216 ms
Wall time: 215 ms
$$\left ( 2397916, \quad 223606\right )$$
$\displaystyle \left( 2397916, \ 223606\right)$
@ -485,7 +489,7 @@ g
$$- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
$\displaystyle - \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
@ -497,7 +501,7 @@ f
$$\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}$$
$\displaystyle \frac{\sqrt{y - 1}}{2} - \frac{1}{2}$
@ -511,7 +515,7 @@ floor(f) + 1
$$\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1$$
$\displaystyle \left\lfloor{\frac{\sqrt{y - 1}}{2} - \frac{1}{2}}\right\rfloor + 1$
@ -523,7 +527,7 @@ F = lambdify(y, floor(f) + 1)
```python
for n in (9, 10, 25, 26, 49, 50):
print n, int(F(n))
print(n, int(F(n)))
```
9 1
@ -541,14 +545,14 @@ It's pretty fast.
%time int(F(23000000000000)) # The clear winner.
```
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.9 µs
CPU times: user 60 µs, sys: 4 µs, total: 64 µs
Wall time: 67 µs
$$2397916$$
$\displaystyle 2397916$
@ -559,7 +563,7 @@ Knowing the equation we could write our own function manually, but the speed is
from math import floor as mfloor, sqrt
def mrank_of(n):
return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1)
return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
```
@ -567,14 +571,14 @@ def mrank_of(n):
%time mrank_of(23000000000000)
```
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 12.9 µs
CPU times: user 7 µs, sys: 1 µs, total: 8 µs
Wall time: 10 µs
$$2397916$$
$\displaystyle 2397916$
@ -598,7 +602,7 @@ offset_of(23000000000000, 2397916)
$$223606$$
$\displaystyle 223606$
@ -632,7 +636,7 @@ aoc20173(23)
$$2$$
$\displaystyle 2$
@ -644,7 +648,7 @@ aoc20173(23000)
$$105$$
$\displaystyle 105$
@ -656,7 +660,7 @@ aoc20173(23000000000000)
$$4572225$$
$\displaystyle 4572225$
@ -665,14 +669,14 @@ $$4572225$$
%time aoc20173(23000000000000000000000000) # Fast for large values.
```
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 20 µs
CPU times: user 22 µs, sys: 2 µs, total: 24 µs
Wall time: 26.7 µs
$$2690062495969$$
$\displaystyle 2690062495969$
@ -698,7 +702,7 @@ The translation is straightforward.
```python
define('rank_of == -- sqrt 2 / 0.5 - floor ++')
define('rank_of -- sqrt 2 / 0.5 - floor ++')
```
### `offset_of`
@ -732,7 +736,7 @@ Ergo:
```python
define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')
define('offset_of dup 2 * [dup -- 4 * * 2 + -] dip %')
```
### `row_value`
@ -752,7 +756,7 @@ define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')
```python
define('row_value == over -- - abs +')
define('row_value over -- - abs +')
```
### `aoc2017.3`
@ -770,7 +774,7 @@ define('row_value == over -- - abs +')
```python
define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value')
define('aoc2017.3 dup rank_of [offset_of] dupdip swap row_value')
```
@ -794,45 +798,45 @@ J('23000 aoc2017.3')
V('23000000000000 aoc2017.3')
```
. 23000000000000 aoc2017.3
23000000000000 . aoc2017.3
23000000000000 . dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 . ++ [offset_of] dupdip swap row_value
23000000000000 2397916 . [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] . dupdip swap row_value
23000000000000 2397916 . offset_of 2397916 swap row_value
23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value
23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value
5019438 . 4795832 % 2397916 swap row_value
5019438 4795832 . % 2397916 swap row_value
223606 . 2397916 swap row_value
223606 2397916 . swap row_value
2397916 223606 . row_value
2397916 223606 . over -- - abs +
2397916 223606 2397916 . -- - abs +
2397916 223606 2397915 . - abs +
2397916 -2174309 . abs +
2397916 2174309 . +
4572225 .
23000000000000 aoc2017.3
23000000000000 aoc2017.3
23000000000000 dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 ++ [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 offset_of 2397916 swap row_value
23000000000000 2397916 dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 - 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
223606 2397916 swap row_value
223606 2397916 swap row_value
2397916 223606 row_value
2397916 223606 over -- - abs +
2397916 223606 2397916 -- - abs +
2397916 223606 2397915 - abs +
2397916 -2174309 abs +
2397916 2174309 +
4572225
rank_of == -- sqrt 2 / 0.5 - floor ++

View File

@ -27,7 +27,7 @@ Distance between the location of the data and square 1.
For example:
- Data from square 1 is carried 0 steps, since its at the access port.
- Data from square 1 is carried 0 steps, since it's at the access port.
- Data from square 12 is carried 3 steps, such as: down, left, left.
- Data from square 23 is carried only 2 steps: up twice.
- Data from square 1024 must be carried 31 steps.
@ -39,8 +39,8 @@ Analysis
~~~~~~~~
I freely admit that I worked out the program I wanted to write using
graph paper and some Python doodles. Theres no point in trying to write
a Joy program until Im sure I understand the problem well enough.
graph paper and some Python doodles. There's no point in trying to write
a Joy program until I'm sure I understand the problem well enough.
The first thing I did was to write a column of numbers from 1 to n (32
as it happens) and next to them the desired output number, to look for
@ -81,7 +81,7 @@ patterns directly:
31 6
32 5
There are four groups repeating for a given “rank”, then the pattern
There are four groups repeating for a given "rank", then the pattern
enlarges and four groups repeat again, etc.
::
@ -93,7 +93,7 @@ enlarges and four groups repeat again, etc.
9 8 7 6 5 6 7 8 9 10
Four of this pyramid interlock to tile the plane extending from the
initial “1” square.
initial "1" square.
::
@ -102,7 +102,7 @@ initial “1” square.
And so on.
We can figure out the pattern for a row of the pyramid at a given “rank”
We can figure out the pattern for a row of the pyramid at a given "rank"
:math:`k`:
:math:`2k - 1, 2k - 2, ..., k, k + 1, k + 2, ..., 2k`
@ -115,82 +115,86 @@ This shows that the series consists at each place of :math:`k` plus some
number that begins at :math:`k - 1`, decreases to zero, then increases
to :math:`k`. Each row has :math:`2k` members.
Lets figure out how, given an index into a row, we can calculate the
Let's figure out how, given an index into a row, we can calculate the
value there. The index will be from 0 to :math:`k - 1`.
Lets look at an example, with :math:`k = 4`:
Let's look at an example, with :math:`k = 4`:
::
0 1 2 3 4 5 6 7
7 6 5 4 5 6 7 8
.. code:: ipython2
.. code:: ipython3
k = 4
Subtract :math:`k` from the index and take the absolute value:
.. code:: ipython2
.. code:: ipython3
for n in range(2 * k):
print abs(n - k),
print(abs(n - k),)
.. parsed-literal::
4 3 2 1 0 1 2 3
4
3
2
1
0
1
2
3
Not quite. Subtract :math:`k - 1` from the index and take the absolute
value:
.. code:: ipython2
.. code:: ipython3
for n in range(2 * k):
print abs(n - (k - 1)),
print(abs(n - (k - 1)), end=' ')
.. parsed-literal::
3 2 1 0 1 2 3 4
Great, now add :math:`k`...
Great, now add :math:`k`\ …
.. code:: ipython2
.. code:: ipython3
for n in range(2 * k):
print abs(n - (k - 1)) + k,
print(abs(n - (k - 1)) + k, end=' ')
.. parsed-literal::
7 6 5 4 5 6 7 8
So to write a function that can give us the value of a row at a given
index:
.. code:: ipython2
.. code:: ipython3
def row_value(k, i):
i %= (2 * k) # wrap the index at the row boundary.
return abs(i - (k - 1)) + k
.. code:: ipython2
.. code:: ipython3
k = 5
for i in range(2 * k):
print row_value(k, i),
print(row_value(k, i), end=' ')
.. parsed-literal::
9 8 7 6 5 6 7 8 9 10
(Im leaving out details of how I figured this all out and just giving
(I'm leaving out details of how I figured this all out and just giving
the relevent bits. It took a little while to zero in of the aspects of
the pattern that were important for the task.)
@ -209,11 +213,11 @@ initial square we have:
:math:`corner_k = 1 + \sum_{n=1}^k 8n`
Im not mathematically sophisticated enough to turn this directly into a
formula (but Sympy is, see below.) Im going to write a simple Python
I'm not mathematically sophisticated enough to turn this directly into a
formula (but Sympy is, see below.) I'm going to write a simple Python
function to iterate and search:
.. code:: ipython2
.. code:: ipython3
def rank_and_offset(n):
assert n >= 2 # Guard the domain.
@ -228,10 +232,10 @@ function to iterate and search:
n -= m # Remove this rank's worth.
k += 1
.. code:: ipython2
.. code:: ipython3
for n in range(2, 51):
print n, rank_and_offset(n)
print(n, rank_and_offset(n))
.. parsed-literal::
@ -287,11 +291,11 @@ function to iterate and search:
50 (4, 0)
.. code:: ipython2
.. code:: ipython3
for n in range(2, 51):
k, i = rank_and_offset(n)
print n, row_value(k, i)
print(n, row_value(k, i))
.. parsed-literal::
@ -350,7 +354,7 @@ function to iterate and search:
Putting it all together
~~~~~~~~~~~~~~~~~~~~~~~
.. code:: ipython2
.. code:: ipython3
def row_value(k, i):
return abs(i - (k - 1)) + k
@ -375,7 +379,7 @@ Putting it all together
k, i = rank_and_offset(n)
return row_value(k, i)
.. code:: ipython2
.. code:: ipython3
aoc20173(23)
@ -388,7 +392,7 @@ Putting it all together
.. code:: ipython2
.. code:: ipython3
aoc20173(23000)
@ -401,7 +405,7 @@ Putting it all together
.. code:: ipython2
.. code:: ipython3
aoc20173(23000000000000)
@ -420,17 +424,17 @@ Sympy to the Rescue
Find the rank for large numbers
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Using e.g. Sympy we can find the rank directly by solving for the roots
Using e.g. Sympy we can find the rank directly by solving for the roots
of an equation. For large numbers this will (eventually) be faster than
iterating as ``rank_and_offset()`` does.
.. code:: ipython2
.. code:: ipython3
from sympy import floor, lambdify, solve, symbols
from sympy import init_printing
init_printing()
.. code:: ipython2
.. code:: ipython3
k = symbols('k')
@ -444,7 +448,7 @@ and
We want:
.. code:: ipython2
.. code:: ipython3
E = 2 + 8 * k * (k + 1) / 2 # For the reason for adding 2 see above.
@ -455,13 +459,13 @@ We want:
.. math::
4 k \left(k + 1\right) + 2
\displaystyle 4 k \left(k + 1\right) + 2
We can write a function to solve for :math:`k` given some :math:`n`\ …
We can write a function to solve for :math:`k` given some :math:`n`...
.. code:: ipython2
.. code:: ipython3
def rank_of(n):
return floor(max(solve(E - n, k))) + 1
@ -472,15 +476,15 @@ about the larger one we use ``max()`` to select it. It will generally
not be a nice integer (unless :math:`n` is the number of an end-corner
of a rank) so we take the ``floor()`` and add 1 to get the integer rank
of :math:`n`. (Taking the ``ceiling()`` gives off-by-one errors on the
rank boundaries. I dont know why. Im basically like a monkey doing
rank boundaries. I don't know why. I'm basically like a monkey doing
math here.) =-D
It gives correct answers:
.. code:: ipython2
.. code:: ipython3
for n in (9, 10, 25, 26, 49, 50):
print n, rank_of(n)
print(n, rank_of(n))
.. parsed-literal::
@ -495,46 +499,46 @@ It gives correct answers:
And it runs much faster (at least for large numbers):
.. code:: ipython2
.. code:: ipython3
%time rank_of(23000000000000) # Compare runtime with rank_and_offset()!
.. parsed-literal::
CPU times: user 68 ms, sys: 8 ms, total: 76 ms
Wall time: 73.8 ms
CPU times: user 27.8 ms, sys: 5 µs, total: 27.8 ms
Wall time: 27.3 ms
.. math::
2397916
\displaystyle 2397916
.. code:: ipython2
.. code:: ipython3
%time rank_and_offset(23000000000000)
.. parsed-literal::
CPU times: user 308 ms, sys: 0 ns, total: 308 ms
Wall time: 306 ms
CPU times: user 216 ms, sys: 89 µs, total: 216 ms
Wall time: 215 ms
.. math::
\left ( 2397916, \quad 223606\right )
\displaystyle \left( 2397916, \ 223606\right)
After finding the rank you would still have to find the actual value of
the ranks first corner and subtract it (plus 2) from the number and
the rank's first corner and subtract it (plus 2) from the number and
compute the offset as above and then the final output, but this overhead
is partially shared by the other method, and overshadowed by the time it
(the other iterative method) would take for really big inputs.
@ -542,25 +546,25 @@ is partially shared by the other method, and overshadowed by the time it
The fun thing to do here would be to graph the actual runtime of both
methods against each other to find the trade-off point.
It took me a second to realize I could do this
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
It took me a second to realize I could do this...
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Sympy is a *symbolic* math library, and it supports symbolic
manipulation of equations. I can put in :math:`y` (instead of a value)
and ask it to solve for :math:`k`.
.. code:: ipython2
.. code:: ipython3
y = symbols('y')
.. code:: ipython2
.. code:: ipython3
g, f = solve(E - y, k)
The equation is quadratic so there are two roots, we are interested in
the greater one
the greater one...
.. code:: ipython2
.. code:: ipython3
g
@ -569,11 +573,11 @@ the greater one…
.. math::
- \frac{1}{2} \sqrt{y - 1} - \frac{1}{2}
\displaystyle - \frac{\sqrt{y - 1}}{2} - \frac{1}{2}
.. code:: ipython2
.. code:: ipython3
f
@ -582,14 +586,14 @@ the greater one…
.. math::
\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}
\displaystyle \frac{\sqrt{y - 1}}{2} - \frac{1}{2}
Now we can take the ``floor()``, add 1, and ``lambdify()`` the equation
to get a Python function that calculates the rank directly.
.. code:: ipython2
.. code:: ipython3
floor(f) + 1
@ -598,18 +602,18 @@ to get a Python function that calculates the rank directly.
.. math::
\lfloor{\frac{1}{2} \sqrt{y - 1} - \frac{1}{2}}\rfloor + 1
\displaystyle \left\lfloor{\frac{\sqrt{y - 1}}{2} - \frac{1}{2}}\right\rfloor + 1
.. code:: ipython2
.. code:: ipython3
F = lambdify(y, floor(f) + 1)
.. code:: ipython2
.. code:: ipython3
for n in (9, 10, 25, 26, 49, 50):
print n, int(F(n))
print(n, int(F(n)))
.. parsed-literal::
@ -622,53 +626,53 @@ to get a Python function that calculates the rank directly.
50 4
Its pretty fast.
It's pretty fast.
.. code:: ipython2
.. code:: ipython3
%time int(F(23000000000000)) # The clear winner.
.. parsed-literal::
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 11.9 µs
CPU times: user 60 µs, sys: 4 µs, total: 64 µs
Wall time: 67 µs
.. math::
2397916
\displaystyle 2397916
Knowing the equation we could write our own function manually, but the
speed is no better.
.. code:: ipython2
.. code:: ipython3
from math import floor as mfloor, sqrt
def mrank_of(n):
return int(mfloor(sqrt(23000000000000 - 1) / 2 - 0.5) + 1)
return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
.. code:: ipython2
.. code:: ipython3
%time mrank_of(23000000000000)
.. parsed-literal::
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 12.9 µs
CPU times: user 7 µs, sys: 1 µs, total: 8 µs
Wall time: 10 µs
.. math::
2397916
\displaystyle 2397916
@ -678,19 +682,19 @@ Given :math:`n` and a rank, compute the offset.
Now that we have a fast way to get the rank, we still need to use it to
compute the offset into a pyramid row.
.. code:: ipython2
.. code:: ipython3
def offset_of(n, k):
return (n - 2 + 4 * k * (k - 1)) % (2 * k)
(Note the sneaky way the sign changes from :math:`k(k + 1)` to
:math:`k(k - 1)`. This is because we want to subract the
:math:`(k - 1)`\ th ranks total places (its own and those of lesser
:math:`(k - 1)`\ th rank's total places (its own and those of lesser
rank) from our :math:`n` of rank :math:`k`. Substituting :math:`k - 1`
for :math:`k` in :math:`k(k + 1)` gives :math:`(k - 1)(k - 1 + 1)`,
which of course simplifies to :math:`k(k - 1)`.)
.. code:: ipython2
.. code:: ipython3
offset_of(23000000000000, 2397916)
@ -699,13 +703,13 @@ which of course simplifies to :math:`k(k - 1)`.)
.. math::
223606
\displaystyle 223606
So, we can compute the rank, then the offset, then the row value.
.. code:: ipython2
.. code:: ipython3
def rank_of(n):
return int(mfloor(sqrt(n - 1) / 2 - 0.5) + 1)
@ -724,7 +728,7 @@ So, we can compute the rank, then the offset, then the row value.
i = offset_of(n, k)
return row_value(k, i)
.. code:: ipython2
.. code:: ipython3
aoc20173(23)
@ -733,11 +737,11 @@ So, we can compute the rank, then the offset, then the row value.
.. math::
2
\displaystyle 2
.. code:: ipython2
.. code:: ipython3
aoc20173(23000)
@ -746,11 +750,11 @@ So, we can compute the rank, then the offset, then the row value.
.. math::
105
\displaystyle 105
.. code:: ipython2
.. code:: ipython3
aoc20173(23000000000000)
@ -759,26 +763,26 @@ So, we can compute the rank, then the offset, then the row value.
.. math::
4572225
\displaystyle 4572225
.. code:: ipython2
.. code:: ipython3
%time aoc20173(23000000000000000000000000) # Fast for large values.
.. parsed-literal::
CPU times: user 0 ns, sys: 0 ns, total: 0 ns
Wall time: 20 µs
CPU times: user 22 µs, sys: 2 µs, total: 24 µs
Wall time: 26.7 µs
.. math::
2690062495969
\displaystyle 2690062495969
@ -788,7 +792,7 @@ A Joy Version
At this point I feel confident that I can implement a concise version of
this code in Joy. ;-)
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
@ -809,9 +813,9 @@ The translation is straightforward.
rank_of == -- sqrt 2 / 0.5 - floor ++
.. code:: ipython2
.. code:: ipython3
define('rank_of == -- sqrt 2 / 0.5 - floor ++')
define('rank_of -- sqrt 2 / 0.5 - floor ++')
``offset_of``
~~~~~~~~~~~~~
@ -824,7 +828,7 @@ The translation is straightforward.
(n - 2 + 4 * k * (k - 1)) % (2 * k)
A little tricky
A little tricky...
::
@ -849,9 +853,9 @@ Ergo:
offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %
.. code:: ipython2
.. code:: ipython3
define('offset_of == dup 2 * [dup -- 4 * * 2 + -] dip %')
define('offset_of dup 2 * [dup -- 4 * * 2 + -] dip %')
``row_value``
~~~~~~~~~~~~~
@ -871,9 +875,9 @@ Ergo:
k |i-k-1| +
k+|i-k-1|
.. code:: ipython2
.. code:: ipython3
define('row_value == over -- - abs +')
define('row_value over -- - abs +')
``aoc2017.3``
~~~~~~~~~~~~~
@ -891,11 +895,11 @@ Ergo:
k i row_value
m
.. code:: ipython2
.. code:: ipython3
define('aoc2017.3 == dup rank_of [offset_of] dupdip swap row_value')
define('aoc2017.3 dup rank_of [offset_of] dupdip swap row_value')
.. code:: ipython2
.. code:: ipython3
J('23 aoc2017.3')
@ -905,7 +909,7 @@ Ergo:
2
.. code:: ipython2
.. code:: ipython3
J('23000 aoc2017.3')
@ -915,52 +919,52 @@ Ergo:
105
.. code:: ipython2
.. code:: ipython3
V('23000000000000 aoc2017.3')
.. parsed-literal::
. 23000000000000 aoc2017.3
23000000000000 . aoc2017.3
23000000000000 . dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 . -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 . sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 . 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 . / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 . 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 . - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 . floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 . ++ [offset_of] dupdip swap row_value
23000000000000 2397916 . [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] . dupdip swap row_value
23000000000000 2397916 . offset_of 2397916 swap row_value
23000000000000 2397916 . dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 . 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 . * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 . [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] . dip % 2397916 swap row_value
23000000000000 2397916 . dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 . -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 . 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 . * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 . * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 . 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 . + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 . - 4795832 % 2397916 swap row_value
5019438 . 4795832 % 2397916 swap row_value
5019438 4795832 . % 2397916 swap row_value
223606 . 2397916 swap row_value
223606 2397916 . swap row_value
2397916 223606 . row_value
2397916 223606 . over -- - abs +
2397916 223606 2397916 . -- - abs +
2397916 223606 2397915 . - abs +
2397916 -2174309 . abs +
2397916 2174309 . +
4572225 .
23000000000000 aoc2017.3
23000000000000 aoc2017.3
23000000000000 dup rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 rank_of [offset_of] dupdip swap row_value
23000000000000 23000000000000 -- sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 22999999999999 sqrt 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 4795831.523312615 2 / 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.7616563076 0.5 - floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915.2616563076 floor ++ [offset_of] dupdip swap row_value
23000000000000 2397915 ++ [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 [offset_of] dupdip swap row_value
23000000000000 2397916 offset_of 2397916 swap row_value
23000000000000 2397916 dup 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 2397916 2 * [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 4795832 [dup -- 4 * * 2 + -] dip % 2397916 swap row_value
23000000000000 2397916 dup -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397916 -- 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 2397915 4 * * 2 + - 4795832 % 2397916 swap row_value
23000000000000 2397916 9591660 * 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980560 2 + - 4795832 % 2397916 swap row_value
23000000000000 22999994980562 - 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
5019438 4795832 % 2397916 swap row_value
223606 2397916 swap row_value
223606 2397916 swap row_value
2397916 223606 row_value
2397916 223606 over -- - abs +
2397916 223606 2397916 -- - abs +
2397916 223606 2397915 - abs +
2397916 -2174309 abs +
2397916 2174309 +
4572225
::

View File

@ -13094,7 +13094,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13141,7 +13141,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[step_zero] help&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[step_zero] help&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13158,7 +13158,13 @@ div#notebook {
<div class="output_subarea output_stream output_stdout output_text">
<pre>0 roll&gt; step
<pre>
==== Help on step_zero ====
0 roll&gt; step
---- end (step_zero)
</pre>
</div>
@ -13182,7 +13188,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.4 == [[size] [unique size] cleave = +] step_zero&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.4 [[size] [unique size] cleave = +] step_zero&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13195,7 +13201,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1">[[5 1 9 5]</span>
<span class="s1"> [7 5 4 3]</span>

View File

@ -66,7 +66,13 @@
"name": "stdout",
"output_type": "stream",
"text": [
"\n",
"==== Help on step_zero ====\n",
"\n",
"0 roll> step\n",
"\n",
"---- end (step_zero)\n",
"\n",
"\n"
]
}
@ -88,7 +94,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero')"
"define('AoC2017.4 [[size] [unique size] cleave = +] step_zero')"
]
},
{
@ -124,14 +130,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -37,15 +37,21 @@ The `step_zero` combinator includes the `0 swap` that would normally open one of
J('[step_zero] help')
```
==== Help on step_zero ====
0 roll> step
---- end (step_zero)
AoC2017.4 == [F +] step_zero
```python
define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero')
define('AoC2017.4 [[size] [unique size] cleave = +] step_zero')
```

View File

@ -12,14 +12,14 @@ For example:
- aa bb cc dd aa is not valid - the word aa appears more than once.
- aa bb cc dd aaa is valid - aa and aaa count as different words.
The systems full passphrase list is available as your puzzle input. How
The system's full passphrase list is available as your puzzle input. How
many passphrases are valid?
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
Ill assume the input is a Joy sequence of sequences of integers.
I'll assume the input is a Joy sequence of sequences of integers.
::
@ -40,26 +40,32 @@ So, obviously, the initial form will be a ``step`` function:
The ``step_zero`` combinator includes the ``0 swap`` that would normally
open one of these definitions:
.. code:: ipython2
.. code:: ipython3
J('[step_zero] help')
.. parsed-literal::
==== Help on step_zero ====
0 roll> step
---- end (step_zero)
::
AoC2017.4 == [F +] step_zero
.. code:: ipython2
.. code:: ipython3
define('AoC2017.4 == [[size] [unique size] cleave = +] step_zero')
define('AoC2017.4 [[size] [unique size] cleave = +] step_zero')
.. code:: ipython2
.. code:: ipython3
J('''

View File

@ -13178,7 +13178,7 @@ E == roll&lt; popop</code></pre>
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">SimpleFunctionWrapper</span>
<span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">list_to_stack</span>
@ -13217,7 +13217,7 @@ E == roll&lt; popop</code></pre>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;3 [0 1 2 3 4 5] incr_at&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;3 [0 1 2 3 4 5] incr_at&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13300,7 +13300,7 @@ R1 == get_value incr_value add_value incr_step_count
F == [P] [T] [R1] primrec
F == [popop !size! &gt;=] [roll&lt; pop] [get_value incr_value add_value incr_step_count] primrec</code></pre>
F == [popop !size! &gt;=] [roll&lt; pop] [get_value incr_value add_value incr_step_count] tailrec</code></pre>
</div>
</div>
@ -13310,17 +13310,17 @@ F == [popop !size! &gt;=] [roll&lt; pop] [get_value incr_value add_value incr_st
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">DefinitionWrapper</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">DefinitionWrapper</span>
<span class="n">DefinitionWrapper</span><span class="o">.</span><span class="n">add_definitions</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1"> get_value == [roll&lt; at] nullary</span>
<span class="s1"> incr_value == [[popd incr_at] unary] dip</span>
<span class="s1"> add_value == [+] cons dipd</span>
<span class="s1">incr_step_count == [++] dip</span>
<span class="s1"> get_value [roll&lt; at] nullary</span>
<span class="s1"> incr_value [[popd incr_at] unary] dip</span>
<span class="s1"> add_value [+] cons dipd</span>
<span class="s1">incr_step_count [++] dip</span>
<span class="s1"> AoC2017.5.0 == get_value incr_value add_value incr_step_count</span>
<span class="s1"> AoC2017.5.0 get_value incr_value add_value incr_step_count</span>
<span class="s1">&#39;&#39;&#39;</span><span class="p">,</span> <span class="n">D</span><span class="p">)</span>
</pre></div>
@ -13335,7 +13335,19 @@ F == [popop !size! &gt;=] [roll&lt; pop] [get_value incr_value add_value incr_st
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;F == [popop 5 &gt;=] [roll&lt; popop] [AoC2017.5.0] primrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">DefinitionWrapper</span>
<span class="n">DefinitionWrapper</span><span class="o">.</span><span class="n">add_definitions</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1"> get_value [roll&lt; at] nullary</span>
<span class="s1"> incr_value [[popd incr_at] unary] dip</span>
<span class="s1"> add_value [+] cons dipd</span>
<span class="s1">incr_step_count [++] dip</span>
<span class="s1"> AoC2017.5.0 get_value incr_value add_value incr_step_count</span>
<span class="s1">&#39;&#39;&#39;</span><span class="p">,</span> <span class="n">D</span><span class="p">)</span>
</pre></div>
</div>
@ -13348,7 +13360,20 @@ F == [popop !size! &gt;=] [roll&lt; pop] [get_value incr_value add_value incr_st
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;0 0 [0 3 0 1 -3] F&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;F [popop 5 &gt;=] [roll&lt; popop] [AoC2017.5.0] tailrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;0 0 [0 3 0 1 -3] F&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13408,17 +13433,17 @@ AoC2017.5.preamble == init-index-and-step-count prepare-predicate</code></pre>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">DefinitionWrapper</span><span class="o">.</span><span class="n">add_definitions</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">DefinitionWrapper</span><span class="o">.</span><span class="n">add_definitions</span><span class="p">(</span><span class="s1">&#39;&#39;&#39;</span>
<span class="s1">init-index-and-step-count == 0 0 roll&lt;</span>
<span class="s1"> prepare-predicate == dup size [&gt;=] cons [popop] swoncat</span>
<span class="s1">init-index-and-step-count 0 0 roll&lt;</span>
<span class="s1">prepare-predicate dup size [&gt;=] cons [popop] swoncat</span>
<span class="s1"> AoC2017.5.preamble == init-index-and-step-count prepare-predicate</span>
<span class="s1">AoC2017.5.preamble init-index-and-step-count prepare-predicate</span>
<span class="s1"> AoC2017.5 == AoC2017.5.preamble [roll&lt; popop] [AoC2017.5.0] primrec</span>
<span class="s1">AoC2017.5 AoC2017.5.preamble [roll&lt; popop] [AoC2017.5.0] tailrec</span>
<span class="s1">&#39;&#39;&#39;</span><span class="p">,</span> <span class="n">D</span><span class="p">)</span>
</pre></div>
@ -13430,10 +13455,10 @@ AoC2017.5.preamble == init-index-and-step-count prepare-predicate</code></pre>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 3 0 1 -3] AoC2017.5&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 3 0 1 -3] AoC2017.5&#39;</span><span class="p">)</span>
</pre></div>
</div>

View File

@ -209,7 +209,7 @@
"\n",
" F == [P] [T] [R1] primrec\n",
" \n",
" F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec"
" F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] tailrec"
]
},
{
@ -223,12 +223,12 @@
"\n",
"DefinitionWrapper.add_definitions('''\n",
"\n",
" get_value == [roll< at] nullary\n",
" incr_value == [[popd incr_at] unary] dip\n",
" add_value == [+] cons dipd\n",
"incr_step_count == [++] dip\n",
" get_value [roll< at] nullary\n",
" incr_value [[popd incr_at] unary] dip\n",
" add_value [+] cons dipd\n",
"incr_step_count [++] dip\n",
"\n",
" AoC2017.5.0 == get_value incr_value add_value incr_step_count\n",
" AoC2017.5.0 get_value incr_value add_value incr_step_count\n",
"\n",
"''', D)"
]
@ -239,12 +239,33 @@
"metadata": {},
"outputs": [],
"source": [
"define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')"
"from joy.library import DefinitionWrapper\n",
"\n",
"\n",
"DefinitionWrapper.add_definitions('''\n",
"\n",
" get_value [roll< at] nullary\n",
" incr_value [[popd incr_at] unary] dip\n",
" add_value [+] cons dipd\n",
"incr_step_count [++] dip\n",
"\n",
" AoC2017.5.0 get_value incr_value add_value incr_step_count\n",
"\n",
"''', D)"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"define('F [popop 5 >=] [roll< popop] [AoC2017.5.0] tailrec')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"scrolled": true
},
@ -294,25 +315,25 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"DefinitionWrapper.add_definitions('''\n",
"\n",
"init-index-and-step-count == 0 0 roll<\n",
" prepare-predicate == dup size [>=] cons [popop] swoncat\n",
"init-index-and-step-count 0 0 roll<\n",
"prepare-predicate dup size [>=] cons [popop] swoncat\n",
"\n",
" AoC2017.5.preamble == init-index-and-step-count prepare-predicate\n",
"AoC2017.5.preamble init-index-and-step-count prepare-predicate\n",
"\n",
" AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec\n",
"AoC2017.5 AoC2017.5.preamble [roll< popop] [AoC2017.5.0] tailrec\n",
"\n",
"''', D)"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {
"scrolled": false
},
@ -365,14 +386,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -145,7 +145,7 @@ J('3 [0 1 2 3 4 5] incr_at')
F == [P] [T] [R1] primrec
F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec
F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] tailrec
```python
@ -154,19 +154,36 @@ from joy.library import DefinitionWrapper
DefinitionWrapper.add_definitions('''
get_value == [roll< at] nullary
incr_value == [[popd incr_at] unary] dip
add_value == [+] cons dipd
incr_step_count == [++] dip
get_value [roll< at] nullary
incr_value [[popd incr_at] unary] dip
add_value [+] cons dipd
incr_step_count [++] dip
AoC2017.5.0 == get_value incr_value add_value incr_step_count
AoC2017.5.0 get_value incr_value add_value incr_step_count
''', D)
```
```python
define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')
from joy.library import DefinitionWrapper
DefinitionWrapper.add_definitions('''
get_value [roll< at] nullary
incr_value [[popd incr_at] unary] dip
add_value [+] cons dipd
incr_step_count [++] dip
AoC2017.5.0 get_value incr_value add_value incr_step_count
''', D)
```
```python
define('F [popop 5 >=] [roll< popop] [AoC2017.5.0] tailrec')
```
@ -207,12 +224,12 @@ So:
```python
DefinitionWrapper.add_definitions('''
init-index-and-step-count == 0 0 roll<
prepare-predicate == dup size [>=] cons [popop] swoncat
init-index-and-step-count 0 0 roll<
prepare-predicate dup size [>=] cons [popop] swoncat
AoC2017.5.preamble == init-index-and-step-count prepare-predicate
AoC2017.5.preamble init-index-and-step-count prepare-predicate
AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
AoC2017.5 AoC2017.5.preamble [roll< popop] [AoC2017.5.0] tailrec
''', D)
```

View File

@ -4,7 +4,7 @@ Advent of Code 2017
December 5th
------------
a list of the offsets for each jump. Jumps are relative: -1 moves to
...a list of the offsets for each jump. Jumps are relative: -1 moves to
the previous instruction, and 2 skips the next one. Start at the first
instruction in the list. The goal is to follow the jumps until one leads
outside the list.
@ -24,7 +24,7 @@ For example, consider the following list of jump offsets:
1
-3
Positive jumps (“forward”) move downward; negative jumps move upward.
Positive jumps ("forward") move downward; negative jumps move upward.
For legibility in this example, these offset values will be written all
on one line, with the current instruction marked in parentheses. The
following steps would be taken before an exit is found:
@ -35,24 +35,14 @@ following steps would be taken before an exit is found:
-
(1) 3 0 1 -3 - jump with offset 0 (that is, dont jump at all).
(1) 3 0 1 -3 - jump with offset 0 (that is, don't jump at all).
Fortunately, the instruction is then incremented to 1.
- ::
2 (3) 0 1 -3 - step forward because of the instruction we just modified. The first instruction is incremented again, now to 2.
- ::
2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
- ::
2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
- ::
2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
- 2 (3) 0 1 -3 - step forward because of the instruction we just
modified. The first instruction is incremented again, now to 2.
- 2 4 0 1 (-3) - jump all the way to the end; leave a 4 behind.
- 2 (4) 0 1 -2 - go back to where we just were; increment -3 to -2.
- 2 5 0 1 -2 - jump 4 steps forward, escaping the maze.
In this example, the exit is reached in 5 steps.
@ -61,7 +51,7 @@ How many steps does it take to reach the exit?
Breakdown
---------
For now, Im going to assume a starting state with the size of the
For now, I'm going to assume a starting state with the size of the
sequence pre-computed. We need it to define the exit condition and it is
a trivial preamble to generate it. We then need and ``index`` and a
``step-count``, which are both initially zero. Then we have the sequence
@ -77,7 +67,7 @@ itself, and some recursive function ``F`` that does the work.
Later on I was thinking about it and the Forth heuristic came to mind,
to wit: four things on the stack are kind of much. Immediately I
realized that the size properly belongs in the predicate of ``F``! Doh!
realized that the size properly belongs in the predicate of ``F``! D'oh!
::
@ -85,7 +75,7 @@ realized that the size properly belongs in the predicate of ``F``! Doh!
------------------------------
step-count
So, lets start by nailing down the predicate:
So, let's start by nailing down the predicate:
::
@ -122,16 +112,16 @@ The ``R1`` function has a big job:
The only tricky thing there is incrementing an integer in the sequence.
Joy sequences are not particularly good for random access. We could
encode the list of jump offsets in a big integer and use math to do the
processing for a good speed-up, but it still wouldnt beat the
performance of e.g. a mutable array. This is just one of those places
where “plain vanilla” Joypy doesnt shine (in default performance. The
processing for a good speed-up, but it still wouldn't beat the
performance of e.g. a mutable array. This is just one of those places
where "plain vanilla" Joypy doesn't shine (in default performance. The
legendary *Sufficiently-Smart Compiler* would of course rewrite this
function to use an array “under the hood”.)
function to use an array "under the hood".)
In the meantime, Im going to write a primitive function that just does
In the meantime, I'm going to write a primitive function that just does
what we need.
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import D, J, V, define
from joy.library import SimpleFunctionWrapper
@ -161,7 +151,7 @@ what we need.
D['incr_at'] = incr_at
.. code:: ipython2
.. code:: ipython3
J('3 [0 1 2 3 4 5] incr_at')
@ -207,8 +197,8 @@ increment the step count
3+n 0 [0 1 2 n+1 4] [++] dip
3+n 1 [0 1 2 n+1 4]
All together now
~~~~~~~~~~~~~~~~~
All together now...
~~~~~~~~~~~~~~~~~~~
::
@ -221,29 +211,45 @@ All together now…
F == [P] [T] [R1] primrec
F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] primrec
F == [popop !size! >=] [roll< pop] [get_value incr_value add_value incr_step_count] tailrec
.. code:: ipython2
.. code:: ipython3
from joy.library import DefinitionWrapper
DefinitionWrapper.add_definitions('''
get_value == [roll< at] nullary
incr_value == [[popd incr_at] unary] dip
add_value == [+] cons dipd
incr_step_count == [++] dip
get_value [roll< at] nullary
incr_value [[popd incr_at] unary] dip
add_value [+] cons dipd
incr_step_count [++] dip
AoC2017.5.0 == get_value incr_value add_value incr_step_count
AoC2017.5.0 get_value incr_value add_value incr_step_count
''', D)
.. code:: ipython2
.. code:: ipython3
define('F == [popop 5 >=] [roll< popop] [AoC2017.5.0] primrec')
from joy.library import DefinitionWrapper
.. code:: ipython2
DefinitionWrapper.add_definitions('''
get_value [roll< at] nullary
incr_value [[popd incr_at] unary] dip
add_value [+] cons dipd
incr_step_count [++] dip
AoC2017.5.0 get_value incr_value add_value incr_step_count
''', D)
.. code:: ipython3
define('F [popop 5 >=] [roll< popop] [AoC2017.5.0] tailrec')
.. code:: ipython3
J('0 0 [0 3 0 1 -3] F')
@ -288,20 +294,20 @@ So:
AoC2017.5.preamble == init-index-and-step-count prepare-predicate
.. code:: ipython2
.. code:: ipython3
DefinitionWrapper.add_definitions('''
init-index-and-step-count == 0 0 roll<
prepare-predicate == dup size [>=] cons [popop] swoncat
init-index-and-step-count 0 0 roll<
prepare-predicate dup size [>=] cons [popop] swoncat
AoC2017.5.preamble == init-index-and-step-count prepare-predicate
AoC2017.5.preamble init-index-and-step-count prepare-predicate
AoC2017.5 == AoC2017.5.preamble [roll< popop] [AoC2017.5.0] primrec
AoC2017.5 AoC2017.5.preamble [roll< popop] [AoC2017.5.0] tailrec
''', D)
.. code:: ipython2
.. code:: ipython3
J('[0 3 0 1 -3] AoC2017.5')
@ -329,5 +335,5 @@ So:
This is by far the largest program I have yet written in Joy. Even with
the ``incr_at`` function it is still a bear. There may be an arrangement
of the parameters that would permit more elegant definitions, but it
still wouldnt be as efficient as something written in assembly, C, or
still wouldn't be as efficient as something written in assembly, C, or
even Python.

View File

@ -13088,7 +13088,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13101,7 +13101,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] dup max&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] dup max&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13132,7 +13132,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">SimpleFunctionWrapper</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">joy.library</span> <span class="kn">import</span> <span class="n">SimpleFunctionWrapper</span>
<span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">list_to_stack</span>
@ -13176,7 +13176,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] 7 index_of&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] 7 index_of&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13207,7 +13207,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] 23 index_of&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] 23 index_of&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13253,7 +13253,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">iter_stack</span><span class="p">,</span> <span class="n">list_to_stack</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">joy.utils.stack</span> <span class="kn">import</span> <span class="n">iter_stack</span><span class="p">,</span> <span class="n">list_to_stack</span>
<span class="nd">@SimpleFunctionWrapper</span>
@ -13292,7 +13292,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13323,7 +13323,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[2 4 1 2] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[2 4 1 2] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13354,7 +13354,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 1 2 3] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[3 1 2 3] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13385,7 +13385,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 3 4] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 3 4] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13416,7 +13416,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 3 4 1] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 3 4 1] dup max [index_of] nullary distribute&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13469,7 +13469,7 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;direco == dip rest cons&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;direco dip rest cons&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13482,7 +13482,7 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
<div class="prompt input_prompt">In&nbsp;[13]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;G == [direco] cons [swap] swoncat cons&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;G [direco] cons [swap] swoncat cons&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13495,7 +13495,7 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
<div class="prompt input_prompt">In&nbsp;[14]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;make_distributor == [dup dup max [index_of] nullary distribute] G&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;make_distributor [dup dup max [index_of] nullary distribute] G&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13508,7 +13508,7 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
<div class="prompt input_prompt">In&nbsp;[15]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] make_distributor 6 [x] times pop&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] make_distributor 6 [x] times pop&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13539,13 +13539,13 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
<div class="text_cell_render border-box-sizing rendered_html">
<h3 id="A-function-to-drive-a-generator-and-count-how-many-states-before-a-repeat.">A function to drive a generator and count how many states before a repeat.<a class="anchor-link" href="#A-function-to-drive-a-generator-and-count-how-many-states-before-a-repeat.">&#182;</a></h3><p>First draft:</p>
<pre><code>[] [GEN] x [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] primrec
<pre><code>[] [GEN] x [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] tailrec
</code></pre>
<p>(?)</p>
<pre><code>[] [GEN] x [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] primrec
<pre><code>[] [GEN] x [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop index_of 0 &gt;=
[] [...] index_of 0 &gt;=
-1 0 &gt;=
@ -13554,7 +13554,7 @@ w/ G == dup max [index_of] nullary distribute</code></pre>
</code></pre>
<p>Base case</p>
<pre><code>[] [...] [GEN] [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] primrec
<pre><code>[] [...] [GEN] [pop index_of 0 &gt;=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop size --
[] [...] size --
[] [...] size --
@ -13569,7 +13569,7 @@ n
</code></pre>
<p>Recursive case</p>
<pre><code>[] [...] [GEN] [pop index_of 0 &gt;=] [popop size] [[swons] dip x] primrec
<pre><code>[] [...] [GEN] [pop index_of 0 &gt;=] [popop size] [[swons] dip x] tailrec
[] [...] [GEN] [swons] dip x F
[] [...] swons [GEN] x F
[[...]] [GEN] x F
@ -13580,7 +13580,7 @@ n
</code></pre>
<p>What have we learned?</p>
<pre><code>F == [pop index_of 0 &gt;=] [popop size] [[swons] dip x] primrec</code></pre>
<pre><code>F == [pop index_of 0 &gt;=] [popop size] [[swons] dip x] tailrec</code></pre>
</div>
</div>
@ -13590,7 +13590,7 @@ n
<div class="prompt input_prompt">In&nbsp;[16]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;count_states == [] swap x [pop index_of 0 &gt;=] [popop size] [[swons] dip x] primrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;count_states [] swap x [pop index_of 0 &gt;=] [popop size] [[swons] dip x] tailrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13603,7 +13603,7 @@ n
<div class="prompt input_prompt">In&nbsp;[17]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.6 == make_distributor count_states&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;AoC2017.6 make_distributor count_states&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13616,7 +13616,7 @@ n
<div class="prompt input_prompt">In&nbsp;[18]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] AoC2017.6&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[0 2 7 0] AoC2017.6&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13647,7 +13647,7 @@ n
<div class="prompt input_prompt">In&nbsp;[19]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 1] AoC2017.6&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 1 1] AoC2017.6&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13678,7 +13678,7 @@ n
<div class="prompt input_prompt">In&nbsp;[20]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[8 0 0 0 0 0] AoC2017.6&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[8 0 0 0 0 0] AoC2017.6&#39;</span><span class="p">)</span>
</pre></div>
</div>

View File

@ -275,7 +275,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('direco == dip rest cons')"
"define('direco dip rest cons')"
]
},
{
@ -284,7 +284,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('G == [direco] cons [swap] swoncat cons')"
"define('G [direco] cons [swap] swoncat cons')"
]
},
{
@ -293,7 +293,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('make_distributor == [dup dup max [index_of] nullary distribute] G')"
"define('make_distributor [dup dup max [index_of] nullary distribute] G')"
]
},
{
@ -320,12 +320,12 @@
"### A function to drive a generator and count how many states before a repeat.\n",
"First draft:\n",
"\n",
" [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n",
" [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
"\n",
"(?)\n",
"\n",
" [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n",
" [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n",
" [] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
" [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
" [] [...] [GEN] pop index_of 0 >=\n",
" [] [...] index_of 0 >=\n",
" -1 0 >=\n",
@ -333,7 +333,7 @@
"\n",
"Base case\n",
"\n",
" [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec\n",
" [] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec\n",
" [] [...] [GEN] pop size --\n",
" [] [...] size --\n",
" [] [...] size --\n",
@ -346,7 +346,7 @@
"\n",
"Recursive case\n",
"\n",
" [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec\n",
" [] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec\n",
" [] [...] [GEN] [swons] dip x F\n",
" [] [...] swons [GEN] x F\n",
" [[...]] [GEN] x F\n",
@ -356,7 +356,7 @@
"\n",
"What have we learned?\n",
"\n",
" F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec"
" F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec"
]
},
{
@ -365,7 +365,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')"
"define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')"
]
},
{
@ -374,7 +374,7 @@
"metadata": {},
"outputs": [],
"source": [
"define('AoC2017.6 == make_distributor count_states')"
"define('AoC2017.6 make_distributor count_states')"
]
},
{
@ -442,14 +442,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.13"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -169,17 +169,17 @@ J('[1 3 4 1] dup max [index_of] nullary distribute')
```python
define('direco == dip rest cons')
define('direco dip rest cons')
```
```python
define('G == [direco] cons [swap] swoncat cons')
define('G [direco] cons [swap] swoncat cons')
```
```python
define('make_distributor == [dup dup max [index_of] nullary distribute] G')
define('make_distributor [dup dup max [index_of] nullary distribute] G')
```
@ -193,12 +193,12 @@ J('[0 2 7 0] make_distributor 6 [x] times pop')
### A function to drive a generator and count how many states before a repeat.
First draft:
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
(?)
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop index_of 0 >=
[] [...] index_of 0 >=
-1 0 >=
@ -206,7 +206,7 @@ First draft:
Base case
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop size --
[] [...] size --
[] [...] size --
@ -219,7 +219,7 @@ A mistake, `popop` and no need for `--`
Recursive case
[] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
[] [...] [GEN] [swons] dip x F
[] [...] swons [GEN] x F
[[...]] [GEN] x F
@ -229,16 +229,16 @@ Recursive case
What have we learned?
F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
```python
define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')
define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')
```
```python
define('AoC2017.6 == make_distributor count_states')
define('AoC2017.6 make_distributor count_states')
```

View File

@ -8,11 +8,11 @@ December 6th
[0 2 7 0] dup max
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import D, J, V, define
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] dup max')
@ -22,7 +22,7 @@ December 6th
[0 2 7 0] 7
.. code:: ipython2
.. code:: ipython3
from joy.library import SimpleFunctionWrapper
from joy.utils.stack import list_to_stack
@ -57,7 +57,7 @@ December 6th
D['index_of'] = index_of
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] 7 index_of')
@ -67,7 +67,7 @@ December 6th
2
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] 23 index_of')
@ -77,7 +77,7 @@ December 6th
-1
Starting at ``index`` distribute ``count`` “blocks” to the “banks” in
Starting at ``index`` distribute ``count`` "blocks" to the "banks" in
the sequence.
::
@ -86,9 +86,9 @@ the sequence.
----------------------------
[...]
This seems like it would be a PITA to implement in Joypy
This seems like it would be a PITA to implement in Joypy...
.. code:: ipython2
.. code:: ipython3
from joy.utils.stack import iter_stack, list_to_stack
@ -118,7 +118,7 @@ This seems like it would be a PITA to implement in Joypy…
D['distribute'] = distribute
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] dup max [index_of] nullary distribute')
@ -128,7 +128,7 @@ This seems like it would be a PITA to implement in Joypy…
[2 4 1 2]
.. code:: ipython2
.. code:: ipython3
J('[2 4 1 2] dup max [index_of] nullary distribute')
@ -138,7 +138,7 @@ This seems like it would be a PITA to implement in Joypy…
[3 1 2 3]
.. code:: ipython2
.. code:: ipython3
J('[3 1 2 3] dup max [index_of] nullary distribute')
@ -148,7 +148,7 @@ This seems like it would be a PITA to implement in Joypy…
[0 2 3 4]
.. code:: ipython2
.. code:: ipython3
J('[0 2 3 4] dup max [index_of] nullary distribute')
@ -158,7 +158,7 @@ This seems like it would be a PITA to implement in Joypy…
[1 3 4 1]
.. code:: ipython2
.. code:: ipython3
J('[1 3 4 1] dup max [index_of] nullary distribute')
@ -168,7 +168,7 @@ This seems like it would be a PITA to implement in Joypy…
[2 4 1 2]
Recalling “Generator Programs”
Recalling "Generator Programs"
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
::
@ -188,19 +188,19 @@ Recalling “Generator Programs”
w/ G == dup max [index_of] nullary distribute
.. code:: ipython2
.. code:: ipython3
define('direco == dip rest cons')
define('direco dip rest cons')
.. code:: ipython2
.. code:: ipython3
define('G == [direco] cons [swap] swoncat cons')
define('G [direco] cons [swap] swoncat cons')
.. code:: ipython2
.. code:: ipython3
define('make_distributor == [dup dup max [index_of] nullary distribute] G')
define('make_distributor [dup dup max [index_of] nullary distribute] G')
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] make_distributor 6 [x] times pop')
@ -217,14 +217,14 @@ First draft:
::
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
(?)
::
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [GEN] x [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop index_of 0 >=
[] [...] index_of 0 >=
-1 0 >=
@ -234,7 +234,7 @@ Base case
::
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [pop size --] [[swons] dip x] tailrec
[] [...] [GEN] pop size --
[] [...] size --
[] [...] size --
@ -251,7 +251,7 @@ Recursive case
::
[] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
[] [...] [GEN] [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
[] [...] [GEN] [swons] dip x F
[] [...] swons [GEN] x F
[[...]] [GEN] x F
@ -263,17 +263,17 @@ What have we learned?
::
F == [pop index_of 0 >=] [popop size] [[swons] dip x] primrec
F == [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec
.. code:: ipython2
.. code:: ipython3
define('count_states == [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] primrec')
define('count_states [] swap x [pop index_of 0 >=] [popop size] [[swons] dip x] tailrec')
.. code:: ipython2
.. code:: ipython3
define('AoC2017.6 == make_distributor count_states')
define('AoC2017.6 make_distributor count_states')
.. code:: ipython2
.. code:: ipython3
J('[0 2 7 0] AoC2017.6')
@ -283,7 +283,7 @@ What have we learned?
5
.. code:: ipython2
.. code:: ipython3
J('[1 1 1] AoC2017.6')
@ -293,7 +293,7 @@ What have we learned?
4
.. code:: ipython2
.. code:: ipython3
J('[8 0 0 0 0 0] AoC2017.6')

File diff suppressed because it is too large Load Diff

View File

@ -29,11 +29,11 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 sqr\n",
" 23 . sqr\n",
" 23 . dup mul\n",
"23 23 . mul\n",
" 529 . \n"
" 23 sqr\n",
" 23 sqr\n",
" 23 dup mul\n",
"23 23 mul\n",
" 529 \n"
]
}
],
@ -89,9 +89,9 @@
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 sqr\n",
" 23 . sqr\n",
"529 . \n"
" 23 sqr\n",
" 23 sqr\n",
"529 \n"
]
}
],
@ -131,7 +131,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@ -145,7 +145,7 @@
}
],
"source": [
"print compile_joy_definition(old_sqr)"
"print(compile_joy_definition(old_sqr))"
]
},
{
@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@ -168,7 +168,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@ -186,7 +186,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@ -214,9 +214,21 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "ModuleNotFoundError",
"evalue": "No module named 'joy.utils.types'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-14-d5ef3c7560be>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mfrom\u001b[0m \u001b[0mjoy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mutils\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtypes\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mcompile_\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mdoc_from_stack_effect\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0minfer_string\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mjoy\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mlibrary\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mSimpleFunctionWrapper\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'joy.utils.types'"
]
}
],
"source": [
"from joy.utils.types import compile_, doc_from_stack_effect, infer_string\n",
"from joy.library import SimpleFunctionWrapper"
@ -224,7 +236,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -240,17 +252,9 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(a2 a1 -- a1 a2 a1 a2 a2)\n"
]
}
],
"outputs": [],
"source": [
"for fi, fo in stack_effects:\n",
" print doc_from_stack_effect(fi, fo)"
@ -258,7 +262,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -274,34 +278,27 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def foo(stack):\n",
" \"\"\"\n",
" ::\n",
"\n",
" (a2 a1 -- a1 a2 a1 a2 a2)\n",
"\n",
" \"\"\"\n",
" (a1, (a2, s1)) = stack\n",
" return (a2, (a2, (a1, (a2, (a1, s1)))))\n"
]
}
],
"outputs": [],
"source": [
"print source"
]
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 9,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "SyntaxError",
"evalue": "invalid syntax (<ipython-input-9-1a7e90bf2d7b>, line 1)",
"output_type": "error",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-9-1a7e90bf2d7b>\"\u001b[0;36m, line \u001b[0;32m1\u001b[0m\n\u001b[0;31m exec compile(source, '__main__', 'single')\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m invalid syntax\n"
]
}
],
"source": [
"exec compile(source, '__main__', 'single')\n",
"\n",
@ -310,20 +307,9 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" . 23 18 foo\n",
" 23 . 18 foo\n",
" 23 18 . foo\n",
"18 23 18 23 23 . \n"
]
}
],
"outputs": [],
"source": [
"V('23 18 foo')"
]
@ -351,7 +337,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -360,17 +346,9 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[a b c d] e a [f]] [[a e c d]]\n"
]
}
],
"outputs": [],
"source": [
"Ein = '[a b c d] e a [f]' # The terms should be reversed here but I don't realize that until later.\n",
"Eout = '[a e c d]'\n",
@ -381,7 +359,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -390,38 +368,18 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(((a, (b, (c, (d, ())))), (e, (a, ((f, ()), ())))),\n",
" ((a, (e, (c, (d, ())))), ()))"
]
},
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"fi, fo"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[[a1 a2 a3 a4] a5 a6 a7] [[a1 a5 a3 a4]]\n"
]
}
],
"outputs": [],
"source": [
"Ein = '[a1 a2 a3 a4] a5 a6 a7'\n",
"Eout = '[a1 a5 a3 a4]'\n",
@ -432,7 +390,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -441,49 +399,18 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))),\n",
" ((a1, (a5, (a3, (a4, ())))), ()))"
]
},
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"fi, fo"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": null,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"{'a1': a1,\n",
" 'a2': a2,\n",
" 'a3': a3,\n",
" 'a4': a4,\n",
" 'a5': a5,\n",
" 'a6': a6,\n",
" 'a7': a7,\n",
" 's0': s0,\n",
" 's1': s1}"
]
},
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
],
"outputs": [],
"source": [
"def type_vars():\n",
" from joy.library import a1, a2, a3, a4, a5, a6, a7, s0, s1\n",
@ -495,7 +422,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -504,17 +431,9 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(... a7 a6 a5 [a1 a2 a3 a4 ] -- ... [a1 a5 a3 a4 ])\n"
]
}
],
"outputs": [],
"source": [
"stack_effect = reify(tv, (fi, fo))\n",
"print doc_from_stack_effect(*stack_effect)"
@ -522,17 +441,9 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))), ((a1, (a5, (a3, (a4, ())))), ()))\n"
]
}
],
"outputs": [],
"source": [
"print stack_effect"
]
@ -546,7 +457,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -562,17 +473,9 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(a7 a6 a5 [a1 a2 a3 a4 ...1] -- [a1 a5 a3 a4 ...1])\n"
]
}
],
"outputs": [],
"source": [
"print doc_from_stack_effect(*stack_effect)"
]
@ -586,7 +489,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -602,42 +505,18 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])\n"
]
}
],
"outputs": [],
"source": [
"print doc_from_stack_effect(*stack_effect)"
]
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def Ee(stack):\n",
" \"\"\"\n",
" ::\n",
"\n",
" (a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])\n",
"\n",
" \"\"\"\n",
" ((a1, (a2, s1)), (a5, (a6, (a7, s0)))) = stack\n",
" return ((a1, (a5, s1)), s0)\n"
]
}
],
"outputs": [],
"source": [
"source = compile_('Ee', stack_effect)\n",
"print source"
@ -652,7 +531,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -661,42 +540,18 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])\n"
]
}
],
"outputs": [],
"source": [
"print doc_from_stack_effect(*stack_effect)"
]
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def Ee(stack):\n",
" \"\"\"\n",
" ::\n",
"\n",
" ([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])\n",
"\n",
" \"\"\"\n",
" (a7, (a6, (a5, ((a1, (a2, s1)), s0)))) = stack\n",
" return ((a1, (a5, s1)), s0)\n"
]
}
],
"outputs": [],
"source": [
"source = compile_('Ee', stack_effect)\n",
"print source"
@ -715,7 +570,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -725,24 +580,11 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
" . [a b c d] 1 2 [f] Ee\n",
" [a b c d] . 1 2 [f] Ee\n",
" [a b c d] 1 . 2 [f] Ee\n",
" [a b c d] 1 2 . [f] Ee\n",
"[a b c d] 1 2 [f] . Ee\n",
" [a 1 c d] . \n"
]
}
],
"outputs": [],
"source": [
"V('[a b c d] 1 2 [f] Ee')"
]
@ -765,7 +607,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -785,17 +627,9 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(n1 -- n2)\n"
]
}
],
"outputs": [],
"source": [
"stack_effects = infer_string('dup mul')\n",
"for fi, fo in stack_effects:\n",
@ -811,7 +645,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -846,17 +680,9 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(n4 n3 n2 n1 -- n5)\n"
]
}
],
"outputs": [],
"source": [
"stack_effects = infer_string('mul mul sub')\n",
"for fi, fo in stack_effects:\n",
@ -865,7 +691,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -896,17 +722,9 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"(a2 a1 -- a1 a2 a1)\n"
]
}
],
"outputs": [],
"source": [
"stack_effects = infer_string('tuck')\n",
"for fi, fo in stack_effects:\n",
@ -936,7 +754,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -945,7 +763,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -967,7 +785,7 @@
},
{
"cell_type": "code",
"execution_count": 80,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -992,7 +810,7 @@
},
{
"cell_type": "code",
"execution_count": 64,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -1023,7 +841,7 @@
},
{
"cell_type": "code",
"execution_count": 50,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -1092,7 +910,7 @@
},
{
"cell_type": "code",
"execution_count": 69,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@ -1102,18 +920,9 @@
},
{
"cell_type": "code",
"execution_count": 74,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"<ipython-input-74-a6ea700b09d9>:1: SyntaxWarning: import * only allowed at module level\n",
" def import_yin():\n"
]
}
],
"outputs": [],
"source": [
"def import_yin():\n",
" from joy.utils.generated_library import *\n",
@ -1139,44 +948,18 @@
},
{
"cell_type": "code",
"execution_count": 75,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def mul_(stack):\n",
" (a31, (a32, stack)) = stack\n",
" a33 = mul(a32, a31)\n",
" stack = (a33, stack)\n",
" return stack\n",
"\n"
]
}
],
"outputs": [],
"source": [
"print compile_yinyang('mul_', (names(), (names(), (mul, ()))))"
]
},
{
"cell_type": "code",
"execution_count": 76,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def sqr(stack):\n",
" (a34, stack) = stack\n",
" a35 = mul(a34, a34)\n",
" stack = (a35, stack)\n",
" return stack\n",
"\n"
]
}
],
"outputs": [],
"source": [
"e = (names(), (dup, (mul, ())))\n",
"print compile_yinyang('sqr', e)"
@ -1184,25 +967,11 @@
},
{
"cell_type": "code",
"execution_count": 77,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def foo(stack):\n",
" (a36, (a37, stack)) = stack\n",
" a38 = sub(a37, a36)\n",
" a39 = mul(a38, a36)\n",
" stack = (a39, stack)\n",
" return stack\n",
"\n"
]
}
],
"outputs": [],
"source": [
"e = (names(), (dup, (names(), (sub, (mul, ())))))\n",
"print compile_yinyang('foo', e)"
@ -1210,23 +979,9 @@
},
{
"cell_type": "code",
"execution_count": 78,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def bar(stack):\n",
" (a40, (a41, stack)) = stack\n",
" a42 = mul(a41, a40)\n",
" a43 = sub(a42, a42)\n",
" stack = (a43, (a43, stack))\n",
" return stack\n",
"\n"
]
}
],
"outputs": [],
"source": [
"e = (names(), (names(), (mul, (dup, (sub, (dup, ()))))))\n",
"print compile_yinyang('bar', e)"
@ -1234,24 +989,9 @@
},
{
"cell_type": "code",
"execution_count": 79,
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"def to_the_fifth_power(stack):\n",
" (a44, stack) = stack\n",
" a45 = mul(a44, a44)\n",
" a46 = mul(a45, a45)\n",
" a47 = mul(a46, a44)\n",
" stack = (a47, stack)\n",
" return stack\n",
"\n"
]
}
],
"outputs": [],
"source": [
"e = (names(), (dup, (dup, (mul, (dup, (mul, (mul, ())))))))\n",
"print compile_yinyang('to_the_fifth_power', e)"
@ -1295,14 +1035,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.15"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -13,11 +13,11 @@ Given a Joy program like:
V('23 sqr')
```
. 23 sqr
23 . sqr
23 . dup mul
23 23 . mul
529 .
23 sqr
23 sqr
23 dup mul
23 23 mul
529
How would we go about compiling this code (to Python for now)?
@ -47,9 +47,9 @@ D['sqr'] = sqr
V('23 sqr')
```
. 23 sqr
23 . sqr
529 .
23 sqr
23 sqr
529
It's simple to write a function to emit this kind of crude "compiled" code.
@ -76,7 +76,7 @@ def compile_joy_definition(defi):
```python
print compile_joy_definition(old_sqr)
print(compile_joy_definition(old_sqr))
```
def sqr(stack, expression, dictionary):
@ -123,6 +123,19 @@ from joy.library import SimpleFunctionWrapper
```
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-14-d5ef3c7560be> in <module>
----> 1 from joy.utils.types import compile_, doc_from_stack_effect, infer_string
2 from joy.library import SimpleFunctionWrapper
ModuleNotFoundError: No module named 'joy.utils.types'
```python
stack_effects = infer_string('tuck over dup')
```
@ -135,9 +148,6 @@ for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
```
(a2 a1 -- a1 a2 a1 a2 a2)
```python
source = compile_('foo', stack_effects[0])
@ -150,17 +160,6 @@ All Yin functions can be described in Python as a tuple-unpacking (or "-destruct
print source
```
def foo(stack):
"""
::
(a2 a1 -- a1 a2 a1 a2 a2)
"""
(a1, (a2, s1)) = stack
return (a2, (a2, (a1, (a2, (a1, s1)))))
```python
exec compile(source, '__main__', 'single')
@ -169,16 +168,18 @@ D['foo'] = SimpleFunctionWrapper(foo)
```
File "<ipython-input-9-1a7e90bf2d7b>", line 1
exec compile(source, '__main__', 'single')
^
SyntaxError: invalid syntax
```python
V('23 18 foo')
```
. 23 18 foo
23 . 18 foo
23 18 . foo
18 23 18 23 23 .
## Compiling from Stack Effects
There are times when you're deriving a Joy program when you have a stack effect for a Yin function and you need to define it. For example, in the Ordered Binary Trees notebook there is a point where we must derive a function `Ee`:
@ -209,9 +210,6 @@ E = '[%s] [%s]' % (Ein, Eout)
print E
```
[[a b c d] e a [f]] [[a e c d]]
```python
(fi, (fo, _)) = text_to_expression(E)
@ -223,14 +221,6 @@ fi, fo
```
(((a, (b, (c, (d, ())))), (e, (a, ((f, ()), ())))),
((a, (e, (c, (d, ())))), ()))
```python
Ein = '[a1 a2 a3 a4] a5 a6 a7'
Eout = '[a1 a5 a3 a4]'
@ -239,9 +229,6 @@ E = '[%s] [%s]' % (Ein, Eout)
print E
```
[[a1 a2 a3 a4] a5 a6 a7] [[a1 a5 a3 a4]]
```python
(fi, (fo, _)) = text_to_expression(E)
@ -253,14 +240,6 @@ fi, fo
```
(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))),
((a1, (a5, (a3, (a4, ())))), ()))
```python
def type_vars():
from joy.library import a1, a2, a3, a4, a5, a6, a7, s0, s1
@ -271,21 +250,6 @@ tv
```
{'a1': a1,
'a2': a2,
'a3': a3,
'a4': a4,
'a5': a5,
'a6': a6,
'a7': a7,
's0': s0,
's1': s1}
```python
from joy.utils.types import reify
```
@ -296,17 +260,11 @@ stack_effect = reify(tv, (fi, fo))
print doc_from_stack_effect(*stack_effect)
```
(... a7 a6 a5 [a1 a2 a3 a4 ] -- ... [a1 a5 a3 a4 ])
```python
print stack_effect
```
(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))), ((a1, (a5, (a3, (a4, ())))), ()))
Almost, but what we really want is something like this:
@ -321,9 +279,6 @@ Note the change of `()` to `JoyStackType` type variables.
print doc_from_stack_effect(*stack_effect)
```
(a7 a6 a5 [a1 a2 a3 a4 ...1] -- [a1 a5 a3 a4 ...1])
Now we can omit `a3` and `a4` if we like:
@ -338,26 +293,12 @@ The `right` and `left` parts of the ordered binary tree node are subsumed in the
print doc_from_stack_effect(*stack_effect)
```
(a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])
```python
source = compile_('Ee', stack_effect)
print source
```
def Ee(stack):
"""
::
(a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])
"""
((a1, (a2, s1)), (a5, (a6, (a7, s0)))) = stack
return ((a1, (a5, s1)), s0)
Oops! The input stack is backwards...
@ -370,26 +311,12 @@ stack_effect = eval('((a7, (a6, (a5, ((a1, (a2, s1)), s0)))), ((a1, (a5, s1)), s
print doc_from_stack_effect(*stack_effect)
```
([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])
```python
source = compile_('Ee', stack_effect)
print source
```
def Ee(stack):
"""
::
([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])
"""
(a7, (a6, (a5, ((a1, (a2, s1)), s0)))) = stack
return ((a1, (a5, s1)), s0)
Compare:
[key old_value left right] new_value key [Tree-add] Ee
@ -408,14 +335,6 @@ D['Ee'] = SimpleFunctionWrapper(Ee)
V('[a b c d] 1 2 [f] Ee')
```
. [a b c d] 1 2 [f] Ee
[a b c d] . 1 2 [f] Ee
[a b c d] 1 . 2 [f] Ee
[a b c d] 1 2 . [f] Ee
[a b c d] 1 2 [f] . Ee
[a 1 c d] .
```python
@ -444,9 +363,6 @@ for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
```
(n1 -- n2)
Then we would want something like this:
@ -479,9 +395,6 @@ for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
```
(n4 n3 n2 n1 -- n5)
```python
@ -515,9 +428,6 @@ for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
```
(a2 a1 -- a1 a2 a1)
```python
@ -667,10 +577,6 @@ dup = yin_dict['dup']
# return (n, (n, stack)), expression
```
<ipython-input-74-a6ea700b09d9>:1: SyntaxWarning: import * only allowed at module level
def import_yin():
... and there we are.
@ -678,74 +584,30 @@ dup = yin_dict['dup']
print compile_yinyang('mul_', (names(), (names(), (mul, ()))))
```
def mul_(stack):
(a31, (a32, stack)) = stack
a33 = mul(a32, a31)
stack = (a33, stack)
return stack
```python
e = (names(), (dup, (mul, ())))
print compile_yinyang('sqr', e)
```
def sqr(stack):
(a34, stack) = stack
a35 = mul(a34, a34)
stack = (a35, stack)
return stack
```python
e = (names(), (dup, (names(), (sub, (mul, ())))))
print compile_yinyang('foo', e)
```
def foo(stack):
(a36, (a37, stack)) = stack
a38 = sub(a37, a36)
a39 = mul(a38, a36)
stack = (a39, stack)
return stack
```python
e = (names(), (names(), (mul, (dup, (sub, (dup, ()))))))
print compile_yinyang('bar', e)
```
def bar(stack):
(a40, (a41, stack)) = stack
a42 = mul(a41, a40)
a43 = sub(a42, a42)
stack = (a43, (a43, stack))
return stack
```python
e = (names(), (dup, (dup, (mul, (dup, (mul, (mul, ())))))))
print compile_yinyang('to_the_fifth_power', e)
```
def to_the_fifth_power(stack):
(a44, stack) = stack
a45 = mul(a44, a44)
a46 = mul(a45, a45)
a47 = mul(a46, a44)
stack = (a47, stack)
return stack
```python

View File

@ -1,4 +1,4 @@
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import D, J, V, define
@ -11,18 +11,18 @@ Given a Joy program like:
sqr == dup mul
.. code:: ipython2
.. code:: ipython3
V('23 sqr')
.. parsed-literal::
. 23 sqr
23 . sqr
23 . dup mul
23 23 . mul
529 .
23 sqr
23 sqr
23 dup mul
23 23 mul
529
How would we go about compiling this code (to Python for now)?
@ -32,36 +32,36 @@ Naive Call Chaining
The simplest thing would be to compose the functions from the library:
.. code:: ipython2
.. code:: ipython3
dup, mul = D['dup'], D['mul']
.. code:: ipython2
.. code:: ipython3
def sqr(stack, expression, dictionary):
return mul(*dup(stack, expression, dictionary))
.. code:: ipython2
.. code:: ipython3
old_sqr = D['sqr']
D['sqr'] = sqr
.. code:: ipython2
.. code:: ipython3
V('23 sqr')
.. parsed-literal::
. 23 sqr
23 . sqr
529 .
23 sqr
23 sqr
529
Its simple to write a function to emit this kind of crude “compiled”
It's simple to write a function to emit this kind of crude "compiled"
code.
.. code:: ipython2
.. code:: ipython3
def compile_joy(name, expression):
term, expression = expression
@ -80,9 +80,9 @@ code.
return compile_joy(defi.name, defi.body)
.. code:: ipython2
.. code:: ipython3
print compile_joy_definition(old_sqr)
print(compile_joy_definition(old_sqr))
.. parsed-literal::
@ -98,11 +98,11 @@ But what about literals?
quoted == [unit] dip
.. code:: ipython2
.. code:: ipython3
unit, dip = D['unit'], D['dip']
.. code:: ipython2
.. code:: ipython3
# print compile_joy_definition(D['quoted'])
# raises
@ -111,7 +111,7 @@ But what about literals?
For a program like ``foo == bar baz 23 99 baq lerp barp`` we would want
something like:
.. code:: ipython2
.. code:: ipython3
def foo(stack, expression, dictionary):
stack, expression, dictionary = baz(*bar(stack, expression, dictionary))
@ -126,86 +126,84 @@ Compiling Yin Functions
Call-chaining results in code that does too much work. For functions
that operate on stacks and only rearrange values, what I like to call
“Yin Functions”, we can do better.
"Yin Functions", we can do better.
We can infer the stack effects of these functions (or “expressions” or
“programs”) automatically, and the stack effects completely define the
We can infer the stack effects of these functions (or "expressions" or
"programs") automatically, and the stack effects completely define the
semantics of the functions, so we can directly write out a two-line
Python function for them. This is already implemented in the
``joy.utils.types.compile_()`` function.
.. code:: ipython2
.. code:: ipython3
from joy.utils.types import compile_, doc_from_stack_effect, infer_string
from joy.library import SimpleFunctionWrapper
.. code:: ipython2
::
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
<ipython-input-14-d5ef3c7560be> in <module>
----> 1 from joy.utils.types import compile_, doc_from_stack_effect, infer_string
2 from joy.library import SimpleFunctionWrapper
ModuleNotFoundError: No module named 'joy.utils.types'
.. code:: ipython3
stack_effects = infer_string('tuck over dup')
Yin functions have only a single stack effect, they do not branch or
loop.
.. code:: ipython2
.. code:: ipython3
for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
.. parsed-literal::
(a2 a1 -- a1 a2 a1 a2 a2)
.. code:: ipython2
.. code:: ipython3
source = compile_('foo', stack_effects[0])
All Yin functions can be described in Python as a tuple-unpacking (or
“-destructuring”) of the stack datastructure followed by building up the
"-destructuring") of the stack datastructure followed by building up the
new stack structure.
.. code:: ipython2
.. code:: ipython3
print source
.. parsed-literal::
def foo(stack):
"""
::
(a2 a1 -- a1 a2 a1 a2 a2)
"""
(a1, (a2, s1)) = stack
return (a2, (a2, (a1, (a2, (a1, s1)))))
.. code:: ipython2
.. code:: ipython3
exec compile(source, '__main__', 'single')
D['foo'] = SimpleFunctionWrapper(foo)
.. code:: ipython2
::
File "<ipython-input-9-1a7e90bf2d7b>", line 1
exec compile(source, '__main__', 'single')
^
SyntaxError: invalid syntax
.. code:: ipython3
V('23 18 foo')
.. parsed-literal::
. 23 18 foo
23 . 18 foo
23 18 . foo
18 23 18 23 23 .
Compiling from Stack Effects
----------------------------
There are times when youre deriving a Joy program when you have a stack
There are times when you're deriving a Joy program when you have a stack
effect for a Yin function and you need to define it. For example, in the
Ordered Binary Trees notebook there is a point where we must derive a
function ``Ee``:
@ -226,14 +224,14 @@ stack effect:
--------------------------
[a e c d]
(I havent yet implemented a simple interface for this yet. What follow
(I haven't yet implemented a simple interface for this yet. What follow
is an exploration of how to do it.)
.. code:: ipython2
.. code:: ipython3
from joy.parser import text_to_expression
.. code:: ipython2
.. code:: ipython3
Ein = '[a b c d] e a [f]' # The terms should be reversed here but I don't realize that until later.
Eout = '[a e c d]'
@ -241,31 +239,15 @@ is an exploration of how to do it.)
print E
.. parsed-literal::
[[a b c d] e a [f]] [[a e c d]]
.. code:: ipython2
.. code:: ipython3
(fi, (fo, _)) = text_to_expression(E)
.. code:: ipython2
.. code:: ipython3
fi, fo
.. parsed-literal::
(((a, (b, (c, (d, ())))), (e, (a, ((f, ()), ())))),
((a, (e, (c, (d, ())))), ()))
.. code:: ipython2
.. code:: ipython3
Ein = '[a1 a2 a3 a4] a5 a6 a7'
Eout = '[a1 a5 a3 a4]'
@ -273,31 +255,15 @@ is an exploration of how to do it.)
print E
.. parsed-literal::
[[a1 a2 a3 a4] a5 a6 a7] [[a1 a5 a3 a4]]
.. code:: ipython2
.. code:: ipython3
(fi, (fo, _)) = text_to_expression(E)
.. code:: ipython2
.. code:: ipython3
fi, fo
.. parsed-literal::
(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))),
((a1, (a5, (a3, (a4, ())))), ()))
.. code:: ipython2
.. code:: ipython3
def type_vars():
from joy.library import a1, a2, a3, a4, a5, a6, a7, s0, s1
@ -306,139 +272,64 @@ is an exploration of how to do it.)
tv = type_vars()
tv
.. parsed-literal::
{'a1': a1,
'a2': a2,
'a3': a3,
'a4': a4,
'a5': a5,
'a6': a6,
'a7': a7,
's0': s0,
's1': s1}
.. code:: ipython2
.. code:: ipython3
from joy.utils.types import reify
.. code:: ipython2
.. code:: ipython3
stack_effect = reify(tv, (fi, fo))
print doc_from_stack_effect(*stack_effect)
.. parsed-literal::
(... a7 a6 a5 [a1 a2 a3 a4 ] -- ... [a1 a5 a3 a4 ])
.. code:: ipython2
.. code:: ipython3
print stack_effect
.. parsed-literal::
(((a1, (a2, (a3, (a4, ())))), (a5, (a6, (a7, ())))), ((a1, (a5, (a3, (a4, ())))), ()))
Almost, but what we really want is something like this:
.. code:: ipython2
.. code:: ipython3
stack_effect = eval('(((a1, (a2, (a3, (a4, s1)))), (a5, (a6, (a7, s0)))), ((a1, (a5, (a3, (a4, s1)))), s0))', tv)
Note the change of ``()`` to ``JoyStackType`` type variables.
.. code:: ipython2
.. code:: ipython3
print doc_from_stack_effect(*stack_effect)
.. parsed-literal::
(a7 a6 a5 [a1 a2 a3 a4 ...1] -- [a1 a5 a3 a4 ...1])
Now we can omit ``a3`` and ``a4`` if we like:
.. code:: ipython2
.. code:: ipython3
stack_effect = eval('(((a1, (a2, s1)), (a5, (a6, (a7, s0)))), ((a1, (a5, s1)), s0))', tv)
The ``right`` and ``left`` parts of the ordered binary tree node are
subsumed in the tail of the nodes stack/list.
subsumed in the tail of the node's stack/list.
.. code:: ipython2
.. code:: ipython3
print doc_from_stack_effect(*stack_effect)
.. parsed-literal::
(a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])
.. code:: ipython2
.. code:: ipython3
source = compile_('Ee', stack_effect)
print source
Oops! The input stack is backwards...
.. parsed-literal::
def Ee(stack):
"""
::
(a7 a6 a5 [a1 a2 ...1] -- [a1 a5 ...1])
"""
((a1, (a2, s1)), (a5, (a6, (a7, s0)))) = stack
return ((a1, (a5, s1)), s0)
Oops! The input stack is backwards…
.. code:: ipython2
.. code:: ipython3
stack_effect = eval('((a7, (a6, (a5, ((a1, (a2, s1)), s0)))), ((a1, (a5, s1)), s0))', tv)
.. code:: ipython2
.. code:: ipython3
print doc_from_stack_effect(*stack_effect)
.. parsed-literal::
([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])
.. code:: ipython2
.. code:: ipython3
source = compile_('Ee', stack_effect)
print source
.. parsed-literal::
def Ee(stack):
"""
::
([a1 a2 ...1] a5 a6 a7 -- [a1 a5 ...1])
"""
(a7, (a6, (a5, ((a1, (a2, s1)), s0)))) = stack
return ((a1, (a5, s1)), s0)
Compare:
::
@ -447,33 +338,22 @@ Compare:
------------------------------------------------------------
[key new_value left right]
.. code:: ipython2
.. code:: ipython3
eval(compile(source, '__main__', 'single'))
D['Ee'] = SimpleFunctionWrapper(Ee)
.. code:: ipython2
.. code:: ipython3
V('[a b c d] 1 2 [f] Ee')
.. parsed-literal::
. [a b c d] 1 2 [f] Ee
[a b c d] . 1 2 [f] Ee
[a b c d] 1 . 2 [f] Ee
[a b c d] 1 2 . [f] Ee
[a b c d] 1 2 [f] . Ee
[a 1 c d] .
Working with Yang Functions
---------------------------
Consider the compiled code of ``dup``:
.. code:: ipython2
.. code:: ipython3
def dup(stack):
@ -484,21 +364,15 @@ Consider the compiled code of ``dup``:
To compile ``sqr == dup mul`` we can compute the stack effect:
.. code:: ipython2
.. code:: ipython3
stack_effects = infer_string('dup mul')
for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
.. parsed-literal::
(n1 -- n2)
Then we would want something like this:
.. code:: ipython2
.. code:: ipython3
def sqr(stack):
@ -510,21 +384,15 @@ Then we would want something like this:
How about
How about...
.. code:: ipython2
.. code:: ipython3
stack_effects = infer_string('mul mul sub')
for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
.. parsed-literal::
(n4 n3 n2 n1 -- n5)
.. code:: ipython2
.. code:: ipython3
def foo(stack):
@ -545,30 +413,24 @@ How about…
.. code:: ipython2
.. code:: ipython3
stack_effects = infer_string('tuck')
for fi, fo in stack_effects:
print doc_from_stack_effect(fi, fo)
.. parsed-literal::
(a2 a1 -- a1 a2 a1)
Compiling Yin~Yang Functions
----------------------------
First, we need a source of Python identifiers. Im going to reuse
First, we need a source of Python identifiers. I'm going to reuse
``Symbol`` class for this.
.. code:: ipython2
.. code:: ipython3
from joy.parser import Symbol
.. code:: ipython2
.. code:: ipython3
def _names():
n = 0
@ -579,9 +441,9 @@ First, we need a source of Python identifiers. Im going to reuse
names = _names().next
Now we need an object that represents a Yang function that accepts two
args and return one result (well implement other kinds a little later.)
args and return one result (we'll implement other kinds a little later.)
.. code:: ipython2
.. code:: ipython3
class Foo(object):
@ -594,10 +456,10 @@ args and return one result (well implement other kinds a little later.)
code.append(('call', out, self.name, (in0, in1)))
return (out, stack), expression, code
A crude “interpreter” that translates expressions of args and Yin and
A crude "interpreter" that translates expressions of args and Yin and
Yang functions into a kind of simple dataflow graph.
.. code:: ipython2
.. code:: ipython3
def I(stack, expression, code):
while expression:
@ -618,7 +480,7 @@ Yang functions into a kind of simple dataflow graph.
Something to convert the graph into Python code.
.. code:: ipython2
.. code:: ipython3
strtup = lambda a, b: '(%s, %s)' % (b, a)
strstk = lambda rest: reduce(strtup, rest, 'stack')
@ -676,14 +538,14 @@ Something to convert the graph into Python code.
''' % (name, code_gen(I((), expression, [])))
A few functions to try it with
A few functions to try it with...
.. code:: ipython2
.. code:: ipython3
mul = Foo('mul')
sub = Foo('sub')
.. code:: ipython2
.. code:: ipython3
def import_yin():
from joy.utils.generated_library import *
@ -699,98 +561,32 @@ A few functions to try it with…
# n, stack = stack
# return (n, (n, stack)), expression
... and there we are.
.. parsed-literal::
<ipython-input-74-a6ea700b09d9>:1: SyntaxWarning: import * only allowed at module level
def import_yin():
… and there we are.
.. code:: ipython2
.. code:: ipython3
print compile_yinyang('mul_', (names(), (names(), (mul, ()))))
.. parsed-literal::
def mul_(stack):
(a31, (a32, stack)) = stack
a33 = mul(a32, a31)
stack = (a33, stack)
return stack
.. code:: ipython2
.. code:: ipython3
e = (names(), (dup, (mul, ())))
print compile_yinyang('sqr', e)
.. parsed-literal::
def sqr(stack):
(a34, stack) = stack
a35 = mul(a34, a34)
stack = (a35, stack)
return stack
.. code:: ipython2
.. code:: ipython3
e = (names(), (dup, (names(), (sub, (mul, ())))))
print compile_yinyang('foo', e)
.. parsed-literal::
def foo(stack):
(a36, (a37, stack)) = stack
a38 = sub(a37, a36)
a39 = mul(a38, a36)
stack = (a39, stack)
return stack
.. code:: ipython2
.. code:: ipython3
e = (names(), (names(), (mul, (dup, (sub, (dup, ()))))))
print compile_yinyang('bar', e)
.. parsed-literal::
def bar(stack):
(a40, (a41, stack)) = stack
a42 = mul(a41, a40)
a43 = sub(a42, a42)
stack = (a43, (a43, stack))
return stack
.. code:: ipython2
.. code:: ipython3
e = (names(), (dup, (dup, (mul, (dup, (mul, (mul, ())))))))
print compile_yinyang('to_the_fifth_power', e)
.. parsed-literal::
def to_the_fifth_power(stack):
(a44, stack) = stack
a45 = mul(a44, a44)
a46 = mul(a45, a45)
a47 = mul(a46, a44)
stack = (a47, stack)
return stack

View File

@ -13088,7 +13088,7 @@ div#notebook {
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13167,7 +13167,7 @@ n+1 2 /
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13180,7 +13180,7 @@ n+1 2 /
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13219,7 +13219,7 @@ n+1 2 /
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra 6 [x popd] times first sqr&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra 6 [x popd] times first sqr&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13285,7 +13285,7 @@ abs(a-b) ε &lt;=
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_P == [first - abs] dip &lt;=&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_P [first - abs] dip &lt;=&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13310,7 +13310,7 @@ abs(a-b) ε &lt;=
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_B == roll&lt; popop first&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_B roll&lt; popop first&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13328,7 +13328,7 @@ abs(a-b) ε &lt;=
<ol>
<li>Discard a.</li>
<li>Use <code>x</code> combinator to generate next term from <code>G</code>.</li>
<li>Run <code>within</code> with <code>i</code> (it is a <code>primrec</code> function.)</li>
<li>Run <code>within</code> with <code>i</code> (it is a "tail-recursive" function.)</li>
</ol>
<p>Pretty straightforward:</p>
@ -13349,7 +13349,7 @@ b [c G] ε within</code></pre>
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_R == [popd x] dip&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_R [popd x] dip&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13373,8 +13373,8 @@ a [b G] ε ...</code></pre>
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec&#39;</span><span class="p">)</span>
<span class="n">define</span><span class="p">(</span><span class="s1">&#39;sqrt == gsra within&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec&#39;</span><span class="p">)</span>
<span class="n">define</span><span class="p">(</span><span class="s1">&#39;sqrt gsra within&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13395,7 +13395,7 @@ a [b G] ε ...</code></pre>
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;36 sqrt&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;36 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13426,7 +13426,7 @@ a [b G] ε ...</code></pre>
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 sqrt&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13465,7 +13465,7 @@ a [b G] ε ...</code></pre>
<div class="prompt input_prompt">In&nbsp;[11]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="mf">4.795831523312719</span><span class="o">**</span><span class="mi">2</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="mf">4.795831523312719</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
@ -13498,7 +13498,7 @@ a [b G] ε ...</code></pre>
<div class="prompt input_prompt">In&nbsp;[12]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">sqrt</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="kn">import</span> <span class="n">sqrt</span>
<span class="n">sqrt</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</pre></div>

View File

@ -55,7 +55,7 @@ The generator can be written as:
```python
define('gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
define('gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
```
@ -109,7 +109,7 @@ Using the _output_ `[a G]` of the above generator for square root approximations
```python
define('_within_P == [first - abs] dip <=')
define('_within_P [first - abs] dip <=')
```
### Base-Case
@ -121,7 +121,7 @@ define('_within_P == [first - abs] dip <=')
```python
define('_within_B == roll< popop first')
define('_within_B roll< popop first')
```
### Recur
@ -130,7 +130,7 @@ define('_within_B == roll< popop first')
1. Discard a.
2. Use `x` combinator to generate next term from `G`.
3. Run `within` with `i` (it is a `primrec` function.)
3. Run `within` with `i` (it is a "tail-recursive" function.)
Pretty straightforward:
@ -145,7 +145,7 @@ Pretty straightforward:
```python
define('_within_R == [popd x] dip')
define('_within_R [popd x] dip')
```
### Setting up
@ -157,8 +157,8 @@ The recursive function we have defined so far needs a slight preamble: `x` to pr
```python
define('within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec')
define('sqrt == gsra within')
define('within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec')
define('sqrt gsra within')
```
Try it out...

View File

@ -1,13 +1,13 @@
`Newtons method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
`Newton's method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
=====================================================================
Lets use the Newton-Raphson method for finding the root of an equation
Let's use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.
Cf. `“Why Functional Programming Matters” by John
Cf. `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
@ -75,11 +75,11 @@ The generator can be written as:
1 [23 over / + 2 /] [dup] swoncat make_generator
1 [dup 23 over / + 2 /] make_generator
.. code:: ipython2
.. code:: ipython3
define('gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
define('gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
.. code:: ipython2
.. code:: ipython3
J('23 gsra')
@ -89,10 +89,10 @@ The generator can be written as:
[1 [dup 23 over / + 2 /] codireco]
Lets drive the generator a few time (with the ``x`` combinator) and
square the approximation to see how well it works
Let's drive the generator a few time (with the ``x`` combinator) and
square the approximation to see how well it works...
.. code:: ipython2
.. code:: ipython3
J('23 gsra 6 [x popd] times first sqr')
@ -105,7 +105,7 @@ square the approximation to see how well it works…
Finding Consecutive Approximations within a Tolerance
-----------------------------------------------------
From `“Why Functional Programming Matters” by John
From `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
The remainder of a square root finder is a function *within*, which
@ -117,7 +117,7 @@ Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
Using the *output* ``[a G]`` of the above generator for square root
approximations, and further assuming that the first term a has been
generated already and epsilon ε is handy on the stack
generated already and epsilon ε is handy on the stack...
::
@ -142,9 +142,9 @@ Predicate
abs(a-b) ε <=
(abs(a-b)<=ε)
.. code:: ipython2
.. code:: ipython3
define('_within_P == [first - abs] dip <=')
define('_within_P [first - abs] dip <=')
Base-Case
~~~~~~~~~
@ -156,9 +156,9 @@ Base-Case
[b G] first
b
.. code:: ipython2
.. code:: ipython3
define('_within_B == roll< popop first')
define('_within_B roll< popop first')
Recur
~~~~~
@ -169,7 +169,7 @@ Recur
1. Discard a.
2. Use ``x`` combinator to generate next term from ``G``.
3. Run ``within`` with ``i`` (it is a ``primrec`` function.)
3. Run ``within`` with ``i`` (it is a "tail-recursive" function.)
Pretty straightforward:
@ -184,9 +184,9 @@ Pretty straightforward:
b [c G] ε within
.. code:: ipython2
.. code:: ipython3
define('_within_R == [popd x] dip')
define('_within_R [popd x] dip')
Setting up
~~~~~~~~~~
@ -199,14 +199,14 @@ The recursive function we have defined so far needs a slight preamble:
[a G] x ε ...
a [b G] ε ...
.. code:: ipython2
.. code:: ipython3
define('within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec')
define('sqrt == gsra within')
define('within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec')
define('sqrt gsra within')
Try it out
Try it out...
.. code:: ipython2
.. code:: ipython3
J('36 sqrt')
@ -216,7 +216,7 @@ Try it out…
6.0
.. code:: ipython2
.. code:: ipython3
J('23 sqrt')
@ -228,7 +228,7 @@ Try it out…
Check it.
.. code:: ipython2
.. code:: ipython3
4.795831523312719**2
@ -241,7 +241,7 @@ Check it.
.. code:: ipython2
.. code:: ipython3
from math import sqrt

View File

@ -13076,10 +13076,10 @@ div#notebook {
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="prompt input_prompt">In&nbsp;[1]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">DefinitionWrapper</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="kn">import</span> <span class="n">D</span><span class="p">,</span> <span class="n">DefinitionWrapper</span><span class="p">,</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
@ -13234,10 +13234,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="prompt input_prompt">In&nbsp;[2]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [pop []] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [pop []] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13273,10 +13273,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="prompt input_prompt">In&nbsp;[3]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [[]] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [[]] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13312,10 +13312,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="prompt input_prompt">In&nbsp;[4]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3] [size 1 &lt;=] [] [[[+] infra] dupdip first] [dip swons] genrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13366,10 +13366,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="prompt input_prompt">In&nbsp;[5]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;scan == [infra] cons [dupdip first] cons [size 1 &lt;=] [] roll&lt; [dip swons] genrec&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;scan [infra] cons [dupdip first] cons [size 1 &lt;=] [] roll&lt; [dip swons] genrec&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13379,10 +13379,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="prompt input_prompt">In&nbsp;[6]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] [+] scan&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] [+] scan&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13410,10 +13410,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="prompt input_prompt">In&nbsp;[7]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] [*] scan&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4] [*] scan&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13441,10 +13441,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="prompt input_prompt">In&nbsp;[8]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4 5 6 7] [neg +] scan&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[1 2 3 4 5 6 7] [neg +] scan&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13485,10 +13485,10 @@ scan == [size 1 &lt;=] [pop []] [[[F] infra] dupdip first] [dip swons] genrec</c
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[10]:</div>
<div class="prompt input_prompt">In&nbsp;[9]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[&quot;hello&quot; &quot;world&quot;] uncons [&quot;</span><span class="se">\n</span><span class="s1">&quot; swap + +] step&#39;</span><span class="p">)</span>
<div class=" highlight hl-ipython3"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;[&quot;hello&quot; &quot;world&quot;] uncons [&quot;</span><span class="se">\n</span><span class="s1">&quot; swap + +] step&#39;</span><span class="p">)</span>
</pre></div>
</div>
@ -13592,19 +13592,6 @@ Lines == ["\n" in] [unit] [split-at-newline swap] [dip swons] genrec</code></pre
</div>
</div>
</div>
<div class="cell border-box-sizing code_cell rendered">
<div class="input">
<div class="prompt input_prompt">In&nbsp;[&nbsp;]:</div>
<div class="inner_cell">
<div class="input_area">
<div class=" highlight hl-ipython2"><pre><span></span>
</pre></div>
</div>
</div>
</div>
</div>
</div>
</div>

View File

@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@ -155,7 +155,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {
"scrolled": false
},
@ -181,7 +181,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 3,
"metadata": {
"scrolled": false
},
@ -207,7 +207,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {
"scrolled": false
},
@ -247,16 +247,16 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
"define('scan == [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')"
"define('scan [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')"
]
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@ -273,7 +273,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@ -290,7 +290,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@ -322,7 +322,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@ -410,13 +410,6 @@
"To limit `F` to working on pairs of terms from its domain.\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
@ -428,14 +421,14 @@
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 2
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython2",
"version": "2.7.12"
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,

View File

@ -137,7 +137,7 @@ And so:
```python
define('scan == [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')
define('scan [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')
```
@ -232,8 +232,3 @@ Or:
To limit `F` to working on pairs of terms from its domain.
```python
```

View File

@ -1,8 +1,8 @@
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import D, DefinitionWrapper, J, V, define
On “Two Exercises Found in a Book on Algorithmics”
On "Two Exercises Found in a Book on Algorithmics"
==================================================
Bird & Meertens
@ -14,8 +14,8 @@ Define ``scan`` in terms of a reduction.
----------------------------------------
Problem I. The reduction operator ``/`` of APL takes some binary
operator ```` on its left and a vector ``x`` of values on its right.
The meaning of ``⨁/x`` for ``x = [a b ... z]`` is the value
operator ```` on its left and a vector ``x`` of values on its
right. The meaning of ``⨁/x`` for ``x = [a b ... z]`` is the value
``a⨁b⨁...⨁z``. For this to be well-defined in the absence of
brackets, the operation ```` has to be associative. Now there is
another operator ``\`` of APL called ``scan``. Its effect is closely
@ -25,10 +25,8 @@ Define ``scan`` in terms of a reduction.
⨁\x = [a a⨁b a⨁b⨁c ... a⨁b⨁...⨁z]
..
The problem is to find some definition of ``scan`` as a reduction. In
other words, we have to find some function ``f`` and an operator
The problem is to find some definition of ``scan`` as a reduction.
In other words, we have to find some function ``f`` and an operator
```` so that
::
@ -75,7 +73,7 @@ instead of two (the b is instead the duplicate of a.)
Initial Definition
~~~~~~~~~~~~~~~~~~
Were building a list of values so this is an “anamorphism”. (An
We're building a list of values so this is an "anamorphism". (An
anamorphism uses ``[]`` for ``c`` and ``swons`` for ``F``.)
::
@ -88,7 +86,7 @@ Convert to ``ifte``:
scan == [P] [pop []] [[G] dupdip [scan] dip swons] ifte
On the recursive branch ``[G] dupdip`` doesnt cut it:
On the recursive branch ``[G] dupdip`` doesn't cut it:
::
@ -132,7 +130,7 @@ less that two items in them:
P == size 1 <=
Lets see what weve got so far:
Let's see what we've got so far:
::
@ -144,7 +142,7 @@ Handling the Last Term
This works to a point, but it throws away the last term:
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] [size 1 <=] [pop []] [[[+] infra] dupdip first] [dip swons] genrec')
@ -154,9 +152,9 @@ This works to a point, but it throws away the last term:
[1 3]
Hmm… Lets take out the ``pop`` for a sec…
Hmm... Let's take out the ``pop`` for a sec...
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] [size 1 <=] [[]] [[[+] infra] dupdip first] [dip swons] genrec')
@ -167,11 +165,11 @@ Hmm… Lets take out the ``pop`` for a sec…
That leaves the last item in our list, then it puts an empty list on the
stack and ``swons``\ s the new terms onto that. If we leave out that
empty list, they will be ``swons``\ d onto that list that already has
the last item.
stack and ``swons``'s the new terms onto that. If we leave out that
empty list, they will be ``swons``'d onto that list that already has the
last item.
.. code:: ipython2
.. code:: ipython3
J('[1 2 3] [size 1 <=] [] [[[+] infra] dupdip first] [dip swons] genrec')
@ -205,11 +203,11 @@ And so:
scan == [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec
.. code:: ipython2
.. code:: ipython3
define('scan == [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')
define('scan [infra] cons [dupdip first] cons [size 1 <=] [] roll< [dip swons] genrec')
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4] [+] scan')
@ -219,7 +217,7 @@ And so:
[1 3 6 10]
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4] [*] scan')
@ -229,7 +227,7 @@ And so:
[1 2 6 24]
.. code:: ipython2
.. code:: ipython3
J('[1 2 3 4 5 6 7] [neg +] scan')
@ -259,7 +257,7 @@ Problem 2.
Unlines = uncons ['\n' swap + +] step
.. code:: ipython2
.. code:: ipython3
J('["hello" "world"] uncons ["\n" swap + +] step')
@ -269,7 +267,7 @@ Problem 2.
'hello\nworld'
Again ignoring the actual task lets just derive ``Lines``:
Again ignoring the actual task let's just derive ``Lines``:
::
@ -315,10 +313,10 @@ properties are discussed. Am I missing the point?
0 [a b c d] [F] step == 0 [a b] [F] step 0 [c d] [F] step concat
For associative function ``F`` and a “unit” element for that function,
For associative function ``F`` and a "unit" element for that function,
here represented by ``0``.
For functions that dont have a “unit” we can fake it (the example is
For functions that don't have a "unit" we can fake it (the example is
given of infinity for the ``min(a, b)`` function.) We can also use:
::
@ -336,4 +334,3 @@ Or:
a [b c] [F] step
To limit ``F`` to working on pairs of terms from its domain.

View File

@ -58,7 +58,7 @@
<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">__future__</span> <span class="k">import</span> <span class="n">print_function</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="k">import</span> <span class="nb">input</span>
<span class="kn">from</span> <span class="nn">traceback</span> <span class="k">import</span> <span class="n">print_exc</span><span class="p">,</span> <span class="n">format_exc</span>
<span class="kn">from</span> <span class="nn">traceback</span> <span class="k">import</span> <span class="n">print_exc</span>
<span class="kn">from</span> <span class="nn">.parser</span> <span class="k">import</span> <span class="n">text_to_expression</span><span class="p">,</span> <span class="n">ParseError</span><span class="p">,</span> <span class="n">Symbol</span>
<span class="kn">from</span> <span class="nn">.utils.stack</span> <span class="k">import</span> <span class="n">stack_to_string</span>
@ -136,8 +136,7 @@
<span class="k">try</span><span class="p">:</span>
<span class="n">stack</span><span class="p">,</span> <span class="n">_</span><span class="p">,</span> <span class="n">dictionary</span> <span class="o">=</span> <span class="n">run</span><span class="p">(</span><span class="n">text</span><span class="p">,</span> <span class="n">stack</span><span class="p">,</span> <span class="n">dictionary</span><span class="p">)</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">exc</span> <span class="o">=</span> <span class="n">format_exc</span><span class="p">()</span> <span class="c1"># Capture the exception.</span>
<span class="nb">print</span><span class="p">(</span><span class="n">exc</span><span class="p">)</span> <span class="c1"># Print the original exception.</span>
<span class="n">print_exc</span><span class="p">()</span>
<span class="k">except</span><span class="p">:</span>
<span class="n">print_exc</span><span class="p">()</span>
<span class="nb">print</span><span class="p">()</span>

View File

@ -56,12 +56,9 @@
<span class="sd">returns a dictionary of Joy functions suitable for use with the joy()</span>
<span class="sd">function.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="kn">from</span> <span class="nn">builtins</span> <span class="k">import</span> <span class="nb">map</span><span class="p">,</span> <span class="nb">object</span><span class="p">,</span> <span class="nb">range</span><span class="p">,</span> <span class="nb">zip</span>
<span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getdoc</span>
<span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getdoc</span><span class="p">,</span> <span class="n">getmembers</span><span class="p">,</span> <span class="n">isfunction</span>
<span class="kn">from</span> <span class="nn">functools</span> <span class="k">import</span> <span class="n">wraps</span>
<span class="kn">from</span> <span class="nn">itertools</span> <span class="k">import</span> <span class="n">count</span>
<span class="kn">from</span> <span class="nn">inspect</span> <span class="k">import</span> <span class="n">getmembers</span><span class="p">,</span> <span class="n">isfunction</span>
<span class="kn">import</span> <span class="nn">operator</span><span class="o">,</span> <span class="nn">math</span>
<span class="kn">from</span> <span class="nn">.parser</span> <span class="k">import</span> <span class="n">text_to_expression</span><span class="p">,</span> <span class="n">Symbol</span>
@ -325,7 +322,7 @@
<span class="k">return</span> <span class="p">(</span>
<span class="n">line</span><span class="o">.</span><span class="n">strip</span><span class="p">()</span>
<span class="k">for</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">text</span><span class="o">.</span><span class="n">splitlines</span><span class="p">()</span>
<span class="k">if</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)</span>
<span class="k">if</span> <span class="n">line</span> <span class="ow">and</span> <span class="ow">not</span> <span class="n">line</span><span class="o">.</span><span class="n">startswith</span><span class="p">(</span><span class="s1">&#39;#&#39;</span><span class="p">)</span>
<span class="p">)</span>
@ -512,9 +509,12 @@
<div class="viewcode-block" id="sum_"><a class="viewcode-back" href="../../library.html#joy.library.sum_">[docs]</a><span class="nd">@inscribe</span>
<span class="nd">@SimpleFunctionWrapper</span>
<span class="k">def</span> <span class="nf">sum_</span><span class="p">(</span><span class="n">S</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;Given a quoted sequence of numbers return the sum.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Given a quoted sequence of numbers return the sum.</span>
<span class="sd"> ::</span>
<span class="sd"> sum == 0 swap [+] step</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="n">tos</span><span class="p">,</span> <span class="n">stack</span> <span class="o">=</span> <span class="n">S</span>
<span class="k">return</span> <span class="nb">sum</span><span class="p">(</span><span class="n">iter_stack</span><span class="p">(</span><span class="n">tos</span><span class="p">)),</span> <span class="n">stack</span></div>
@ -584,7 +584,8 @@
<div class="viewcode-block" id="reverse"><a class="viewcode-back" href="../../library.html#joy.library.reverse">[docs]</a><span class="nd">@inscribe</span>
<span class="nd">@SimpleFunctionWrapper</span>
<span class="k">def</span> <span class="nf">reverse</span><span class="p">(</span><span class="n">S</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;Reverse the list on the top of the stack.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Reverse the list on the top of the stack.</span>
<span class="sd"> ::</span>
<span class="sd"> reverse == [] swap shunt</span>
@ -599,7 +600,8 @@
<div class="viewcode-block" id="concat_"><a class="viewcode-back" href="../../library.html#joy.library.concat_">[docs]</a><span class="nd">@inscribe</span>
<span class="nd">@SimpleFunctionWrapper</span>
<span class="k">def</span> <span class="nf">concat_</span><span class="p">(</span><span class="n">S</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;Concatinate the two lists on the top of the stack.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Concatinate the two lists on the top of the stack.</span>
<span class="sd"> ::</span>
<span class="sd"> [a b c] [d e f] concat</span>
@ -614,7 +616,8 @@
<div class="viewcode-block" id="shunt"><a class="viewcode-back" href="../../library.html#joy.library.shunt">[docs]</a><span class="nd">@inscribe</span>
<span class="nd">@SimpleFunctionWrapper</span>
<span class="k">def</span> <span class="nf">shunt</span><span class="p">(</span><span class="n">stack</span><span class="p">):</span>
<span class="sd">&#39;&#39;&#39;Like concat but reverses the top list into the second.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd"> Like concat but reverses the top list into the second.</span>
<span class="sd"> ::</span>
<span class="sd"> shunt == [swons] step == reverse swap concat</span>
@ -1018,7 +1021,7 @@
<span class="sd"> the data parameter is zero, then the first quotation has to produce</span>
<span class="sd"> the value to be returned. If the data parameter is positive then the</span>
<span class="sd"> second has to combine the data parameter with the result of applying</span>
<span class="sd"> the function to its predecessor.</span>
<span class="sd"> the function to its predecessor.::</span>
<span class="sd"> 5 [1] [*] primrec</span>
@ -1028,7 +1031,7 @@
<span class="sd"> Otherwise it pushes a decremented copy of the top element and</span>
<span class="sd"> recurses. On the way back from the recursion it uses the other</span>
<span class="sd"> quotation, [*], to multiply what is now a factorial on top of the</span>
<span class="sd"> stack by the second element on the stack.</span>
<span class="sd"> stack by the second element on the stack.::</span>
<span class="sd"> n [Base] [Recur] primrec</span>
@ -1222,6 +1225,7 @@
<span class="sd"> ... x [Q] . app1</span>
<span class="sd"> -----------------------------------</span>
<span class="sd"> ... [x ...] [Q] . infra first</span>
<span class="sd"> &#39;&#39;&#39;</span>
<span class="p">(</span><span class="n">quote</span><span class="p">,</span> <span class="p">(</span><span class="n">x</span><span class="p">,</span> <span class="n">stack</span><span class="p">))</span> <span class="o">=</span> <span class="n">S</span>
<span class="n">stack</span> <span class="o">=</span> <span class="p">(</span><span class="n">quote</span><span class="p">,</span> <span class="p">((</span><span class="n">x</span><span class="p">,</span> <span class="n">stack</span><span class="p">),</span> <span class="n">stack</span><span class="p">))</span>
@ -1319,12 +1323,12 @@
<span class="sd"> ... 1 [Q] . times</span>
<span class="sd"> ---------------------------------</span>
<span class="sd"> -----------------------</span>
<span class="sd"> ... . Q</span>
<span class="sd"> ... n [Q] . times</span>
<span class="sd"> --------------------------------- w/ n &gt; 1</span>
<span class="sd"> ------------------------------------- w/ n &gt; 1</span>
<span class="sd"> ... . Q (n - 1) [Q] times</span>
<span class="sd"> &#39;&#39;&#39;</span>

View File

@ -53,22 +53,21 @@
<span class="sd">&#39;&#39;&#39;</span>
<span class="sd">Pretty printing support, e.g.::</span>
<span class="sd"> Joy? 23 18 * 99 +</span>
<span class="sd"> . 23 18 mul 99 add</span>
<span class="sd"> 23 . 18 mul 99 add</span>
<span class="sd"> 23 18 . mul 99 add</span>
<span class="sd"> 414 . 99 add</span>
<span class="sd"> 414 99 . add</span>
<span class="sd"> 513 . </span>
<span class="sd"> Joy? [23 18 * 99 +] trace</span>
<span class="sd"> 23 18 mul 99 add</span>
<span class="sd"> 23 18 mul 99 add</span>
<span class="sd"> 23 18 mul 99 add</span>
<span class="sd"> 414 99 add</span>
<span class="sd"> 414 99 add</span>
<span class="sd"> 513 </span>
<span class="sd"> 513 &lt;-top</span>
<span class="sd"> joy? </span>
<span class="sd">On each line the stack is printed with the top to the right, then a ``.`` to</span>
<span class="sd">represent the current locus of processing, then the pending expression to the</span>
<span class="sd">left.</span>
<span class="sd">On each line the stack is printed with the top to the left, then a</span>
<span class="sd">bullet symbol,``•``, to represent the current locus of processing, then</span>
<span class="sd">the pending expression to the right.</span>
<span class="sd">&#39;&#39;&#39;</span>
<span class="c1"># (Kinda clunky and hacky. This should be swapped out in favor of much</span>
<span class="c1"># smarter stuff.)</span>
@ -147,8 +146,8 @@
<span class="n">n</span> <span class="o">=</span> <span class="nb">len</span><span class="p">(</span><span class="n">stack</span><span class="p">)</span>
<span class="k">if</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="n">max_stack_length</span><span class="p">:</span>
<span class="n">max_stack_length</span> <span class="o">=</span> <span class="n">n</span>
<span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> . </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</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="k">return</span> <span class="p">[</span> <span class="c1"># Prefix spaces to line up &#39;.&#39;s.</span>
<span class="n">lines</span><span class="o">.</span><span class="n">append</span><span class="p">((</span><span class="n">n</span><span class="p">,</span> <span class="s1">&#39;</span><span class="si">%s</span><span class="s1"> </span><span class="si">%s</span><span class="s1">&#39;</span> <span class="o">%</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="k">return</span> <span class="p">[</span> <span class="c1"># Prefix spaces to line up &#39;&#39;s.</span>
<span class="p">(</span><span class="s1">&#39; &#39;</span> <span class="o">*</span> <span class="p">(</span><span class="n">max_stack_length</span> <span class="o">-</span> <span class="n">length</span><span class="p">)</span> <span class="o">+</span> <span class="n">line</span><span class="p">)</span>
<span class="k">for</span> <span class="n">length</span><span class="p">,</span> <span class="n">line</span> <span class="ow">in</span> <span class="n">lines</span>
<span class="p">]</span></div>

View File

@ -514,29 +514,28 @@ data parameter. For an integer data parameter it works like this: If
the data parameter is zero, then the first quotation has to produce
the value to be returned. If the data parameter is positive then the
second has to combine the data parameter with the result of applying
the function to its predecessor.</p>
<p>5 [1] [*] primrec</p>
the function to its predecessor.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mi">5</span> <span class="p">[</span><span class="mi">1</span><span class="p">]</span> <span class="p">[</span><span class="o">*</span><span class="p">]</span> <span class="n">primrec</span>
</pre></div>
</div>
<p>&gt; Then primrec tests whether the top element on the stack (initially
the 5) is equal to zero. If it is, it pops it off and executes one of
the quotations, the [1] which leaves 1 on the stack as the result.
Otherwise it pushes a decremented copy of the top element and
recurses. On the way back from the recursion it uses the other
quotation, [*], to multiply what is now a factorial on top of the
stack by the second element on the stack.</p>
<blockquote>
<div><blockquote>
<div>n [Base] [Recur] primrec</div></blockquote>
<p>0 [Base] [Recur] primrec</p>
</div></blockquote>
<blockquote>
<div><blockquote>
<div>Base</div></blockquote>
<p>n [Base] [Recur] primrec</p>
</div></blockquote>
<dl class="docutils">
<dt>—————————————— n &gt; 0</dt>
<dd>n (n-1) [Base] [Recur] primrec Recur</dd>
</dl>
stack by the second element on the stack.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">n</span> <span class="p">[</span><span class="n">Base</span><span class="p">]</span> <span class="p">[</span><span class="n">Recur</span><span class="p">]</span> <span class="n">primrec</span>
<span class="mi">0</span> <span class="p">[</span><span class="n">Base</span><span class="p">]</span> <span class="p">[</span><span class="n">Recur</span><span class="p">]</span> <span class="n">primrec</span>
<span class="o">------------------------------</span>
<span class="n">Base</span>
<span class="n">n</span> <span class="p">[</span><span class="n">Base</span><span class="p">]</span> <span class="p">[</span><span class="n">Recur</span><span class="p">]</span> <span class="n">primrec</span>
<span class="o">------------------------------------------</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">0</span>
<span class="n">n</span> <span class="p">(</span><span class="n">n</span><span class="o">-</span><span class="mi">1</span><span class="p">)</span> <span class="p">[</span><span class="n">Base</span><span class="p">]</span> <span class="p">[</span><span class="n">Recur</span><span class="p">]</span> <span class="n">primrec</span> <span class="n">Recur</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
@ -644,7 +643,9 @@ on top of the stack.</p>
<dt id="joy.library.sum_">
<code class="descclassname">joy.library.</code><code class="descname">sum_</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#sum_"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.library.sum_" title="Permalink to this definition"></a></dt>
<dd><p>Given a quoted sequence of numbers return the sum.</p>
<p>sum == 0 swap [+] step</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="nb">sum</span> <span class="o">==</span> <span class="mi">0</span> <span class="n">swap</span> <span class="p">[</span><span class="o">+</span><span class="p">]</span> <span class="n">step</span>
</pre></div>
</div>
</dd></dl>
<dl class="function">
@ -670,12 +671,12 @@ use reverse if needed.)</p>
<span class="o">...</span> <span class="mi">1</span> <span class="p">[</span><span class="n">Q</span><span class="p">]</span> <span class="o">.</span> <span class="n">times</span>
<span class="o">---------------------------------</span>
<span class="o">-----------------------</span>
<span class="o">...</span> <span class="o">.</span> <span class="n">Q</span>
<span class="o">...</span> <span class="n">n</span> <span class="p">[</span><span class="n">Q</span><span class="p">]</span> <span class="o">.</span> <span class="n">times</span>
<span class="o">---------------------------------</span> <span class="n">w</span><span class="o">/</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">1</span>
<span class="o">-------------------------------------</span> <span class="n">w</span><span class="o">/</span> <span class="n">n</span> <span class="o">&gt;</span> <span class="mi">1</span>
<span class="o">...</span> <span class="o">.</span> <span class="n">Q</span> <span class="p">(</span><span class="n">n</span> <span class="o">-</span> <span class="mi">1</span><span class="p">)</span> <span class="p">[</span><span class="n">Q</span><span class="p">]</span> <span class="n">times</span>
</pre></div>
</div>

View File

@ -32,13 +32,13 @@
<div class="bodywrapper">
<div class="body" role="main">
<div class="section" id="newtons-method">
<h1><a class="reference external" href="https://en.wikipedia.org/wiki/Newton%27s_method">Newtons method</a><a class="headerlink" href="#newtons-method" title="Permalink to this headline"></a></h1>
<div class="section" id="newton-s-method">
<h1><a class="reference external" href="https://en.wikipedia.org/wiki/Newton%27s_method">Newtons method</a><a class="headerlink" href="#newton-s-method" title="Permalink to this headline"></a></h1>
<p>Lets use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.</p>
<p>Cf. <a class="reference external" href="https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf">“Why Functional Programming Matters” by John
Hughes</a></p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="k">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">notebook_preamble</span> <span class="k">import</span> <span class="n">J</span><span class="p">,</span> <span class="n">V</span><span class="p">,</span> <span class="n">define</span>
</pre></div>
</div>
<div class="section" id="a-generator-for-approximations">
@ -90,10 +90,10 @@ function were writing. If we let 1 be the initial approximation:</p>
<span class="mi">1</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">make_generator</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="p">[</span><span class="mi">1</span> <span class="p">[</span><span class="n">dup</span> <span class="mi">23</span> <span class="n">over</span> <span class="o">/</span> <span class="o">+</span> <span class="mi">2</span> <span class="o">/</span><span class="p">]</span> <span class="n">codireco</span><span class="p">]</span>
@ -101,7 +101,7 @@ function were writing. If we let 1 be the initial approximation:</p>
</div>
<p>Lets drive the generator a few time (with the <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator) and
square the approximation to see how well it works…</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra 6 [x popd] times first sqr&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 gsra 6 [x popd] times first sqr&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">23.0000000001585</span>
@ -142,7 +142,7 @@ generated already and epsilon ε is handy on the stack…</p>
<span class="p">(</span><span class="nb">abs</span><span class="p">(</span><span class="n">a</span><span class="o">-</span><span class="n">b</span><span class="p">)</span><span class="o">&lt;=</span><span class="n">ε</span><span class="p">)</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_P == [first - abs] dip &lt;=&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_P [first - abs] dip &lt;=&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@ -154,7 +154,7 @@ generated already and epsilon ε is handy on the stack…</p>
<span class="n">b</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_B == roll&lt; popop first&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_B roll&lt; popop first&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@ -166,7 +166,7 @@ generated already and epsilon ε is handy on the stack…</p>
<ol class="arabic simple">
<li>Discard a.</li>
<li>Use <code class="docutils literal notranslate"><span class="pre">x</span></code> combinator to generate next term from <code class="docutils literal notranslate"><span class="pre">G</span></code>.</li>
<li>Run <code class="docutils literal notranslate"><span class="pre">within</span></code> with <code class="docutils literal notranslate"><span class="pre">i</span></code> (it is a <code class="docutils literal notranslate"><span class="pre">primrec</span></code> function.)</li>
<li>Run <code class="docutils literal notranslate"><span class="pre">within</span></code> with <code class="docutils literal notranslate"><span class="pre">i</span></code> (it is a “tail-recursive” function.)</li>
</ol>
<p>Pretty straightforward:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">R0</span> <span class="p">[</span><span class="n">within</span><span class="p">]</span> <span class="n">R1</span>
@ -179,7 +179,7 @@ generated already and epsilon ε is handy on the stack…</p>
<span class="n">b</span> <span class="p">[</span><span class="n">c</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="n">within</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_R == [popd x] dip&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;_within_R [popd x] dip&#39;</span><span class="p">)</span>
</pre></div>
</div>
</div>
@ -191,31 +191,31 @@ generated already and epsilon ε is handy on the stack…</p>
<span class="n">a</span> <span class="p">[</span><span class="n">b</span> <span class="n">G</span><span class="p">]</span> <span class="n">ε</span> <span class="o">...</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec&#39;</span><span class="p">)</span>
<span class="n">define</span><span class="p">(</span><span class="s1">&#39;sqrt == gsra within&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">define</span><span class="p">(</span><span class="s1">&#39;within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec&#39;</span><span class="p">)</span>
<span class="n">define</span><span class="p">(</span><span class="s1">&#39;sqrt gsra within&#39;</span><span class="p">)</span>
</pre></div>
</div>
<p>Try it out…</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;36 sqrt&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;36 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">6.0</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 sqrt&#39;</span><span class="p">)</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">J</span><span class="p">(</span><span class="s1">&#39;23 sqrt&#39;</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span>
</pre></div>
</div>
<p>Check it.</p>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span><span class="o">**</span><span class="mi">2</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">4.795831523312719</span><span class="o">**</span><span class="mi">2</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="mf">22.999999999999996</span>
</pre></div>
</div>
<div class="code ipython2 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">sqrt</span>
<div class="code ipython3 highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">math</span> <span class="k">import</span> <span class="n">sqrt</span>
<span class="n">sqrt</span><span class="p">(</span><span class="mi">23</span><span class="p">)</span>
</pre></div>

View File

@ -37,22 +37,22 @@
<div class="section" id="module-joy.utils.pretty_print">
<span id="joy-utils-pretty-print"></span><h2><code class="docutils literal notranslate"><span class="pre">joy.utils.pretty_print</span></code><a class="headerlink" href="#module-joy.utils.pretty_print" title="Permalink to this headline"></a></h2>
<p>Pretty printing support, e.g.:</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>Joy? 23 18 * 99 +
. 23 18 mul 99 add
23 . 18 mul 99 add
23 18 . mul 99 add
414 . 99 add
414 99 . add
513 .
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span>Joy? [23 18 * 99 +] trace
23 18 mul 99 add
23 18 mul 99 add
23 18 mul 99 add
414 99 add
414 99 add
513
513 &lt;-top
joy?
</pre></div>
</div>
<p>On each line the stack is printed with the top to the right, then a <code class="docutils literal notranslate"><span class="pre">.</span></code> to
represent the current locus of processing, then the pending expression to the
left.</p>
<p>On each line the stack is printed with the top to the left, then a
bullet symbol,``•<a href="#id1"><span class="problematic" id="id2">``</span></a>, to represent the current locus of processing, then
the pending expression to the right.</p>
<dl class="class">
<dt id="joy.utils.pretty_print.TracePrinter">
<em class="property">class </em><code class="descclassname">joy.utils.pretty_print.</code><code class="descname">TracePrinter</code><a class="reference internal" href="_modules/joy/utils/pretty_print.html#TracePrinter"><span class="viewcode-link">[source]</span></a><a class="headerlink" href="#joy.utils.pretty_print.TracePrinter" title="Permalink to this definition"></a></dt>

File diff suppressed because one or more lines are too long

View File

@ -1,13 +1,13 @@
`Newtons method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
`Newton's method <https://en.wikipedia.org/wiki/Newton%27s_method>`__
=====================================================================
Lets use the Newton-Raphson method for finding the root of an equation
Let's use the Newton-Raphson method for finding the root of an equation
to write a function that can compute the square root of a number.
Cf. `“Why Functional Programming Matters” by John
Cf. `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__
.. code:: ipython2
.. code:: ipython3
from notebook_preamble import J, V, define
@ -75,11 +75,11 @@ The generator can be written as:
1 [23 over / + 2 /] [dup] swoncat make_generator
1 [dup 23 over / + 2 /] make_generator
.. code:: ipython2
.. code:: ipython3
define('gsra == 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
define('gsra 1 swap [over / + 2 /] cons [dup] swoncat make_generator')
.. code:: ipython2
.. code:: ipython3
J('23 gsra')
@ -89,10 +89,10 @@ The generator can be written as:
[1 [dup 23 over / + 2 /] codireco]
Lets drive the generator a few time (with the ``x`` combinator) and
square the approximation to see how well it works
Let's drive the generator a few time (with the ``x`` combinator) and
square the approximation to see how well it works...
.. code:: ipython2
.. code:: ipython3
J('23 gsra 6 [x popd] times first sqr')
@ -105,7 +105,7 @@ square the approximation to see how well it works…
Finding Consecutive Approximations within a Tolerance
-----------------------------------------------------
From `“Why Functional Programming Matters” by John
From `"Why Functional Programming Matters" by John
Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
The remainder of a square root finder is a function *within*, which
@ -117,7 +117,7 @@ Hughes <https://www.cs.kent.ac.uk/people/staff/dat/miranda/whyfp90.pdf>`__:
Using the *output* ``[a G]`` of the above generator for square root
approximations, and further assuming that the first term a has been
generated already and epsilon ε is handy on the stack
generated already and epsilon ε is handy on the stack...
::
@ -142,9 +142,9 @@ Predicate
abs(a-b) ε <=
(abs(a-b)<=ε)
.. code:: ipython2
.. code:: ipython3
define('_within_P == [first - abs] dip <=')
define('_within_P [first - abs] dip <=')
Base-Case
~~~~~~~~~
@ -156,9 +156,9 @@ Base-Case
[b G] first
b
.. code:: ipython2
.. code:: ipython3
define('_within_B == roll< popop first')
define('_within_B roll< popop first')
Recur
~~~~~
@ -169,7 +169,7 @@ Recur
1. Discard a.
2. Use ``x`` combinator to generate next term from ``G``.
3. Run ``within`` with ``i`` (it is a ``primrec`` function.)
3. Run ``within`` with ``i`` (it is a "tail-recursive" function.)
Pretty straightforward:
@ -184,9 +184,9 @@ Pretty straightforward:
b [c G] ε within
.. code:: ipython2
.. code:: ipython3
define('_within_R == [popd x] dip')
define('_within_R [popd x] dip')
Setting up
~~~~~~~~~~
@ -199,14 +199,14 @@ The recursive function we have defined so far needs a slight preamble:
[a G] x ε ...
a [b G] ε ...
.. code:: ipython2
.. code:: ipython3
define('within == x 0.000000001 [_within_P] [_within_B] [_within_R] primrec')
define('sqrt == gsra within')
define('within x 0.000000001 [_within_P] [_within_B] [_within_R] tailrec')
define('sqrt gsra within')
Try it out
Try it out...
.. code:: ipython2
.. code:: ipython3
J('36 sqrt')
@ -216,7 +216,7 @@ Try it out…
6.0
.. code:: ipython2
.. code:: ipython3
J('23 sqrt')
@ -228,7 +228,7 @@ Try it out…
Check it.
.. code:: ipython2
.. code:: ipython3
4.795831523312719**2
@ -241,7 +241,7 @@ Check it.
.. code:: ipython2
.. code:: ipython3
from math import sqrt

View File

@ -23,12 +23,9 @@ functions. Its main export is a Python function initialize() that
returns a dictionary of Joy functions suitable for use with the joy()
function.
'''
from builtins import map, object, range, zip
from inspect import getdoc
from inspect import getdoc, getmembers, isfunction
from functools import wraps
from itertools import count
from inspect import getmembers, isfunction
import operator, math
from .parser import text_to_expression, Symbol
@ -292,7 +289,7 @@ def _text_to_defs(text):
return (
line.strip()
for line in text.splitlines()
if not line.startswith('#')
if line and not line.startswith('#')
)