Cleaning up docs.
This commit is contained in:
+3
-3
@@ -208,9 +208,9 @@ with the ``x`` combinator.
|
||||
Generating Multiples of Three and Five
|
||||
--------------------------------------
|
||||
|
||||
Look at the treatment of the Project Euler Problem One in `Developing a
|
||||
Program.ipynb <./Developing%20a%20Program.ipynb>`__ and you'll see that
|
||||
we might be interested in generating an endless cycle of:
|
||||
Look at the treatment of the Project Euler Problem One in the
|
||||
"Developing a Program" notebook and you'll see that we might be
|
||||
interested in generating an endless cycle of:
|
||||
|
||||
::
|
||||
|
||||
@@ -1,30 +1,39 @@
|
||||
|
||||
***********************************************************************
|
||||
`Quadratic formula <https://en.wikipedia.org/wiki/Quadratic_formula>`__
|
||||
***********************************************************************
|
||||
=======================================================================
|
||||
|
||||
`The Quadratic formula <https://en.wikipedia.org/wiki/Quadratic_formula>`__
|
||||
.. code:: ipython2
|
||||
|
||||
from notebook_preamble import J, V, define
|
||||
|
||||
Cf.
|
||||
`jp-quadratic.html <http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html>`__
|
||||
|
||||
::
|
||||
|
||||
-b +/- sqrt(b^2 - 4 * a * c)
|
||||
-----------------------------
|
||||
2 * a
|
||||
|
||||
:math:`\frac{-b \pm \sqrt{b^2 - 4ac}}{2a}`
|
||||
|
||||
In
|
||||
`jp-quadratic.html <http://www.kevinalbrecht.com/code/joy-mirror/jp-quadratic.html>`__
|
||||
a Joy function for the Quadratic formula is derived (along with one of my favorite combinators ``[i] map``,
|
||||
which I like to call ``pam``) starting with a version written in Scheme. Here we investigate a different approach.
|
||||
|
||||
Write a program with variable names.
|
||||
====================================
|
||||
Write a straightforward program with variable names.
|
||||
----------------------------------------------------
|
||||
|
||||
::
|
||||
|
||||
b neg b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2
|
||||
|
||||
We use ``cleave`` to compute the sum and difference ("plus-or-minus") and then ``app2`` to finish computing both roots using a quoted program ``[2a truediv]`` built with ``cons``.
|
||||
We use ``cleave`` to compute the sum and difference and then ``app2`` to
|
||||
finish computing both roots using a quoted program ``[2a truediv]``
|
||||
built with ``cons``.
|
||||
|
||||
Check it.
|
||||
~~~~~~~~~
|
||||
|
||||
Evaluating by hand::
|
||||
Evaluating by hand:
|
||||
|
||||
::
|
||||
|
||||
b neg b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2
|
||||
-b b sqr 4 a c * * - sqrt [+] [-] cleave a 2 * [truediv] cons app2
|
||||
@@ -38,19 +47,21 @@ Evaluating by hand::
|
||||
-b -b+sqrt(b^2-4ac) -b-sqrt(b^2-4ac) [2a truediv] app2
|
||||
-b -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a
|
||||
|
||||
(Eventually we'll be able to use e.g. Sympy versions of the Joy commands to do this sort of thing symbolically. This is part of what is meant by a "categorical" language.)
|
||||
(Eventually we’ll be able to use e.g. Sympy versions of the Joy commands
|
||||
to do this sort of thing symbolically. This is part of what is meant by
|
||||
a “categorical” language.)
|
||||
|
||||
Cleanup
|
||||
~~~~~~~
|
||||
|
||||
::
|
||||
|
||||
-b -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a roll< pop
|
||||
-b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a -b pop
|
||||
-b -b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a roll< pop
|
||||
-b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a -b pop
|
||||
-b+sqrt(b^2-4ac)/2a -b-sqrt(b^2-4ac)/2a
|
||||
|
||||
Derive a definition.
|
||||
====================
|
||||
--------------------
|
||||
|
||||
::
|
||||
|
||||
@@ -60,10 +71,6 @@ Derive a definition.
|
||||
b a c a [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop
|
||||
b a c over [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
from notebook_preamble import J, V, define
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
define('quadratic == over [[[neg] dupdip sqr 4] dipd * * - sqrt [+] [-] cleave] dip 2 * [truediv] cons app2 roll< pop')
|
||||
@@ -79,13 +86,13 @@ Derive a definition.
|
||||
|
||||
|
||||
Simplify
|
||||
~~~~~~~~
|
||||
--------
|
||||
|
||||
We can define a ``pm`` plus-or-minus function:
|
||||
|
||||
.. code:: ipython2
|
||||
::
|
||||
|
||||
define('pm == [+] [-] cleave popdd')
|
||||
pm == [+] [-] cleave popdd
|
||||
|
||||
Then ``quadratic`` becomes:
|
||||
|
||||
@@ -109,22 +116,15 @@ Define a "native" ``pm`` function.
|
||||
The definition of ``pm`` above is pretty elegant, but the implementation
|
||||
takes a lot of steps relative to what it's accomplishing. Since we are
|
||||
likely to use ``pm`` more than once in the future, let's write a
|
||||
primitive in Python and add it to the dictionary.
|
||||
primitive in Python and add it to the dictionary. (This has been done
|
||||
already.)
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
from joy.library import SimpleFunctionWrapper
|
||||
from notebook_preamble import D
|
||||
|
||||
|
||||
@SimpleFunctionWrapper
|
||||
def pm(stack):
|
||||
a, (b, stack) = stack
|
||||
p, m, = b + a, b - a
|
||||
return m, (p, stack)
|
||||
|
||||
|
||||
D['pm'] = pm
|
||||
|
||||
The resulting trace is short enough to fit on a page.
|
||||
|
||||
|
||||
@@ -1,4 +1,7 @@
|
||||
|
||||
Traversing Datastructures with Zippers
|
||||
======================================
|
||||
|
||||
This notebook is about using the "zipper" with joy datastructures. See
|
||||
the `Zipper wikipedia
|
||||
entry <https://en.wikipedia.org/wiki/Zipper_%28data_structure%29>`__ or
|
||||
@@ -8,9 +11,6 @@ Huet <https://www.st.cs.uni-saarland.de/edu/seminare/2005/advanced-fp/docs/huet-
|
||||
Given a datastructure on the stack we can navigate through it, modify
|
||||
it, and rebuild it using the "zipper" technique.
|
||||
|
||||
Preamble
|
||||
~~~~~~~~
|
||||
|
||||
.. code:: ipython2
|
||||
|
||||
from notebook_preamble import J, V, define
|
||||
|
||||
@@ -12,9 +12,10 @@ These essays are adapted from Jupyter notebooks. I hope to have those hosted so
|
||||
Replacing
|
||||
Ordered_Binary_Trees
|
||||
Treestep
|
||||
Generator Programs
|
||||
Generator_Programs
|
||||
Newton-Raphson
|
||||
Quadratic
|
||||
Zipper
|
||||
NoUpdates
|
||||
Categorical
|
||||
|
||||
|
||||
Reference in New Issue
Block a user