OSDN Git Service

Bleah.
[joypy/Thun.git] / docs / 2._Library_Examples.ipynb
1 {
2  "cells": [
3   {
4    "cell_type": "markdown",
5    "metadata": {},
6    "source": [
7     "# Examples (and some documentation) for the Words in the Library"
8    ]
9   },
10   {
11    "cell_type": "code",
12    "execution_count": 1,
13    "metadata": {},
14    "outputs": [],
15    "source": [
16     "from notebook_preamble import J, V"
17    ]
18   },
19   {
20    "cell_type": "markdown",
21    "metadata": {},
22    "source": [
23     "# Stack Chatter\n",
24     "This is what I like to call the functions that just rearrange things on the stack.  (One thing I want to mention is that during a hypothetical compilation phase these \"stack chatter\" words effectively disappear, because we can map the logical stack locations to registers that remain static for the duration of the computation.  This remains to be done but it's \"off the shelf\" technology.)"
25    ]
26   },
27   {
28    "cell_type": "markdown",
29    "metadata": {},
30    "source": [
31     "### `clear`"
32    ]
33   },
34   {
35    "cell_type": "code",
36    "execution_count": 2,
37    "metadata": {},
38    "outputs": [
39     {
40      "name": "stdout",
41      "output_type": "stream",
42      "text": [
43       "\n"
44      ]
45     }
46    ],
47    "source": [
48     "J('1 2 3 clear')"
49    ]
50   },
51   {
52    "cell_type": "markdown",
53    "metadata": {},
54    "source": [
55     "### `dup` `dupd`"
56    ]
57   },
58   {
59    "cell_type": "code",
60    "execution_count": 3,
61    "metadata": {},
62    "outputs": [
63     {
64      "name": "stdout",
65      "output_type": "stream",
66      "text": [
67       "1 2 3 3\n"
68      ]
69     }
70    ],
71    "source": [
72     "J('1 2 3 dup')"
73    ]
74   },
75   {
76    "cell_type": "code",
77    "execution_count": 4,
78    "metadata": {},
79    "outputs": [
80     {
81      "name": "stdout",
82      "output_type": "stream",
83      "text": [
84       "1 2 2 3\n"
85      ]
86     }
87    ],
88    "source": [
89     "J('1 2 3 dupd')"
90    ]
91   },
92   {
93    "cell_type": "markdown",
94    "metadata": {},
95    "source": [
96     "### `enstacken` `disenstacken` `stack` `unstack`\n",
97     "\n",
98     "Replace the stack with a quote of itself."
99    ]
100   },
101   {
102    "cell_type": "code",
103    "execution_count": 5,
104    "metadata": {},
105    "outputs": [
106     {
107      "name": "stdout",
108      "output_type": "stream",
109      "text": [
110       "[3 2 1]\n"
111      ]
112     }
113    ],
114    "source": [
115     "J('1 2 3 enstacken')"
116    ]
117   },
118   {
119    "cell_type": "markdown",
120    "metadata": {},
121    "source": [
122     "Unpack a list onto the stack."
123    ]
124   },
125   {
126    "cell_type": "code",
127    "execution_count": 6,
128    "metadata": {},
129    "outputs": [
130     {
131      "name": "stdout",
132      "output_type": "stream",
133      "text": [
134       "4 5 6 3 2 1\n"
135      ]
136     }
137    ],
138    "source": [
139     "J('4 5 6 [3 2 1] unstack')"
140    ]
141   },
142   {
143    "cell_type": "markdown",
144    "metadata": {},
145    "source": [
146     "Get the stack on the stack."
147    ]
148   },
149   {
150    "cell_type": "code",
151    "execution_count": 7,
152    "metadata": {},
153    "outputs": [
154     {
155      "name": "stdout",
156      "output_type": "stream",
157      "text": [
158       "1 2 3 [3 2 1]\n"
159      ]
160     }
161    ],
162    "source": [
163     "J('1 2 3 stack')"
164    ]
165   },
166   {
167    "cell_type": "markdown",
168    "metadata": {},
169    "source": [
170     "Replace the stack with the list on top.\n",
171     "The items appear reversed but they are not,\n",
172     "is on the top of both the list and the stack."
173    ]
174   },
175   {
176    "cell_type": "code",
177    "execution_count": 8,
178    "metadata": {},
179    "outputs": [
180     {
181      "name": "stdout",
182      "output_type": "stream",
183      "text": [
184       "6 5 4\n"
185      ]
186     }
187    ],
188    "source": [
189     "J('1 2 3 [4 5 6] disenstacken')"
190    ]
191   },
192   {
193    "cell_type": "markdown",
194    "metadata": {},
195    "source": [
196     "### `pop` `popd` `popop`"
197    ]
198   },
199   {
200    "cell_type": "code",
201    "execution_count": 9,
202    "metadata": {},
203    "outputs": [
204     {
205      "name": "stdout",
206      "output_type": "stream",
207      "text": [
208       "1 2\n"
209      ]
210     }
211    ],
212    "source": [
213     "J('1 2 3 pop')"
214    ]
215   },
216   {
217    "cell_type": "code",
218    "execution_count": 10,
219    "metadata": {},
220    "outputs": [
221     {
222      "name": "stdout",
223      "output_type": "stream",
224      "text": [
225       "1 3\n"
226      ]
227     }
228    ],
229    "source": [
230     "J('1 2 3 popd')"
231    ]
232   },
233   {
234    "cell_type": "code",
235    "execution_count": 11,
236    "metadata": {},
237    "outputs": [
238     {
239      "name": "stdout",
240      "output_type": "stream",
241      "text": [
242       "1\n"
243      ]
244     }
245    ],
246    "source": [
247     "J('1 2 3 popop')"
248    ]
249   },
250   {
251    "cell_type": "markdown",
252    "metadata": {},
253    "source": [
254     "### `roll<` `rolldown` `roll>` `rollup`\n",
255     "The \"down\" and \"up\" refer to the movement of two of the top three items (displacing the third.)"
256    ]
257   },
258   {
259    "cell_type": "code",
260    "execution_count": 12,
261    "metadata": {},
262    "outputs": [
263     {
264      "name": "stdout",
265      "output_type": "stream",
266      "text": [
267       "2 3 1\n"
268      ]
269     }
270    ],
271    "source": [
272     "J('1 2 3 roll<')"
273    ]
274   },
275   {
276    "cell_type": "code",
277    "execution_count": 13,
278    "metadata": {},
279    "outputs": [
280     {
281      "name": "stdout",
282      "output_type": "stream",
283      "text": [
284       "3 1 2\n"
285      ]
286     }
287    ],
288    "source": [
289     "J('1 2 3 roll>')"
290    ]
291   },
292   {
293    "cell_type": "markdown",
294    "metadata": {},
295    "source": [
296     "### `swap`"
297    ]
298   },
299   {
300    "cell_type": "code",
301    "execution_count": 14,
302    "metadata": {},
303    "outputs": [
304     {
305      "name": "stdout",
306      "output_type": "stream",
307      "text": [
308       "1 3 2\n"
309      ]
310     }
311    ],
312    "source": [
313     "J('1 2 3 swap')"
314    ]
315   },
316   {
317    "cell_type": "markdown",
318    "metadata": {},
319    "source": [
320     "### `tuck` `over`"
321    ]
322   },
323   {
324    "cell_type": "code",
325    "execution_count": 15,
326    "metadata": {},
327    "outputs": [
328     {
329      "name": "stdout",
330      "output_type": "stream",
331      "text": [
332       "1 3 2 3\n"
333      ]
334     }
335    ],
336    "source": [
337     "J('1 2 3 tuck')"
338    ]
339   },
340   {
341    "cell_type": "code",
342    "execution_count": 16,
343    "metadata": {},
344    "outputs": [
345     {
346      "name": "stdout",
347      "output_type": "stream",
348      "text": [
349       "1 2 3 2\n"
350      ]
351     }
352    ],
353    "source": [
354     "J('1 2 3 over')"
355    ]
356   },
357   {
358    "cell_type": "markdown",
359    "metadata": {},
360    "source": [
361     "### `unit` `quoted` `unquoted`"
362    ]
363   },
364   {
365    "cell_type": "code",
366    "execution_count": 17,
367    "metadata": {
368     "scrolled": true
369    },
370    "outputs": [
371     {
372      "name": "stdout",
373      "output_type": "stream",
374      "text": [
375       "1 2 [3]\n"
376      ]
377     }
378    ],
379    "source": [
380     "J('1 2 3 unit')"
381    ]
382   },
383   {
384    "cell_type": "code",
385    "execution_count": 18,
386    "metadata": {
387     "scrolled": true
388    },
389    "outputs": [
390     {
391      "name": "stdout",
392      "output_type": "stream",
393      "text": [
394       "1 [2] 3\n"
395      ]
396     }
397    ],
398    "source": [
399     "J('1 2 3 quoted')"
400    ]
401   },
402   {
403    "cell_type": "code",
404    "execution_count": 19,
405    "metadata": {},
406    "outputs": [
407     {
408      "name": "stdout",
409      "output_type": "stream",
410      "text": [
411       "1 2 3\n"
412      ]
413     }
414    ],
415    "source": [
416     "J('1 [2] 3 unquoted')"
417    ]
418   },
419   {
420    "cell_type": "code",
421    "execution_count": 20,
422    "metadata": {},
423    "outputs": [
424     {
425      "name": "stdout",
426      "output_type": "stream",
427      "text": [
428       "              • 1 [dup] 3 unquoted\n",
429       "            1 • [dup] 3 unquoted\n",
430       "      1 [dup] • 3 unquoted\n",
431       "    1 [dup] 3 • unquoted\n",
432       "    1 [dup] 3 • [i] dip\n",
433       "1 [dup] 3 [i] • dip\n",
434       "      1 [dup] • i 3\n",
435       "            1 • dup 3\n",
436       "          1 1 • 3\n",
437       "        1 1 3 • \n"
438      ]
439     }
440    ],
441    "source": [
442     "V('1 [dup] 3 unquoted')  # Unquoting evaluates.  Be aware."
443    ]
444   },
445   {
446    "cell_type": "markdown",
447    "metadata": {},
448    "source": [
449     "# List words"
450    ]
451   },
452   {
453    "cell_type": "markdown",
454    "metadata": {},
455    "source": [
456     "### `concat` `swoncat` `shunt`"
457    ]
458   },
459   {
460    "cell_type": "code",
461    "execution_count": 21,
462    "metadata": {
463     "scrolled": true
464    },
465    "outputs": [
466     {
467      "name": "stdout",
468      "output_type": "stream",
469      "text": [
470       "[1 2 3 4 5 6]\n"
471      ]
472     }
473    ],
474    "source": [
475     "J('[1 2 3] [4 5 6] concat')"
476    ]
477   },
478   {
479    "cell_type": "code",
480    "execution_count": 22,
481    "metadata": {
482     "scrolled": true
483    },
484    "outputs": [
485     {
486      "name": "stdout",
487      "output_type": "stream",
488      "text": [
489       "[4 5 6 1 2 3]\n"
490      ]
491     }
492    ],
493    "source": [
494     "J('[1 2 3] [4 5 6] swoncat')"
495    ]
496   },
497   {
498    "cell_type": "code",
499    "execution_count": 23,
500    "metadata": {
501     "scrolled": true
502    },
503    "outputs": [
504     {
505      "name": "stdout",
506      "output_type": "stream",
507      "text": [
508       "[6 5 4 1 2 3]\n"
509      ]
510     }
511    ],
512    "source": [
513     "J('[1 2 3] [4 5 6] shunt')"
514    ]
515   },
516   {
517    "cell_type": "markdown",
518    "metadata": {},
519    "source": [
520     "### `cons` `swons` `uncons`"
521    ]
522   },
523   {
524    "cell_type": "code",
525    "execution_count": 24,
526    "metadata": {},
527    "outputs": [
528     {
529      "name": "stdout",
530      "output_type": "stream",
531      "text": [
532       "[1 2 3]\n"
533      ]
534     }
535    ],
536    "source": [
537     "J('1 [2 3] cons')"
538    ]
539   },
540   {
541    "cell_type": "code",
542    "execution_count": 25,
543    "metadata": {},
544    "outputs": [
545     {
546      "name": "stdout",
547      "output_type": "stream",
548      "text": [
549       "[1 2 3]\n"
550      ]
551     }
552    ],
553    "source": [
554     "J('[2 3] 1 swons')"
555    ]
556   },
557   {
558    "cell_type": "code",
559    "execution_count": 26,
560    "metadata": {},
561    "outputs": [
562     {
563      "name": "stdout",
564      "output_type": "stream",
565      "text": [
566       "1 [2 3]\n"
567      ]
568     }
569    ],
570    "source": [
571     "J('[1 2 3] uncons')"
572    ]
573   },
574   {
575    "cell_type": "markdown",
576    "metadata": {},
577    "source": [
578     "### `first` `second` `third` `rest`"
579    ]
580   },
581   {
582    "cell_type": "code",
583    "execution_count": 27,
584    "metadata": {},
585    "outputs": [
586     {
587      "name": "stdout",
588      "output_type": "stream",
589      "text": [
590       "1\n"
591      ]
592     }
593    ],
594    "source": [
595     "J('[1 2 3 4] first')"
596    ]
597   },
598   {
599    "cell_type": "code",
600    "execution_count": 28,
601    "metadata": {},
602    "outputs": [
603     {
604      "name": "stdout",
605      "output_type": "stream",
606      "text": [
607       "2\n"
608      ]
609     }
610    ],
611    "source": [
612     "J('[1 2 3 4] second')"
613    ]
614   },
615   {
616    "cell_type": "code",
617    "execution_count": 29,
618    "metadata": {},
619    "outputs": [
620     {
621      "name": "stdout",
622      "output_type": "stream",
623      "text": [
624       "3\n"
625      ]
626     }
627    ],
628    "source": [
629     "J('[1 2 3 4] third')"
630    ]
631   },
632   {
633    "cell_type": "code",
634    "execution_count": 30,
635    "metadata": {
636     "scrolled": true
637    },
638    "outputs": [
639     {
640      "name": "stdout",
641      "output_type": "stream",
642      "text": [
643       "[2 3 4]\n"
644      ]
645     }
646    ],
647    "source": [
648     "J('[1 2 3 4] rest')"
649    ]
650   },
651   {
652    "cell_type": "markdown",
653    "metadata": {},
654    "source": [
655     "### `flatten`"
656    ]
657   },
658   {
659    "cell_type": "code",
660    "execution_count": 31,
661    "metadata": {},
662    "outputs": [
663     {
664      "name": "stdout",
665      "output_type": "stream",
666      "text": [
667       "[1 2 [3] 4 5 6]\n"
668      ]
669     }
670    ],
671    "source": [
672     "J('[[1] [2 [3] 4] [5 6]] flatten')"
673    ]
674   },
675   {
676    "cell_type": "markdown",
677    "metadata": {},
678    "source": [
679     "### `getitem` `at` `of` `drop` `take`\n",
680     "\n",
681     "`at` and `getitem` are the same function. `of == swap at`"
682    ]
683   },
684   {
685    "cell_type": "code",
686    "execution_count": 32,
687    "metadata": {
688     "scrolled": true
689    },
690    "outputs": [
691     {
692      "name": "stdout",
693      "output_type": "stream",
694      "text": [
695       "12\n"
696      ]
697     }
698    ],
699    "source": [
700     "J('[10 11 12 13 14] 2 getitem')"
701    ]
702   },
703   {
704    "cell_type": "code",
705    "execution_count": 33,
706    "metadata": {},
707    "outputs": [
708     {
709      "name": "stdout",
710      "output_type": "stream",
711      "text": [
712       "1\n"
713      ]
714     }
715    ],
716    "source": [
717     "J('[1 2 3 4] 0 at')"
718    ]
719   },
720   {
721    "cell_type": "code",
722    "execution_count": 34,
723    "metadata": {},
724    "outputs": [
725     {
726      "name": "stdout",
727      "output_type": "stream",
728      "text": [
729       "3\n"
730      ]
731     }
732    ],
733    "source": [
734     "J('2 [1 2 3 4] of')"
735    ]
736   },
737   {
738    "cell_type": "code",
739    "execution_count": 35,
740    "metadata": {},
741    "outputs": [
742     {
743      "name": "stdout",
744      "output_type": "stream",
745      "text": [
746       "[3 4]\n"
747      ]
748     }
749    ],
750    "source": [
751     "J('[1 2 3 4] 2 drop')"
752    ]
753   },
754   {
755    "cell_type": "code",
756    "execution_count": 36,
757    "metadata": {},
758    "outputs": [
759     {
760      "name": "stdout",
761      "output_type": "stream",
762      "text": [
763       "[2 1]\n"
764      ]
765     }
766    ],
767    "source": [
768     "J('[1 2 3 4] 2 take')  # reverses the order"
769    ]
770   },
771   {
772    "cell_type": "markdown",
773    "metadata": {},
774    "source": [
775     "`reverse` could be defines as `reverse == dup size take`"
776    ]
777   },
778   {
779    "cell_type": "markdown",
780    "metadata": {},
781    "source": [
782     "### `remove`"
783    ]
784   },
785   {
786    "cell_type": "code",
787    "execution_count": 37,
788    "metadata": {},
789    "outputs": [
790     {
791      "name": "stdout",
792      "output_type": "stream",
793      "text": [
794       "[2 3 1 4]\n"
795      ]
796     }
797    ],
798    "source": [
799     "J('[1 2 3 1 4] 1 remove')"
800    ]
801   },
802   {
803    "cell_type": "markdown",
804    "metadata": {},
805    "source": [
806     "### `reverse`"
807    ]
808   },
809   {
810    "cell_type": "code",
811    "execution_count": 38,
812    "metadata": {
813     "scrolled": true
814    },
815    "outputs": [
816     {
817      "name": "stdout",
818      "output_type": "stream",
819      "text": [
820       "[4 3 2 1]\n"
821      ]
822     }
823    ],
824    "source": [
825     "J('[1 2 3 4] reverse')"
826    ]
827   },
828   {
829    "cell_type": "markdown",
830    "metadata": {},
831    "source": [
832     "### `size`"
833    ]
834   },
835   {
836    "cell_type": "code",
837    "execution_count": 39,
838    "metadata": {
839     "scrolled": true
840    },
841    "outputs": [
842     {
843      "name": "stdout",
844      "output_type": "stream",
845      "text": [
846       "4\n"
847      ]
848     }
849    ],
850    "source": [
851     "J('[1 1 1 1] size')"
852    ]
853   },
854   {
855    "cell_type": "markdown",
856    "metadata": {},
857    "source": [
858     "### `swaack`\n",
859     "\"Swap stack\" swap the list on the top of the stack for the stack, and put the old stack on top of the new one.  Think of it as a context switch.  Niether of the lists/stacks change their order."
860    ]
861   },
862   {
863    "cell_type": "code",
864    "execution_count": 40,
865    "metadata": {
866     "scrolled": true
867    },
868    "outputs": [
869     {
870      "name": "stdout",
871      "output_type": "stream",
872      "text": [
873       "6 5 4 [3 2 1]\n"
874      ]
875     }
876    ],
877    "source": [
878     "J('1 2 3 [4 5 6] swaack')"
879    ]
880   },
881   {
882    "cell_type": "markdown",
883    "metadata": {},
884    "source": [
885     "### `choice` `select`"
886    ]
887   },
888   {
889    "cell_type": "code",
890    "execution_count": 41,
891    "metadata": {
892     "scrolled": true
893    },
894    "outputs": [
895     {
896      "name": "stdout",
897      "output_type": "stream",
898      "text": [
899       "9\n"
900      ]
901     }
902    ],
903    "source": [
904     "J('23 9 1 choice')"
905    ]
906   },
907   {
908    "cell_type": "code",
909    "execution_count": 42,
910    "metadata": {},
911    "outputs": [
912     {
913      "name": "stdout",
914      "output_type": "stream",
915      "text": [
916       "23\n"
917      ]
918     }
919    ],
920    "source": [
921     "J('23 9 0 choice')"
922    ]
923   },
924   {
925    "cell_type": "code",
926    "execution_count": 43,
927    "metadata": {
928     "scrolled": true
929    },
930    "outputs": [
931     {
932      "name": "stdout",
933      "output_type": "stream",
934      "text": [
935       "9\n"
936      ]
937     }
938    ],
939    "source": [
940     "J('[23 9 7] 1 select')  # select is basically getitem, should retire it?"
941    ]
942   },
943   {
944    "cell_type": "code",
945    "execution_count": 44,
946    "metadata": {},
947    "outputs": [
948     {
949      "name": "stdout",
950      "output_type": "stream",
951      "text": [
952       "23\n"
953      ]
954     }
955    ],
956    "source": [
957     "J('[23 9 7] 0 select')"
958    ]
959   },
960   {
961    "cell_type": "markdown",
962    "metadata": {},
963    "source": [
964     "### `zip`"
965    ]
966   },
967   {
968    "cell_type": "code",
969    "execution_count": 45,
970    "metadata": {},
971    "outputs": [
972     {
973      "name": "stdout",
974      "output_type": "stream",
975      "text": [
976       "[[6 1] [5 2] [4 3]]\n"
977      ]
978     }
979    ],
980    "source": [
981     "J('[1 2 3] [6 5 4] zip')"
982    ]
983   },
984   {
985    "cell_type": "code",
986    "execution_count": 46,
987    "metadata": {},
988    "outputs": [
989     {
990      "name": "stdout",
991      "output_type": "stream",
992      "text": [
993       "[7 7 7]\n"
994      ]
995     }
996    ],
997    "source": [
998     "J('[1 2 3] [6 5 4] zip [sum] map')"
999    ]
1000   },
1001   {
1002    "cell_type": "markdown",
1003    "metadata": {},
1004    "source": [
1005     "# Math words"
1006    ]
1007   },
1008   {
1009    "cell_type": "markdown",
1010    "metadata": {},
1011    "source": [
1012     "### `+` `add`"
1013    ]
1014   },
1015   {
1016    "cell_type": "code",
1017    "execution_count": 47,
1018    "metadata": {},
1019    "outputs": [
1020     {
1021      "name": "stdout",
1022      "output_type": "stream",
1023      "text": [
1024       "32\n"
1025      ]
1026     }
1027    ],
1028    "source": [
1029     "J('23 9 +')"
1030    ]
1031   },
1032   {
1033    "cell_type": "markdown",
1034    "metadata": {},
1035    "source": [
1036     "### `-` `sub`"
1037    ]
1038   },
1039   {
1040    "cell_type": "code",
1041    "execution_count": 48,
1042    "metadata": {},
1043    "outputs": [
1044     {
1045      "name": "stdout",
1046      "output_type": "stream",
1047      "text": [
1048       "14\n"
1049      ]
1050     }
1051    ],
1052    "source": [
1053     "J('23 9 -')"
1054    ]
1055   },
1056   {
1057    "cell_type": "markdown",
1058    "metadata": {},
1059    "source": [
1060     "### `*` `mul`"
1061    ]
1062   },
1063   {
1064    "cell_type": "code",
1065    "execution_count": 49,
1066    "metadata": {},
1067    "outputs": [
1068     {
1069      "name": "stdout",
1070      "output_type": "stream",
1071      "text": [
1072       "207\n"
1073      ]
1074     }
1075    ],
1076    "source": [
1077     "J('23 9 *')"
1078    ]
1079   },
1080   {
1081    "cell_type": "markdown",
1082    "metadata": {},
1083    "source": [
1084     "### `/` `div` `floordiv` `truediv`"
1085    ]
1086   },
1087   {
1088    "cell_type": "code",
1089    "execution_count": 50,
1090    "metadata": {
1091     "scrolled": true
1092    },
1093    "outputs": [
1094     {
1095      "name": "stdout",
1096      "output_type": "stream",
1097      "text": [
1098       "2.5555555555555554\n"
1099      ]
1100     }
1101    ],
1102    "source": [
1103     "J('23 9 /')"
1104    ]
1105   },
1106   {
1107    "cell_type": "code",
1108    "execution_count": 51,
1109    "metadata": {
1110     "scrolled": false
1111    },
1112    "outputs": [
1113     {
1114      "name": "stdout",
1115      "output_type": "stream",
1116      "text": [
1117       "-2.5555555555555554\n"
1118      ]
1119     }
1120    ],
1121    "source": [
1122     "J('23 -9 truediv')"
1123    ]
1124   },
1125   {
1126    "cell_type": "code",
1127    "execution_count": 52,
1128    "metadata": {
1129     "scrolled": false
1130    },
1131    "outputs": [
1132     {
1133      "name": "stdout",
1134      "output_type": "stream",
1135      "text": [
1136       "2.5555555555555554\n"
1137      ]
1138     }
1139    ],
1140    "source": [
1141     "J('23 9 div')"
1142    ]
1143   },
1144   {
1145    "cell_type": "code",
1146    "execution_count": 53,
1147    "metadata": {
1148     "scrolled": false
1149    },
1150    "outputs": [
1151     {
1152      "name": "stdout",
1153      "output_type": "stream",
1154      "text": [
1155       "2\n"
1156      ]
1157     }
1158    ],
1159    "source": [
1160     "J('23 9 floordiv')"
1161    ]
1162   },
1163   {
1164    "cell_type": "code",
1165    "execution_count": 54,
1166    "metadata": {
1167     "scrolled": false
1168    },
1169    "outputs": [
1170     {
1171      "name": "stdout",
1172      "output_type": "stream",
1173      "text": [
1174       "-2.5555555555555554\n"
1175      ]
1176     }
1177    ],
1178    "source": [
1179     "J('23 -9 div')"
1180    ]
1181   },
1182   {
1183    "cell_type": "code",
1184    "execution_count": 55,
1185    "metadata": {
1186     "scrolled": false
1187    },
1188    "outputs": [
1189     {
1190      "name": "stdout",
1191      "output_type": "stream",
1192      "text": [
1193       "-3\n"
1194      ]
1195     }
1196    ],
1197    "source": [
1198     "J('23 -9 floordiv')"
1199    ]
1200   },
1201   {
1202    "cell_type": "markdown",
1203    "metadata": {},
1204    "source": [
1205     "### `%` `mod` `modulus` `rem` `remainder`"
1206    ]
1207   },
1208   {
1209    "cell_type": "code",
1210    "execution_count": 56,
1211    "metadata": {},
1212    "outputs": [
1213     {
1214      "name": "stdout",
1215      "output_type": "stream",
1216      "text": [
1217       "5\n"
1218      ]
1219     }
1220    ],
1221    "source": [
1222     "J('23 9 %')"
1223    ]
1224   },
1225   {
1226    "cell_type": "markdown",
1227    "metadata": {},
1228    "source": [
1229     "### `neg`"
1230    ]
1231   },
1232   {
1233    "cell_type": "code",
1234    "execution_count": 57,
1235    "metadata": {},
1236    "outputs": [
1237     {
1238      "name": "stdout",
1239      "output_type": "stream",
1240      "text": [
1241       "-23 5\n"
1242      ]
1243     }
1244    ],
1245    "source": [
1246     "J('23 neg -5 neg')"
1247    ]
1248   },
1249   {
1250    "cell_type": "markdown",
1251    "metadata": {},
1252    "source": [
1253     "### pow"
1254    ]
1255   },
1256   {
1257    "cell_type": "code",
1258    "execution_count": 58,
1259    "metadata": {},
1260    "outputs": [
1261     {
1262      "name": "stdout",
1263      "output_type": "stream",
1264      "text": [
1265       "1024\n"
1266      ]
1267     }
1268    ],
1269    "source": [
1270     "J('2 10 pow')"
1271    ]
1272   },
1273   {
1274    "cell_type": "markdown",
1275    "metadata": {},
1276    "source": [
1277     "### `sqr` `sqrt`"
1278    ]
1279   },
1280   {
1281    "cell_type": "code",
1282    "execution_count": 59,
1283    "metadata": {},
1284    "outputs": [
1285     {
1286      "name": "stdout",
1287      "output_type": "stream",
1288      "text": [
1289       "529\n"
1290      ]
1291     }
1292    ],
1293    "source": [
1294     "J('23 sqr')"
1295    ]
1296   },
1297   {
1298    "cell_type": "code",
1299    "execution_count": 60,
1300    "metadata": {},
1301    "outputs": [
1302     {
1303      "name": "stdout",
1304      "output_type": "stream",
1305      "text": [
1306       "4.795831523312719\n"
1307      ]
1308     }
1309    ],
1310    "source": [
1311     "J('23 sqrt')"
1312    ]
1313   },
1314   {
1315    "cell_type": "markdown",
1316    "metadata": {},
1317    "source": [
1318     "### `++` `succ` `--` `pred`"
1319    ]
1320   },
1321   {
1322    "cell_type": "code",
1323    "execution_count": 61,
1324    "metadata": {},
1325    "outputs": [
1326     {
1327      "name": "stdout",
1328      "output_type": "stream",
1329      "text": [
1330       "2\n"
1331      ]
1332     }
1333    ],
1334    "source": [
1335     "J('1 ++')"
1336    ]
1337   },
1338   {
1339    "cell_type": "code",
1340    "execution_count": 62,
1341    "metadata": {
1342     "scrolled": true
1343    },
1344    "outputs": [
1345     {
1346      "name": "stdout",
1347      "output_type": "stream",
1348      "text": [
1349       "0\n"
1350      ]
1351     }
1352    ],
1353    "source": [
1354     "J('1 --')"
1355    ]
1356   },
1357   {
1358    "cell_type": "markdown",
1359    "metadata": {},
1360    "source": [
1361     "### `<<` `lshift` `>>` `rshift`"
1362    ]
1363   },
1364   {
1365    "cell_type": "code",
1366    "execution_count": 63,
1367    "metadata": {},
1368    "outputs": [
1369     {
1370      "name": "stdout",
1371      "output_type": "stream",
1372      "text": [
1373       "16\n"
1374      ]
1375     }
1376    ],
1377    "source": [
1378     "J('8 1 <<')"
1379    ]
1380   },
1381   {
1382    "cell_type": "code",
1383    "execution_count": 64,
1384    "metadata": {},
1385    "outputs": [
1386     {
1387      "name": "stdout",
1388      "output_type": "stream",
1389      "text": [
1390       "4\n"
1391      ]
1392     }
1393    ],
1394    "source": [
1395     "J('8 1 >>')"
1396    ]
1397   },
1398   {
1399    "cell_type": "markdown",
1400    "metadata": {},
1401    "source": [
1402     "### `average`"
1403    ]
1404   },
1405   {
1406    "cell_type": "code",
1407    "execution_count": 65,
1408    "metadata": {},
1409    "outputs": [
1410     {
1411      "name": "stdout",
1412      "output_type": "stream",
1413      "text": [
1414       "2.75\n"
1415      ]
1416     }
1417    ],
1418    "source": [
1419     "J('[1 2 3 5] average')"
1420    ]
1421   },
1422   {
1423    "cell_type": "markdown",
1424    "metadata": {},
1425    "source": [
1426     "### `range` `range_to_zero` `down_to_zero`"
1427    ]
1428   },
1429   {
1430    "cell_type": "code",
1431    "execution_count": 66,
1432    "metadata": {},
1433    "outputs": [
1434     {
1435      "name": "stdout",
1436      "output_type": "stream",
1437      "text": [
1438       "[4 3 2 1 0]\n"
1439      ]
1440     }
1441    ],
1442    "source": [
1443     "J('5 range')"
1444    ]
1445   },
1446   {
1447    "cell_type": "code",
1448    "execution_count": 67,
1449    "metadata": {},
1450    "outputs": [
1451     {
1452      "name": "stdout",
1453      "output_type": "stream",
1454      "text": [
1455       "[0 1 2 3 4 5]\n"
1456      ]
1457     }
1458    ],
1459    "source": [
1460     "J('5 range_to_zero')"
1461    ]
1462   },
1463   {
1464    "cell_type": "code",
1465    "execution_count": 68,
1466    "metadata": {},
1467    "outputs": [
1468     {
1469      "name": "stdout",
1470      "output_type": "stream",
1471      "text": [
1472       "5 4 3 2 1 0\n"
1473      ]
1474     }
1475    ],
1476    "source": [
1477     "J('5 down_to_zero')"
1478    ]
1479   },
1480   {
1481    "cell_type": "markdown",
1482    "metadata": {},
1483    "source": [
1484     "### `product`"
1485    ]
1486   },
1487   {
1488    "cell_type": "code",
1489    "execution_count": 69,
1490    "metadata": {},
1491    "outputs": [
1492     {
1493      "name": "stdout",
1494      "output_type": "stream",
1495      "text": [
1496       "30\n"
1497      ]
1498     }
1499    ],
1500    "source": [
1501     "J('[1 2 3 5] product')"
1502    ]
1503   },
1504   {
1505    "cell_type": "markdown",
1506    "metadata": {},
1507    "source": [
1508     "### `sum`"
1509    ]
1510   },
1511   {
1512    "cell_type": "code",
1513    "execution_count": 70,
1514    "metadata": {},
1515    "outputs": [
1516     {
1517      "name": "stdout",
1518      "output_type": "stream",
1519      "text": [
1520       "11\n"
1521      ]
1522     }
1523    ],
1524    "source": [
1525     "J('[1 2 3 5] sum')"
1526    ]
1527   },
1528   {
1529    "cell_type": "markdown",
1530    "metadata": {},
1531    "source": [
1532     "### `min`"
1533    ]
1534   },
1535   {
1536    "cell_type": "code",
1537    "execution_count": 71,
1538    "metadata": {},
1539    "outputs": [
1540     {
1541      "name": "stdout",
1542      "output_type": "stream",
1543      "text": [
1544       "1\n"
1545      ]
1546     }
1547    ],
1548    "source": [
1549     "J('[1 2 3 5] min')"
1550    ]
1551   },
1552   {
1553    "cell_type": "markdown",
1554    "metadata": {},
1555    "source": [
1556     "### `gcd`"
1557    ]
1558   },
1559   {
1560    "cell_type": "code",
1561    "execution_count": 72,
1562    "metadata": {},
1563    "outputs": [
1564     {
1565      "name": "stdout",
1566      "output_type": "stream",
1567      "text": [
1568       "15\n"
1569      ]
1570     }
1571    ],
1572    "source": [
1573     "J('45 30 gcd')"
1574    ]
1575   },
1576   {
1577    "cell_type": "markdown",
1578    "metadata": {},
1579    "source": [
1580     "### `least_fraction`\n",
1581     "If we represent fractions as a quoted pair of integers [q d] this word reduces them to their ... least common factors or whatever."
1582    ]
1583   },
1584   {
1585    "cell_type": "code",
1586    "execution_count": 73,
1587    "metadata": {},
1588    "outputs": [
1589     {
1590      "name": "stdout",
1591      "output_type": "stream",
1592      "text": [
1593       "[3.0 2.0]\n"
1594      ]
1595     }
1596    ],
1597    "source": [
1598     "J('[45 30] least_fraction')"
1599    ]
1600   },
1601   {
1602    "cell_type": "code",
1603    "execution_count": 74,
1604    "metadata": {},
1605    "outputs": [
1606     {
1607      "name": "stdout",
1608      "output_type": "stream",
1609      "text": [
1610       "[23.0 12.0]\n"
1611      ]
1612     }
1613    ],
1614    "source": [
1615     "J('[23 12] least_fraction')"
1616    ]
1617   },
1618   {
1619    "cell_type": "markdown",
1620    "metadata": {},
1621    "source": [
1622     "# Logic and Comparison"
1623    ]
1624   },
1625   {
1626    "cell_type": "markdown",
1627    "metadata": {},
1628    "source": [
1629     "### `?` `truthy`\n",
1630     "Get the Boolean value of the item on the top of the stack."
1631    ]
1632   },
1633   {
1634    "cell_type": "code",
1635    "execution_count": 75,
1636    "metadata": {},
1637    "outputs": [
1638     {
1639      "name": "stdout",
1640      "output_type": "stream",
1641      "text": [
1642       "True\n"
1643      ]
1644     }
1645    ],
1646    "source": [
1647     "J('23 truthy')"
1648    ]
1649   },
1650   {
1651    "cell_type": "code",
1652    "execution_count": 76,
1653    "metadata": {},
1654    "outputs": [
1655     {
1656      "name": "stdout",
1657      "output_type": "stream",
1658      "text": [
1659       "False\n"
1660      ]
1661     }
1662    ],
1663    "source": [
1664     "J('[] truthy')  # Python semantics."
1665    ]
1666   },
1667   {
1668    "cell_type": "code",
1669    "execution_count": 77,
1670    "metadata": {},
1671    "outputs": [
1672     {
1673      "name": "stdout",
1674      "output_type": "stream",
1675      "text": [
1676       "False\n"
1677      ]
1678     }
1679    ],
1680    "source": [
1681     "J('0 truthy')"
1682    ]
1683   },
1684   {
1685    "cell_type": "markdown",
1686    "metadata": {},
1687    "source": [
1688     "    ? == dup truthy"
1689    ]
1690   },
1691   {
1692    "cell_type": "code",
1693    "execution_count": 78,
1694    "metadata": {},
1695    "outputs": [
1696     {
1697      "name": "stdout",
1698      "output_type": "stream",
1699      "text": [
1700       "        • 23 ?\n",
1701       "     23 • ?\n",
1702       "     23 • dup truthy\n",
1703       "  23 23 • truthy\n",
1704       "23 True • \n"
1705      ]
1706     }
1707    ],
1708    "source": [
1709     "V('23 ?')"
1710    ]
1711   },
1712   {
1713    "cell_type": "code",
1714    "execution_count": 79,
1715    "metadata": {},
1716    "outputs": [
1717     {
1718      "name": "stdout",
1719      "output_type": "stream",
1720      "text": [
1721       "[] False\n"
1722      ]
1723     }
1724    ],
1725    "source": [
1726     "J('[] ?')"
1727    ]
1728   },
1729   {
1730    "cell_type": "code",
1731    "execution_count": 80,
1732    "metadata": {},
1733    "outputs": [
1734     {
1735      "name": "stdout",
1736      "output_type": "stream",
1737      "text": [
1738       "0 False\n"
1739      ]
1740     }
1741    ],
1742    "source": [
1743     "J('0 ?')"
1744    ]
1745   },
1746   {
1747    "cell_type": "markdown",
1748    "metadata": {},
1749    "source": [
1750     "### `&` `and` "
1751    ]
1752   },
1753   {
1754    "cell_type": "code",
1755    "execution_count": 81,
1756    "metadata": {},
1757    "outputs": [
1758     {
1759      "name": "stdout",
1760      "output_type": "stream",
1761      "text": [
1762       "1\n"
1763      ]
1764     }
1765    ],
1766    "source": [
1767     "J('23 9 &')"
1768    ]
1769   },
1770   {
1771    "cell_type": "markdown",
1772    "metadata": {},
1773    "source": [
1774     "### `!=` `<>` `ne`"
1775    ]
1776   },
1777   {
1778    "cell_type": "code",
1779    "execution_count": 82,
1780    "metadata": {},
1781    "outputs": [
1782     {
1783      "name": "stdout",
1784      "output_type": "stream",
1785      "text": [
1786       "True\n"
1787      ]
1788     }
1789    ],
1790    "source": [
1791     "J('23 9 !=')"
1792    ]
1793   },
1794   {
1795    "cell_type": "markdown",
1796    "metadata": {},
1797    "source": [
1798     "The usual suspects:\n",
1799     "- `<` `lt`\n",
1800     "- `<=` `le`  \n",
1801     "- `=` `eq`\n",
1802     "- `>` `gt`\n",
1803     "- `>=` `ge`\n",
1804     "- `not`\n",
1805     "- `or`"
1806    ]
1807   },
1808   {
1809    "cell_type": "markdown",
1810    "metadata": {},
1811    "source": [
1812     "### `^` `xor`"
1813    ]
1814   },
1815   {
1816    "cell_type": "code",
1817    "execution_count": 83,
1818    "metadata": {},
1819    "outputs": [
1820     {
1821      "name": "stdout",
1822      "output_type": "stream",
1823      "text": [
1824       "0\n"
1825      ]
1826     }
1827    ],
1828    "source": [
1829     "J('1 1 ^')"
1830    ]
1831   },
1832   {
1833    "cell_type": "code",
1834    "execution_count": 84,
1835    "metadata": {},
1836    "outputs": [
1837     {
1838      "name": "stdout",
1839      "output_type": "stream",
1840      "text": [
1841       "1\n"
1842      ]
1843     }
1844    ],
1845    "source": [
1846     "J('1 0 ^')"
1847    ]
1848   },
1849   {
1850    "cell_type": "markdown",
1851    "metadata": {},
1852    "source": [
1853     "# Miscellaneous"
1854    ]
1855   },
1856   {
1857    "cell_type": "markdown",
1858    "metadata": {},
1859    "source": [
1860     "### `help`"
1861    ]
1862   },
1863   {
1864    "cell_type": "code",
1865    "execution_count": 85,
1866    "metadata": {
1867     "scrolled": true
1868    },
1869    "outputs": [
1870     {
1871      "name": "stdout",
1872      "output_type": "stream",
1873      "text": [
1874       "\n",
1875       "==== Help on help ====\n",
1876       "\n",
1877       "Accepts a quoted symbol on the top of the stack and prints its docs.\n",
1878       "\n",
1879       "---- end (help)\n",
1880       "\n",
1881       "\n"
1882      ]
1883     }
1884    ],
1885    "source": [
1886     "J('[help] help')"
1887    ]
1888   },
1889   {
1890    "cell_type": "markdown",
1891    "metadata": {},
1892    "source": [
1893     "### `parse`"
1894    ]
1895   },
1896   {
1897    "cell_type": "code",
1898    "execution_count": 86,
1899    "metadata": {
1900     "scrolled": true
1901    },
1902    "outputs": [
1903     {
1904      "name": "stdout",
1905      "output_type": "stream",
1906      "text": [
1907       "\n",
1908       "==== Help on parse ====\n",
1909       "\n",
1910       "Parse the string on the stack to a Joy expression.\n",
1911       "\n",
1912       "---- end (parse)\n",
1913       "\n",
1914       "\n"
1915      ]
1916     }
1917    ],
1918    "source": [
1919     "J('[parse] help')"
1920    ]
1921   },
1922   {
1923    "cell_type": "code",
1924    "execution_count": 87,
1925    "metadata": {},
1926    "outputs": [
1927     {
1928      "name": "stdout",
1929      "output_type": "stream",
1930      "text": [
1931       "1 [2 [3] dup]\n"
1932      ]
1933     }
1934    ],
1935    "source": [
1936     "J('1 \"2 [3] dup\" parse')"
1937    ]
1938   },
1939   {
1940    "cell_type": "markdown",
1941    "metadata": {},
1942    "source": [
1943     "### `run`\n",
1944     "Evaluate a quoted Joy sequence."
1945    ]
1946   },
1947   {
1948    "cell_type": "code",
1949    "execution_count": 88,
1950    "metadata": {},
1951    "outputs": [
1952     {
1953      "name": "stdout",
1954      "output_type": "stream",
1955      "text": [
1956       "[5]\n"
1957      ]
1958     }
1959    ],
1960    "source": [
1961     "J('[1 2 dup + +] run')"
1962    ]
1963   },
1964   {
1965    "cell_type": "markdown",
1966    "metadata": {},
1967    "source": [
1968     "# Combinators"
1969    ]
1970   },
1971   {
1972    "cell_type": "markdown",
1973    "metadata": {},
1974    "source": [
1975     "### `app1` `app2` `app3`"
1976    ]
1977   },
1978   {
1979    "cell_type": "code",
1980    "execution_count": 89,
1981    "metadata": {},
1982    "outputs": [
1983     {
1984      "name": "stdout",
1985      "output_type": "stream",
1986      "text": [
1987       "\n",
1988       "==== Help on app1 ====\n",
1989       "\n",
1990       "Given a quoted program on TOS and anything as the second stack item run\n",
1991       "the program and replace the two args with the first result of the\n",
1992       "program.\n",
1993       "::\n",
1994       "\n",
1995       "                 ... x [Q] . app1\n",
1996       "        -----------------------------------\n",
1997       "           ... [x ...] [Q] . infra first\n",
1998       "\n",
1999       "---- end (app1)\n",
2000       "\n",
2001       "\n"
2002      ]
2003     }
2004    ],
2005    "source": [
2006     "J('[app1] help')"
2007    ]
2008   },
2009   {
2010    "cell_type": "code",
2011    "execution_count": 90,
2012    "metadata": {},
2013    "outputs": [
2014     {
2015      "name": "stdout",
2016      "output_type": "stream",
2017      "text": [
2018       "10 160\n"
2019      ]
2020     }
2021    ],
2022    "source": [
2023     "J('10 4 [sqr *] app1')"
2024    ]
2025   },
2026   {
2027    "cell_type": "code",
2028    "execution_count": 91,
2029    "metadata": {},
2030    "outputs": [
2031     {
2032      "name": "stdout",
2033      "output_type": "stream",
2034      "text": [
2035       "10 90 160\n"
2036      ]
2037     }
2038    ],
2039    "source": [
2040     "J('10 3 4 [sqr *] app2')"
2041    ]
2042   },
2043   {
2044    "cell_type": "code",
2045    "execution_count": 92,
2046    "metadata": {},
2047    "outputs": [
2048     {
2049      "name": "stdout",
2050      "output_type": "stream",
2051      "text": [
2052       "\n",
2053       "==== Help on app2 ====\n",
2054       "\n",
2055       "Like app1 with two items.\n",
2056       "::\n",
2057       "\n",
2058       "               ... y x [Q] . app2\n",
2059       "        -----------------------------------\n",
2060       "           ... [y ...] [Q] . infra first\n",
2061       "               [x ...] [Q]   infra first\n",
2062       "\n",
2063       "---- end (app2)\n",
2064       "\n",
2065       "\n"
2066      ]
2067     }
2068    ],
2069    "source": [
2070     "J('[app2] help')"
2071    ]
2072   },
2073   {
2074    "cell_type": "code",
2075    "execution_count": 93,
2076    "metadata": {},
2077    "outputs": [
2078     {
2079      "name": "stdout",
2080      "output_type": "stream",
2081      "text": [
2082       "10 40 90 160\n"
2083      ]
2084     }
2085    ],
2086    "source": [
2087     "J('10 2 3 4 [sqr *] app3')"
2088    ]
2089   },
2090   {
2091    "cell_type": "markdown",
2092    "metadata": {},
2093    "source": [
2094     "### `anamorphism`\n",
2095     "Given an initial value, a predicate function `[P]`, and a generator function `[G]`, the `anamorphism` combinator creates a sequence.\n",
2096     "\n",
2097     "       n [P] [G] anamorphism\n",
2098     "    ---------------------------\n",
2099     "              [...]\n",
2100     "\n",
2101     "Example, `range`:\n",
2102     "\n",
2103     "    range == [0 <=] [1 - dup] anamorphism"
2104    ]
2105   },
2106   {
2107    "cell_type": "code",
2108    "execution_count": 94,
2109    "metadata": {},
2110    "outputs": [
2111     {
2112      "name": "stdout",
2113      "output_type": "stream",
2114      "text": [
2115       "[2 1 0]\n"
2116      ]
2117     }
2118    ],
2119    "source": [
2120     "J('3 [0 <=] [1 - dup] anamorphism')"
2121    ]
2122   },
2123   {
2124    "cell_type": "markdown",
2125    "metadata": {},
2126    "source": [
2127     "### `branch`"
2128    ]
2129   },
2130   {
2131    "cell_type": "code",
2132    "execution_count": 95,
2133    "metadata": {},
2134    "outputs": [
2135     {
2136      "name": "stdout",
2137      "output_type": "stream",
2138      "text": [
2139       "12\n"
2140      ]
2141     }
2142    ],
2143    "source": [
2144     "J('3 4 1 [+] [*] branch')"
2145    ]
2146   },
2147   {
2148    "cell_type": "code",
2149    "execution_count": 96,
2150    "metadata": {},
2151    "outputs": [
2152     {
2153      "name": "stdout",
2154      "output_type": "stream",
2155      "text": [
2156       "7\n"
2157      ]
2158     }
2159    ],
2160    "source": [
2161     "J('3 4 0 [+] [*] branch')"
2162    ]
2163   },
2164   {
2165    "cell_type": "markdown",
2166    "metadata": {},
2167    "source": [
2168     "### `cleave`\n",
2169     "    ... x [P] [Q] cleave\n",
2170     "\n",
2171     "From the original Joy docs: \"The cleave combinator expects two quotations, and below that an item `x`\n",
2172     "It first executes `[P]`, with `x` on top, and saves the top result element.\n",
2173     "Then it executes `[Q]`, again with `x`, and saves the top result.\n",
2174     "Finally it restores the stack to what it was below `x` and pushes the two\n",
2175     "results P(X) and Q(X).\"\n",
2176     "\n",
2177     "Note that `P` and `Q` can use items from the stack freely, since the stack (below `x`) is restored.  `cleave` is a kind of *parallel* primitive, and it would make sense to create a version that uses, e.g. Python threads or something, to actually run `P` and `Q` concurrently.  The current implementation of `cleave` is a definition in terms of `app2`:\n",
2178     "\n",
2179     "    cleave == [i] app2 [popd] dip"
2180    ]
2181   },
2182   {
2183    "cell_type": "code",
2184    "execution_count": 97,
2185    "metadata": {},
2186    "outputs": [
2187     {
2188      "name": "stdout",
2189      "output_type": "stream",
2190      "text": [
2191       "10 12 8\n"
2192      ]
2193     }
2194    ],
2195    "source": [
2196     "J('10 2 [+] [-] cleave')"
2197    ]
2198   },
2199   {
2200    "cell_type": "markdown",
2201    "metadata": {},
2202    "source": [
2203     "### `dip` `dipd` `dipdd`"
2204    ]
2205   },
2206   {
2207    "cell_type": "code",
2208    "execution_count": 98,
2209    "metadata": {},
2210    "outputs": [
2211     {
2212      "name": "stdout",
2213      "output_type": "stream",
2214      "text": [
2215       "1 2 7 5\n"
2216      ]
2217     }
2218    ],
2219    "source": [
2220     "J('1 2 3 4 5 [+] dip')"
2221    ]
2222   },
2223   {
2224    "cell_type": "code",
2225    "execution_count": 99,
2226    "metadata": {},
2227    "outputs": [
2228     {
2229      "name": "stdout",
2230      "output_type": "stream",
2231      "text": [
2232       "1 5 4 5\n"
2233      ]
2234     }
2235    ],
2236    "source": [
2237     "J('1 2 3 4 5 [+] dipd')"
2238    ]
2239   },
2240   {
2241    "cell_type": "code",
2242    "execution_count": 100,
2243    "metadata": {},
2244    "outputs": [
2245     {
2246      "name": "stdout",
2247      "output_type": "stream",
2248      "text": [
2249       "3 3 4 5\n"
2250      ]
2251     }
2252    ],
2253    "source": [
2254     "J('1 2 3 4 5 [+] dipdd')"
2255    ]
2256   },
2257   {
2258    "cell_type": "markdown",
2259    "metadata": {},
2260    "source": [
2261     "### `dupdip`\n",
2262     "Expects a quoted program `[Q]` on the stack and some item under it, `dup` the item and `dip` the quoted program under it.\n",
2263     "\n",
2264     "    n [Q] dupdip == n Q n"
2265    ]
2266   },
2267   {
2268    "cell_type": "code",
2269    "execution_count": 101,
2270    "metadata": {},
2271    "outputs": [
2272     {
2273      "name": "stdout",
2274      "output_type": "stream",
2275      "text": [
2276       "        • 23 [++] dupdip *\n",
2277       "     23 • [++] dupdip *\n",
2278       "23 [++] • dupdip *\n",
2279       "     23 • ++ 23 *\n",
2280       "     24 • 23 *\n",
2281       "  24 23 • *\n",
2282       "    552 • \n"
2283      ]
2284     }
2285    ],
2286    "source": [
2287     "V('23 [++] dupdip *')  # N(N + 1)"
2288    ]
2289   },
2290   {
2291    "cell_type": "markdown",
2292    "metadata": {},
2293    "source": [
2294     "### `genrec` `primrec`"
2295    ]
2296   },
2297   {
2298    "cell_type": "code",
2299    "execution_count": 102,
2300    "metadata": {},
2301    "outputs": [
2302     {
2303      "name": "stdout",
2304      "output_type": "stream",
2305      "text": [
2306       "\n",
2307       "==== Help on genrec ====\n",
2308       "\n",
2309       "General Recursion Combinator.\n",
2310       "::\n",
2311       "\n",
2312       "                          [if] [then] [rec1] [rec2] genrec\n",
2313       "    ---------------------------------------------------------------------\n",
2314       "       [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte\n",
2315       "\n",
2316       "From \"Recursion Theory and Joy\" (j05cmp.html) by Manfred von Thun:\n",
2317       "\"The genrec combinator takes four program parameters in addition to\n",
2318       "whatever data parameters it needs. Fourth from the top is an if-part,\n",
2319       "followed by a then-part. If the if-part yields true, then the then-part\n",
2320       "is executed and the combinator terminates. The other two parameters are\n",
2321       "the rec1-part and the rec2-part. If the if-part yields false, the\n",
2322       "rec1-part is executed. Following that the four program parameters and\n",
2323       "the combinator are again pushed onto the stack bundled up in a quoted\n",
2324       "form. Then the rec2-part is executed, where it will find the bundled\n",
2325       "form. Typically it will then execute the bundled form, either with i or\n",
2326       "with app2, or some other combinator.\"\n",
2327       "\n",
2328       "The way to design one of these is to fix your base case [then] and the\n",
2329       "test [if], and then treat rec1 and rec2 as an else-part \"sandwiching\"\n",
2330       "a quotation of the whole function.\n",
2331       "\n",
2332       "For example, given a (general recursive) function 'F':\n",
2333       "::\n",
2334       "\n",
2335       "        F == [I] [T] [R1] [R2] genrec\n",
2336       "\n",
2337       "If the [I] if-part fails you must derive R1 and R2 from:\n",
2338       "::\n",
2339       "\n",
2340       "        ... R1 [F] R2\n",
2341       "\n",
2342       "Just set the stack arguments in front, and figure out what R1 and R2\n",
2343       "have to do to apply the quoted [F] in the proper way.  In effect, the\n",
2344       "genrec combinator turns into an ifte combinator with a quoted copy of\n",
2345       "the original definition in the else-part:\n",
2346       "::\n",
2347       "\n",
2348       "        F == [I] [T] [R1]   [R2] genrec\n",
2349       "          == [I] [T] [R1 [F] R2] ifte\n",
2350       "\n",
2351       "Primitive recursive functions are those where R2 == i.\n",
2352       "::\n",
2353       "\n",
2354       "        P == [I] [T] [R] tailrec\n",
2355       "          == [I] [T] [R [P] i] ifte\n",
2356       "          == [I] [T] [R P] ifte\n",
2357       "\n",
2358       "---- end (genrec)\n",
2359       "\n",
2360       "\n"
2361      ]
2362     }
2363    ],
2364    "source": [
2365     "J('[genrec] help')"
2366    ]
2367   },
2368   {
2369    "cell_type": "code",
2370    "execution_count": 103,
2371    "metadata": {},
2372    "outputs": [
2373     {
2374      "name": "stdout",
2375      "output_type": "stream",
2376      "text": [
2377       "6\n"
2378      ]
2379     }
2380    ],
2381    "source": [
2382     "J('3 [1 <=] [] [dup --] [i *] genrec')"
2383    ]
2384   },
2385   {
2386    "cell_type": "markdown",
2387    "metadata": {},
2388    "source": [
2389     "### `i`"
2390    ]
2391   },
2392   {
2393    "cell_type": "code",
2394    "execution_count": 104,
2395    "metadata": {
2396     "scrolled": true
2397    },
2398    "outputs": [
2399     {
2400      "name": "stdout",
2401      "output_type": "stream",
2402      "text": [
2403       "            • 1 2 3 [+ +] i\n",
2404       "          1 • 2 3 [+ +] i\n",
2405       "        1 2 • 3 [+ +] i\n",
2406       "      1 2 3 • [+ +] i\n",
2407       "1 2 3 [+ +] • i\n",
2408       "      1 2 3 • + +\n",
2409       "        1 5 • +\n",
2410       "          6 • \n"
2411      ]
2412     }
2413    ],
2414    "source": [
2415     "V('1 2 3 [+ +] i')"
2416    ]
2417   },
2418   {
2419    "cell_type": "markdown",
2420    "metadata": {},
2421    "source": [
2422     "### `ifte`\n",
2423     "    [predicate] [then] [else] ifte"
2424    ]
2425   },
2426   {
2427    "cell_type": "code",
2428    "execution_count": 105,
2429    "metadata": {},
2430    "outputs": [
2431     {
2432      "name": "stdout",
2433      "output_type": "stream",
2434      "text": [
2435       "3\n"
2436      ]
2437     }
2438    ],
2439    "source": [
2440     "J('1 2 [1] [+] [*] ifte')"
2441    ]
2442   },
2443   {
2444    "cell_type": "code",
2445    "execution_count": 106,
2446    "metadata": {},
2447    "outputs": [
2448     {
2449      "name": "stdout",
2450      "output_type": "stream",
2451      "text": [
2452       "2\n"
2453      ]
2454     }
2455    ],
2456    "source": [
2457     "J('1 2 [0] [+] [*] ifte')"
2458    ]
2459   },
2460   {
2461    "cell_type": "markdown",
2462    "metadata": {},
2463    "source": [
2464     "### `infra`"
2465    ]
2466   },
2467   {
2468    "cell_type": "code",
2469    "execution_count": 107,
2470    "metadata": {},
2471    "outputs": [
2472     {
2473      "name": "stdout",
2474      "output_type": "stream",
2475      "text": [
2476       "                    • 1 2 3 [4 5 6] [* +] infra\n",
2477       "                  1 • 2 3 [4 5 6] [* +] infra\n",
2478       "                1 2 • 3 [4 5 6] [* +] infra\n",
2479       "              1 2 3 • [4 5 6] [* +] infra\n",
2480       "      1 2 3 [4 5 6] • [* +] infra\n",
2481       "1 2 3 [4 5 6] [* +] • infra\n",
2482       "              6 5 4 • * + [3 2 1] swaack\n",
2483       "               6 20 • + [3 2 1] swaack\n",
2484       "                 26 • [3 2 1] swaack\n",
2485       "         26 [3 2 1] • swaack\n",
2486       "         1 2 3 [26] • \n"
2487      ]
2488     }
2489    ],
2490    "source": [
2491     "V('1 2 3 [4 5 6] [* +] infra')"
2492    ]
2493   },
2494   {
2495    "cell_type": "markdown",
2496    "metadata": {},
2497    "source": [
2498     "### `loop`"
2499    ]
2500   },
2501   {
2502    "cell_type": "code",
2503    "execution_count": 108,
2504    "metadata": {},
2505    "outputs": [
2506     {
2507      "name": "stdout",
2508      "output_type": "stream",
2509      "text": [
2510       "\n",
2511       "==== Help on loop ====\n",
2512       "\n",
2513       "Basic loop combinator.\n",
2514       "::\n",
2515       "\n",
2516       "           ... True [Q] loop\n",
2517       "        -----------------------\n",
2518       "              ... Q [Q] loop\n",
2519       "\n",
2520       "           ... False [Q] loop\n",
2521       "        ------------------------\n",
2522       "                  ...\n",
2523       "\n",
2524       "---- end (loop)\n",
2525       "\n",
2526       "\n"
2527      ]
2528     }
2529    ],
2530    "source": [
2531     "J('[loop] help')"
2532    ]
2533   },
2534   {
2535    "cell_type": "code",
2536    "execution_count": 109,
2537    "metadata": {},
2538    "outputs": [
2539     {
2540      "name": "stdout",
2541      "output_type": "stream",
2542      "text": [
2543       "              • 3 dup [1 - dup] loop\n",
2544       "            3 • dup [1 - dup] loop\n",
2545       "          3 3 • [1 - dup] loop\n",
2546       "3 3 [1 - dup] • loop\n",
2547       "            3 • 1 - dup [1 - dup] loop\n",
2548       "          3 1 • - dup [1 - dup] loop\n",
2549       "            2 • dup [1 - dup] loop\n",
2550       "          2 2 • [1 - dup] loop\n",
2551       "2 2 [1 - dup] • loop\n",
2552       "            2 • 1 - dup [1 - dup] loop\n",
2553       "          2 1 • - dup [1 - dup] loop\n",
2554       "            1 • dup [1 - dup] loop\n",
2555       "          1 1 • [1 - dup] loop\n",
2556       "1 1 [1 - dup] • loop\n",
2557       "            1 • 1 - dup [1 - dup] loop\n",
2558       "          1 1 • - dup [1 - dup] loop\n",
2559       "            0 • dup [1 - dup] loop\n",
2560       "          0 0 • [1 - dup] loop\n",
2561       "0 0 [1 - dup] • loop\n",
2562       "            0 • \n"
2563      ]
2564     }
2565    ],
2566    "source": [
2567     "V('3 dup [1 - dup] loop')"
2568    ]
2569   },
2570   {
2571    "cell_type": "markdown",
2572    "metadata": {},
2573    "source": [
2574     "### `map` `pam`"
2575    ]
2576   },
2577   {
2578    "cell_type": "code",
2579    "execution_count": 110,
2580    "metadata": {},
2581    "outputs": [
2582     {
2583      "name": "stdout",
2584      "output_type": "stream",
2585      "text": [
2586       "10 [10 20 30]\n"
2587      ]
2588     }
2589    ],
2590    "source": [
2591     "J('10 [1 2 3] [*] map')"
2592    ]
2593   },
2594   {
2595    "cell_type": "code",
2596    "execution_count": 111,
2597    "metadata": {},
2598    "outputs": [
2599     {
2600      "name": "stdout",
2601      "output_type": "stream",
2602      "text": [
2603       "10 5 [50 2.0 15 5]\n"
2604      ]
2605     }
2606    ],
2607    "source": [
2608     "J('10 5 [[*][/][+][-]] pam')"
2609    ]
2610   },
2611   {
2612    "cell_type": "markdown",
2613    "metadata": {},
2614    "source": [
2615     "### `nullary` `unary` `binary` `ternary`\n",
2616     "Run a quoted program enforcing [arity](https://en.wikipedia.org/wiki/Arity)."
2617    ]
2618   },
2619   {
2620    "cell_type": "code",
2621    "execution_count": 112,
2622    "metadata": {},
2623    "outputs": [
2624     {
2625      "name": "stdout",
2626      "output_type": "stream",
2627      "text": [
2628       "1 2 3 4 5 9\n"
2629      ]
2630     }
2631    ],
2632    "source": [
2633     "J('1 2 3 4 5 [+] nullary')"
2634    ]
2635   },
2636   {
2637    "cell_type": "code",
2638    "execution_count": 113,
2639    "metadata": {},
2640    "outputs": [
2641     {
2642      "name": "stdout",
2643      "output_type": "stream",
2644      "text": [
2645       "1 2 3 4 9\n"
2646      ]
2647     }
2648    ],
2649    "source": [
2650     "J('1 2 3 4 5 [+] unary')"
2651    ]
2652   },
2653   {
2654    "cell_type": "code",
2655    "execution_count": 114,
2656    "metadata": {},
2657    "outputs": [
2658     {
2659      "name": "stdout",
2660      "output_type": "stream",
2661      "text": [
2662       "1 2 3 9\n"
2663      ]
2664     }
2665    ],
2666    "source": [
2667     "J('1 2 3 4 5 [+] binary')  # + has arity 2 so this is technically pointless..."
2668    ]
2669   },
2670   {
2671    "cell_type": "code",
2672    "execution_count": 115,
2673    "metadata": {},
2674    "outputs": [
2675     {
2676      "name": "stdout",
2677      "output_type": "stream",
2678      "text": [
2679       "1 2 9\n"
2680      ]
2681     }
2682    ],
2683    "source": [
2684     "J('1 2 3 4 5 [+] ternary')"
2685    ]
2686   },
2687   {
2688    "cell_type": "markdown",
2689    "metadata": {},
2690    "source": [
2691     "### `step`"
2692    ]
2693   },
2694   {
2695    "cell_type": "code",
2696    "execution_count": 116,
2697    "metadata": {},
2698    "outputs": [
2699     {
2700      "name": "stdout",
2701      "output_type": "stream",
2702      "text": [
2703       "\n",
2704       "==== Help on step ====\n",
2705       "\n",
2706       "Run a quoted program on each item in a sequence.\n",
2707       "::\n",
2708       "\n",
2709       "           ... [] [Q] . step\n",
2710       "        -----------------------\n",
2711       "                  ... .\n",
2712       "\n",
2713       "\n",
2714       "           ... [a] [Q] . step\n",
2715       "        ------------------------\n",
2716       "                 ... a . Q\n",
2717       "\n",
2718       "\n",
2719       "           ... [a b c] [Q] . step\n",
2720       "        ----------------------------------------\n",
2721       "                     ... a . Q [b c] [Q] step\n",
2722       "\n",
2723       "The step combinator executes the quotation on each member of the list\n",
2724       "on top of the stack.\n",
2725       "\n",
2726       "---- end (step)\n",
2727       "\n",
2728       "\n"
2729      ]
2730     }
2731    ],
2732    "source": [
2733     "J('[step] help')"
2734    ]
2735   },
2736   {
2737    "cell_type": "code",
2738    "execution_count": 117,
2739    "metadata": {
2740     "scrolled": true
2741    },
2742    "outputs": [
2743     {
2744      "name": "stdout",
2745      "output_type": "stream",
2746      "text": [
2747       "              • 0 [1 2 3] [+] step\n",
2748       "            0 • [1 2 3] [+] step\n",
2749       "    0 [1 2 3] • [+] step\n",
2750       "0 [1 2 3] [+] • step\n",
2751       "      0 1 [+] • i [2 3] [+] step\n",
2752       "          0 1 • + [2 3] [+] step\n",
2753       "            1 • [2 3] [+] step\n",
2754       "      1 [2 3] • [+] step\n",
2755       "  1 [2 3] [+] • step\n",
2756       "      1 2 [+] • i [3] [+] step\n",
2757       "          1 2 • + [3] [+] step\n",
2758       "            3 • [3] [+] step\n",
2759       "        3 [3] • [+] step\n",
2760       "    3 [3] [+] • step\n",
2761       "      3 3 [+] • i\n",
2762       "          3 3 • +\n",
2763       "            6 • \n"
2764      ]
2765     }
2766    ],
2767    "source": [
2768     "V('0 [1 2 3] [+] step')"
2769    ]
2770   },
2771   {
2772    "cell_type": "markdown",
2773    "metadata": {},
2774    "source": [
2775     "### `times`"
2776    ]
2777   },
2778   {
2779    "cell_type": "code",
2780    "execution_count": 118,
2781    "metadata": {},
2782    "outputs": [
2783     {
2784      "name": "stdout",
2785      "output_type": "stream",
2786      "text": [
2787       "            • 3 2 1 2 [+] times\n",
2788       "          3 • 2 1 2 [+] times\n",
2789       "        3 2 • 1 2 [+] times\n",
2790       "      3 2 1 • 2 [+] times\n",
2791       "    3 2 1 2 • [+] times\n",
2792       "3 2 1 2 [+] • times\n",
2793       "      3 2 1 • + 1 [+] times\n",
2794       "        3 3 • 1 [+] times\n",
2795       "      3 3 1 • [+] times\n",
2796       "  3 3 1 [+] • times\n",
2797       "        3 3 • +\n",
2798       "          6 • \n"
2799      ]
2800     }
2801    ],
2802    "source": [
2803     "V('3 2 1 2 [+] times')"
2804    ]
2805   },
2806   {
2807    "cell_type": "markdown",
2808    "metadata": {},
2809    "source": [
2810     "### `b`"
2811    ]
2812   },
2813   {
2814    "cell_type": "code",
2815    "execution_count": 119,
2816    "metadata": {},
2817    "outputs": [
2818     {
2819      "name": "stdout",
2820      "output_type": "stream",
2821      "text": [
2822       "\n",
2823       "==== Help on b ====\n",
2824       "\n",
2825       "::\n",
2826       "\n",
2827       "        b == [i] dip i\n",
2828       "\n",
2829       "        ... [P] [Q] b == ... [P] i [Q] i\n",
2830       "        ... [P] [Q] b == ... P Q\n",
2831       "\n",
2832       "---- end (b)\n",
2833       "\n",
2834       "\n"
2835      ]
2836     }
2837    ],
2838    "source": [
2839     "J('[b] help')"
2840    ]
2841   },
2842   {
2843    "cell_type": "code",
2844    "execution_count": 120,
2845    "metadata": {},
2846    "outputs": [
2847     {
2848      "name": "stdout",
2849      "output_type": "stream",
2850      "text": [
2851       "            • 1 2 [3] [4] b\n",
2852       "          1 • 2 [3] [4] b\n",
2853       "        1 2 • [3] [4] b\n",
2854       "    1 2 [3] • [4] b\n",
2855       "1 2 [3] [4] • b\n",
2856       "        1 2 • 3 4\n",
2857       "      1 2 3 • 4\n",
2858       "    1 2 3 4 • \n"
2859      ]
2860     }
2861    ],
2862    "source": [
2863     "V('1 2 [3] [4] b')"
2864    ]
2865   },
2866   {
2867    "cell_type": "markdown",
2868    "metadata": {},
2869    "source": [
2870     "### `while`\n",
2871     "    [predicate] [body] while"
2872    ]
2873   },
2874   {
2875    "cell_type": "code",
2876    "execution_count": 121,
2877    "metadata": {},
2878    "outputs": [
2879     {
2880      "name": "stdout",
2881      "output_type": "stream",
2882      "text": [
2883       "3 2 1 0\n"
2884      ]
2885     }
2886    ],
2887    "source": [
2888     "J('3 [0 >] [dup --] while')"
2889    ]
2890   },
2891   {
2892    "cell_type": "markdown",
2893    "metadata": {},
2894    "source": [
2895     "### `x`"
2896    ]
2897   },
2898   {
2899    "cell_type": "code",
2900    "execution_count": 122,
2901    "metadata": {},
2902    "outputs": [
2903     {
2904      "name": "stdout",
2905      "output_type": "stream",
2906      "text": [
2907       "\n",
2908       "==== Help on x ====\n",
2909       "\n",
2910       "::\n",
2911       "\n",
2912       "        x == dup i\n",
2913       "\n",
2914       "        ... [Q] x = ... [Q] dup i\n",
2915       "        ... [Q] x = ... [Q] [Q] i\n",
2916       "        ... [Q] x = ... [Q]  Q\n",
2917       "\n",
2918       "---- end (x)\n",
2919       "\n",
2920       "\n"
2921      ]
2922     }
2923    ],
2924    "source": [
2925     "J('[x] help')"
2926    ]
2927   },
2928   {
2929    "cell_type": "code",
2930    "execution_count": 123,
2931    "metadata": {},
2932    "outputs": [
2933     {
2934      "name": "stdout",
2935      "output_type": "stream",
2936      "text": [
2937       "            • 1 [2] [i 3] x\n",
2938       "          1 • [2] [i 3] x\n",
2939       "      1 [2] • [i 3] x\n",
2940       "1 [2] [i 3] • x\n",
2941       "1 [2] [i 3] • i 3\n",
2942       "      1 [2] • i 3 3\n",
2943       "          1 • 2 3 3\n",
2944       "        1 2 • 3 3\n",
2945       "      1 2 3 • 3\n",
2946       "    1 2 3 3 • \n"
2947      ]
2948     }
2949    ],
2950    "source": [
2951     "V('1 [2] [i 3] x')  # Kind of a pointless example."
2952    ]
2953   },
2954   {
2955    "cell_type": "markdown",
2956    "metadata": {},
2957    "source": [
2958     "# `void`\n",
2959     "Implements [**Laws of Form** *arithmetic*](https://en.wikipedia.org/wiki/Laws_of_Form#The_primary_arithmetic_.28Chapter_4.29) over quote-only datastructures (that is, datastructures that consist soley of containers, without strings or numbers or anything else.)"
2960    ]
2961   },
2962   {
2963    "cell_type": "code",
2964    "execution_count": 124,
2965    "metadata": {},
2966    "outputs": [
2967     {
2968      "name": "stdout",
2969      "output_type": "stream",
2970      "text": [
2971       "False\n"
2972      ]
2973     }
2974    ],
2975    "source": [
2976     "J('[] void')"
2977    ]
2978   },
2979   {
2980    "cell_type": "code",
2981    "execution_count": 125,
2982    "metadata": {},
2983    "outputs": [
2984     {
2985      "name": "stdout",
2986      "output_type": "stream",
2987      "text": [
2988       "True\n"
2989      ]
2990     }
2991    ],
2992    "source": [
2993     "J('[[]] void')"
2994    ]
2995   },
2996   {
2997    "cell_type": "code",
2998    "execution_count": 126,
2999    "metadata": {},
3000    "outputs": [
3001     {
3002      "name": "stdout",
3003      "output_type": "stream",
3004      "text": [
3005       "True\n"
3006      ]
3007     }
3008    ],
3009    "source": [
3010     "J('[[][[]]] void')"
3011    ]
3012   },
3013   {
3014    "cell_type": "code",
3015    "execution_count": 127,
3016    "metadata": {},
3017    "outputs": [
3018     {
3019      "name": "stdout",
3020      "output_type": "stream",
3021      "text": [
3022       "False\n"
3023      ]
3024     }
3025    ],
3026    "source": [
3027     "J('[[[]][[][]]] void')"
3028    ]
3029   }
3030  ],
3031  "metadata": {
3032   "kernelspec": {
3033    "display_name": "Python 2",
3034    "language": "python",
3035    "name": "python2"
3036   },
3037   "language_info": {
3038    "codemirror_mode": {
3039     "name": "ipython",
3040     "version": 3
3041    },
3042    "file_extension": ".py",
3043    "mimetype": "text/x-python",
3044    "name": "python",
3045    "nbconvert_exporter": "python",
3046    "pygments_lexer": "ipython3",
3047    "version": "3.8.3"
3048   }
3049  },
3050  "nbformat": 4,
3051  "nbformat_minor": 2
3052 }