-.. code:: ipython2
+.. code:: python
from notebook_preamble import D, DefinitionWrapper, J, V, define
It may be helpful to see this function implemented in imperative Python
code.
-.. code:: ipython2
+.. code:: python
def hylomorphism(c, F, P, G):
'''Return a hylomorphism function H.'''
hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec
-.. code:: ipython2
+.. code:: python
define('hylomorphism == [unit [pop] swoncat] dipd [dip] swoncat genrec')
- ``[G]`` is ``[-- dup]``
- ``[F]`` is ``[+]``
-.. code:: ipython2
+.. code:: python
define('triangular_number == [1 <=] 0 [-- dup] [+] hylomorphism')
Let’s try it:
-.. code:: ipython2
+.. code:: python
J('5 triangular_number')
10
-.. code:: ipython2
+.. code:: python
J('[0 1 2 3 4 5 6] [triangular_number] map')
H1 == [P] [pop c] [G] [dip F] genrec
== [0 <=] [pop []] [-- dup] [dip swons] genrec
-.. code:: ipython2
+.. code:: python
define('range == [0 <=] [] [-- dup] [swons] hylomorphism')
-.. code:: ipython2
+.. code:: python
J('5 range')
H2 == c swap [P] [pop] [G [F] dip] primrec
== [] swap [0 <=] [pop] [-- dup [swons] dip] primrec
-.. code:: ipython2
+.. code:: python
define('range_reverse == [] swap [0 <=] [pop] [-- dup [swons] dip] primrec')
-.. code:: ipython2
+.. code:: python
J('5 range_reverse')
H3 == [P] [pop c] [[G] dupdip] [dip F] genrec
== [0 <=] [pop []] [[--] dupdip] [dip swons] genrec
-.. code:: ipython2
+.. code:: python
define('ranger == [0 <=] [pop []] [[--] dupdip] [dip swons] genrec')
-.. code:: ipython2
+.. code:: python
J('5 ranger')
H4 == c swap [P] [pop] [[F] dupdip G ] primrec
== [] swap [0 <=] [pop] [[swons] dupdip --] primrec
-.. code:: ipython2
+.. code:: python
define('ranger_reverse == [] swap [0 <=] [pop] [[swons] dupdip --] primrec')
-.. code:: ipython2
+.. code:: python
J('5 ranger_reverse')
C == [not] c [uncons swap] [F] hylomorphism
-.. code:: ipython2
+.. code:: python
define('swuncons == uncons swap') # Awkward name.
sum == [not] 0 [swuncons] [+] hylomorphism
-.. code:: ipython2
+.. code:: python
define('sum == [not] 0 [swuncons] [+] hylomorphism')
-.. code:: ipython2
+.. code:: python
J('[5 4 3 2 1] sum')
The ``step`` combinator will usually be better to use than
``catamorphism``.
-.. code:: ipython2
+.. code:: python
J('[step] help')
-.. code:: ipython2
+.. code:: python
define('sum == 0 swap [+] step')
-.. code:: ipython2
+.. code:: python
J('[5 4 3 2 1] sum')
G == --
P == 1 <=
-.. code:: ipython2
+.. code:: python
define('factorial == 1 swap [1 <=] [pop] [[*] dupdip --] primrec')
-.. code:: ipython2
+.. code:: python
J('5 factorial')
G == rest dup
P == not
-.. code:: ipython2
+.. code:: python
define('tails == [] swap [not] [pop] [rest dup [swons] dip] primrec')
-.. code:: ipython2
+.. code:: python
J('[1 2 3] tails')