-.. code:: python
+.. code:: ipython2
from notebook_preamble import D, DefinitionWrapper, J, V, define
It may be helpful to see this function implemented in imperative Python
code.
-.. code:: python
+.. code:: ipython2
def hylomorphism(c, F, P, G):
'''Return a hylomorphism function H.'''
hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec
-.. code:: python
+.. code:: ipython2
define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec')
- ``[G]`` is ``[-- dup]``
- ``[F]`` is ``[+]``
-.. code:: python
+.. code:: ipython2
define('triangular_number == [1 <=] 0 [-- dup] [+] hylomorphism')
Let’s try it:
-.. code:: python
+.. code:: ipython2
J('5 triangular_number')
10
-.. code:: python
+.. code:: ipython2
J('[0 1 2 3 4 5 6] [triangular_number] map')
A == [P] [] [G] [swons] hylomorphism
-``range`` et. al.
-~~~~~~~~~~~~~~~~~
-
-An example of an anamorphism is the ``range`` function which generates the list of integers from 0 to *n* - 1 given *n*.
+``range`` et. al. An example of an anamorphism is the ``range`` function which generates the list of integers from 0 to *n* - 1 given *n*.
+~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Each of the above variations can be used to make four slightly different
``range`` functions.
H1 == [P] [pop c] [G] [dip F] genrec
== [0 <=] [pop []] [-- dup] [dip swons] genrec
-.. code:: python
+.. code:: ipython2
define('range == [0 <=] [] [-- dup] [swons] hylomorphism')
-.. code:: python
+.. code:: ipython2
J('5 range')
H2 == c swap [P] [pop] [G [F] dip] primrec
== [] swap [0 <=] [pop] [-- dup [swons] dip] primrec
-.. code:: python
+.. code:: ipython2
define('range_reverse == [] swap [0 <=] [pop] [-- dup [swons] dip] primrec')
-.. code:: python
+.. code:: ipython2
J('5 range_reverse')
H3 == [P] [pop c] [[G] dupdip] [dip F] genrec
== [0 <=] [pop []] [[--] dupdip] [dip swons] genrec
-.. code:: python
+.. code:: ipython2
define('ranger == [0 <=] [pop []] [[--] dupdip] [dip swons] genrec')
-.. code:: python
+.. code:: ipython2
J('5 ranger')
H4 == c swap [P] [pop] [[F] dupdip G ] primrec
== [] swap [0 <=] [pop] [[swons] dupdip --] primrec
-.. code:: python
+.. code:: ipython2
define('ranger_reverse == [] swap [0 <=] [pop] [[swons] dupdip --] primrec')
-.. code:: python
+.. code:: ipython2
J('5 ranger_reverse')
C == [not] c [uncons swap] [F] hylomorphism
-.. code:: python
+.. code:: ipython2
define('swuncons == uncons swap') # Awkward name.
sum == [not] 0 [swuncons] [+] hylomorphism
-.. code:: python
+.. code:: ipython2
define('sum == [not] 0 [swuncons] [+] hylomorphism')
-.. code:: python
+.. code:: ipython2
J('[5 4 3 2 1] sum')
The ``step`` combinator will usually be better to use than
``catamorphism``.
-.. code:: python
+.. code:: ipython2
J('[step] help')
-.. code:: python
+.. code:: ipython2
define('sum == 0 swap [+] step')
-.. code:: python
+.. code:: ipython2
J('[5 4 3 2 1] sum')
G == --
P == 1 <=
-.. code:: python
+.. code:: ipython2
define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec')
-.. code:: python
+.. code:: ipython2
J('5 factorial')
G == rest dup
P == not
-.. code:: python
+.. code:: ipython2
define('tails == [] swap [not] [pop] [rest dup [swons] dip] primrec')
-.. code:: python
+.. code:: ipython2
J('[1 2 3] tails')