OSDN Git Service

Cleaning up docs.
[joypy/Thun.git] / docs / html / FuncRef.html
1 <!DOCTYPE html>
2 <html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
3 <head>
4   <meta charset="utf-8" />
5   <meta name="generator" content="pandoc" />
6   <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
7   <title>Function Reference</title>
8   <style>
9     html {
10       line-height: 1.5;
11       font-family: Georgia, serif;
12       font-size: 20px;
13       color: #1a1a1a;
14       background-color: #fdfdfd;
15     }
16     body {
17       margin: 0 auto;
18       max-width: 36em;
19       padding-left: 50px;
20       padding-right: 50px;
21       padding-top: 50px;
22       padding-bottom: 50px;
23       hyphens: auto;
24       overflow-wrap: break-word;
25       text-rendering: optimizeLegibility;
26       font-kerning: normal;
27     }
28     @media (max-width: 600px) {
29       body {
30         font-size: 0.9em;
31         padding: 1em;
32       }
33     }
34     @media print {
35       body {
36         background-color: transparent;
37         color: black;
38         font-size: 12pt;
39       }
40       p, h2, h3 {
41         orphans: 3;
42         widows: 3;
43       }
44       h2, h3, h4 {
45         page-break-after: avoid;
46       }
47     }
48     p {
49       margin: 1em 0;
50     }
51     a {
52       color: #1a1a1a;
53     }
54     a:visited {
55       color: #1a1a1a;
56     }
57     img {
58       max-width: 100%;
59     }
60     h1, h2, h3, h4, h5, h6 {
61       margin-top: 1.4em;
62     }
63     h5, h6 {
64       font-size: 1em;
65       font-style: italic;
66     }
67     h6 {
68       font-weight: normal;
69     }
70     ol, ul {
71       padding-left: 1.7em;
72       margin-top: 1em;
73     }
74     li > ol, li > ul {
75       margin-top: 0;
76     }
77     blockquote {
78       margin: 1em 0 1em 1.7em;
79       padding-left: 1em;
80       border-left: 2px solid #e6e6e6;
81       color: #606060;
82     }
83     code {
84       font-family: Menlo, Monaco, 'Lucida Console', Consolas, monospace;
85       font-size: 85%;
86       margin: 0;
87     }
88     pre {
89       margin: 1em 0;
90       overflow: auto;
91     }
92     pre code {
93       padding: 0;
94       overflow: visible;
95       overflow-wrap: normal;
96     }
97     .sourceCode {
98      background-color: transparent;
99      overflow: visible;
100     }
101     hr {
102       background-color: #1a1a1a;
103       border: none;
104       height: 1px;
105       margin: 1em 0;
106     }
107     table {
108       margin: 1em 0;
109       border-collapse: collapse;
110       width: 100%;
111       overflow-x: auto;
112       display: block;
113       font-variant-numeric: lining-nums tabular-nums;
114     }
115     table caption {
116       margin-bottom: 0.75em;
117     }
118     tbody {
119       margin-top: 0.5em;
120       border-top: 1px solid #1a1a1a;
121       border-bottom: 1px solid #1a1a1a;
122     }
123     th {
124       border-top: 1px solid #1a1a1a;
125       padding: 0.25em 0.5em 0.25em 0.5em;
126     }
127     td {
128       padding: 0.125em 0.5em 0.25em 0.5em;
129     }
130     header {
131       margin-bottom: 4em;
132       text-align: center;
133     }
134     #TOC li {
135       list-style: none;
136     }
137     #TOC a:not(:hover) {
138       text-decoration: none;
139     }
140     code{white-space: pre-wrap;}
141     span.smallcaps{font-variant: small-caps;}
142     span.underline{text-decoration: underline;}
143     div.column{display: inline-block; vertical-align: top; width: 50%;}
144     div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
145     ul.task-list{list-style: none;}
146     .display.math{display: block; text-align: center; margin: 0.5rem auto;}
147   </style>
148   <!--[if lt IE 9]>
149     <script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
150   <![endif]-->
151 </head>
152 <body>
153 <header id="title-block-header">
154 <h1 class="title">Function Reference</h1>
155 </header>
156 <nav id="TOC" role="doc-toc">
157 <ul>
158 <li><a href="#section">&amp;</a></li>
159 <li><a href="#section-1">&amp;&amp;</a></li>
160 <li><a href="#section-2">*</a></li>
161 <li><a href="#section-3">&#x2022;</a></li>
162 <li><a href="#section-4">^</a></li>
163 <li><a href="#section-5">=</a></li>
164 <li><a href="#section-6">!=</a></li>
165 <li><a href="#section-7">!-</a></li>
166 <li><a href="#section-8">&gt;</a></li>
167 <li><a href="#section-9">&gt;=</a></li>
168 <li><a href="#section-10">&gt;&gt;</a></li>
169 <li><a href="#section-11">-</a></li>
170 <li><a href="#section-12">&#x2013;</a></li>
171 <li><a href="#section-13">&lt;</a></li>
172 <li><a href="#section-14">&lt;=</a></li>
173 <li><a href="#section-15">&lt;&gt;</a></li>
174 <li><a href="#section-16">&lt;{}</a></li>
175 <li><a href="#section-17">&lt;&lt;</a></li>
176 <li><a href="#section-18">&lt;&lt;{}</a></li>
177 <li><a href="#section-19">%</a></li>
178 <li><a href="#section-20">+</a></li>
179 <li><a href="#section-21">++</a></li>
180 <li><a href="#section-22">?</a></li>
181 <li><a href="#section-23">/</a></li>
182 <li><a href="#section-24">//</a></li>
183 <li><a href="#floor">/floor</a></li>
184 <li><a href="#section-25">||</a></li>
185 <li><a href="#abs">abs</a></li>
186 <li><a href="#add">add</a></li>
187 <li><a href="#anamorphism">anamorphism</a></li>
188 <li><a href="#and">and</a></li>
189 <li><a href="#app1">app1</a></li>
190 <li><a href="#app2">app2</a></li>
191 <li><a href="#app3">app3</a></li>
192 <li><a href="#appn">appN</a></li>
193 <li><a href="#at">at</a></li>
194 <li><a href="#average">average</a></li>
195 <li><a href="#b">b</a></li>
196 <li><a href="#binary">binary</a></li>
197 <li><a href="#bool">bool</a></li>
198 <li><a href="#branch">branch</a></li>
199 <li><a href="#ccccons">ccccons</a></li>
200 <li><a href="#ccons">ccons</a></li>
201 <li><a href="#choice">choice</a></li>
202 <li><a href="#clear">clear</a></li>
203 <li><a href="#cleave">cleave</a></li>
204 <li><a href="#clop">clop</a></li>
205 <li><a href="#cmp">cmp</a></li>
206 <li><a href="#codi">codi</a></li>
207 <li><a href="#codireco">codireco</a></li>
208 <li><a href="#concat">concat</a></li>
209 <li><a href="#cond">cond</a></li>
210 <li><a href="#cons">cons</a></li>
211 <li><a href="#dinfrirst">dinfrirst</a></li>
212 <li><a href="#dip">dip</a></li>
213 <li><a href="#dipd">dipd</a></li>
214 <li><a href="#dipdd">dipdd</a></li>
215 <li><a href="#disenstacken">disenstacken</a></li>
216 <li><a href="#div">div</a></li>
217 <li><a href="#divmod">divmod</a></li>
218 <li><a href="#down_to_zero">down_to_zero</a></li>
219 <li><a href="#drop">drop</a></li>
220 <li><a href="#dup">dup</a></li>
221 <li><a href="#dupd">dupd</a></li>
222 <li><a href="#dupdd">dupdd</a></li>
223 <li><a href="#dupdip">dupdip</a></li>
224 <li><a href="#dupdipd">dupdipd</a></li>
225 <li><a href="#enstacken">enstacken</a></li>
226 <li><a href="#eq">eq</a></li>
227 <li><a href="#first">first</a></li>
228 <li><a href="#first_two">first_two</a></li>
229 <li><a href="#flatten">flatten</a></li>
230 <li><a href="#floor-1">floor</a></li>
231 <li><a href="#floordiv">floordiv</a></li>
232 <li><a href="#fork">fork</a></li>
233 <li><a href="#fourth">fourth</a></li>
234 <li><a href="#gcd">gcd</a></li>
235 <li><a href="#gcd2">gcd2</a></li>
236 <li><a href="#ge">ge</a></li>
237 <li><a href="#genrec">genrec</a></li>
238 <li><a href="#getitem">getitem</a></li>
239 <li><a href="#grabn">grabN</a></li>
240 <li><a href="#grba">grba</a></li>
241 <li><a href="#gt">gt</a></li>
242 <li><a href="#help">help</a></li>
243 <li><a href="#hypot">hypot</a></li>
244 <li><a href="#i">i</a></li>
245 <li><a href="#id">id</a></li>
246 <li><a href="#ifte">ifte</a></li>
247 <li><a href="#ii">ii</a></li>
248 <li><a href="#infra">infra</a></li>
249 <li><a href="#infrst">infrst</a></li>
250 <li><a href="#inscribe">inscribe</a></li>
251 <li><a href="#le">le</a></li>
252 <li><a href="#loop">loop</a></li>
253 <li><a href="#lshift">lshift</a></li>
254 <li><a href="#lt">lt</a></li>
255 <li><a href="#make_generator">make_generator</a></li>
256 <li><a href="#map">map</a></li>
257 <li><a href="#max">max</a></li>
258 <li><a href="#min">min</a></li>
259 <li><a href="#mod">mod</a></li>
260 <li><a href="#modulus">modulus</a></li>
261 <li><a href="#mul">mul</a></li>
262 <li><a href="#ne">ne</a></li>
263 <li><a href="#neg">neg</a></li>
264 <li><a href="#not">not</a></li>
265 <li><a href="#nulco">nulco</a></li>
266 <li><a href="#nullary">nullary</a></li>
267 <li><a href="#of">of</a></li>
268 <li><a href="#or">or</a></li>
269 <li><a href="#over">over</a></li>
270 <li><a href="#pam">pam</a></li>
271 <li><a href="#pick">pick</a></li>
272 <li><a href="#pm">pm</a></li>
273 <li><a href="#pop">pop</a></li>
274 <li><a href="#popd">popd</a></li>
275 <li><a href="#popdd">popdd</a></li>
276 <li><a href="#popop">popop</a></li>
277 <li><a href="#popopd">popopd</a></li>
278 <li><a href="#popopdd">popopdd</a></li>
279 <li><a href="#popopop">popopop</a></li>
280 <li><a href="#pow">pow</a></li>
281 <li><a href="#pred">pred</a></li>
282 <li><a href="#primrec">primrec</a></li>
283 <li><a href="#product">product</a></li>
284 <li><a href="#quoted">quoted</a></li>
285 <li><a href="#range">range</a></li>
286 <li><a href="#range_to_zero">range_to_zero</a></li>
287 <li><a href="#reco">reco</a></li>
288 <li><a href="#rem">rem</a></li>
289 <li><a href="#remainder">remainder</a></li>
290 <li><a href="#remove">remove</a></li>
291 <li><a href="#rest">rest</a></li>
292 <li><a href="#reverse">reverse</a></li>
293 <li><a href="#rolldown">rolldown</a></li>
294 <li><a href="#rollup">rollup</a></li>
295 <li><a href="#roll">roll&gt;</a></li>
296 <li><a href="#roll-1">roll&lt;</a></li>
297 <li><a href="#round">round</a></li>
298 <li><a href="#rrest">rrest</a></li>
299 <li><a href="#rshift">rshift</a></li>
300 <li><a href="#run">run</a></li>
301 <li><a href="#second">second</a></li>
302 <li><a href="#select">select</a></li>
303 <li><a href="#sharing">sharing</a></li>
304 <li><a href="#shift">shift</a></li>
305 <li><a href="#shunt">shunt</a></li>
306 <li><a href="#size">size</a></li>
307 <li><a href="#sort">sort</a></li>
308 <li><a href="#spiral_next">spiral_next</a></li>
309 <li><a href="#split_at">split_at</a></li>
310 <li><a href="#split_list">split_list</a></li>
311 <li><a href="#sqr">sqr</a></li>
312 <li><a href="#sqrt">sqrt</a></li>
313 <li><a href="#stack">stack</a></li>
314 <li><a href="#stackd">stackd</a></li>
315 <li><a href="#step">step</a></li>
316 <li><a href="#step_zero">step_zero</a></li>
317 <li><a href="#stuncons">stuncons</a></li>
318 <li><a href="#stununcons">stununcons</a></li>
319 <li><a href="#sub">sub</a></li>
320 <li><a href="#succ">succ</a></li>
321 <li><a href="#sum">sum</a></li>
322 <li><a href="#swaack">swaack</a></li>
323 <li><a href="#swap">swap</a></li>
324 <li><a href="#swapd">swapd</a></li>
325 <li><a href="#swoncat">swoncat</a></li>
326 <li><a href="#swons">swons</a></li>
327 <li><a href="#tailrec">tailrec</a></li>
328 <li><a href="#take">take</a></li>
329 <li><a href="#ternary">ternary</a></li>
330 <li><a href="#third">third</a></li>
331 <li><a href="#times">times</a></li>
332 <li><a href="#truthy">truthy</a></li>
333 <li><a href="#tuck">tuck</a></li>
334 <li><a href="#unary">unary</a></li>
335 <li><a href="#uncons">uncons</a></li>
336 <li><a href="#unique">unique</a></li>
337 <li><a href="#unit">unit</a></li>
338 <li><a href="#unquoted">unquoted</a></li>
339 <li><a href="#unswons">unswons</a></li>
340 <li><a href="#void">void</a></li>
341 <li><a href="#warranty">warranty</a></li>
342 <li><a href="#while">while</a></li>
343 <li><a href="#words">words</a></li>
344 <li><a href="#x">x</a></li>
345 <li><a href="#xor">xor</a></li>
346 <li><a href="#zip">zip</a></li>
347 </ul>
348 </nav>
349 <p>Version -10.0.0</p>
350 <p>Each function, combinator, or definition should be documented here.</p>
351 <hr />
352 <h2 id="section">&amp;</h2>
353 <p>See <a href="#and">and</a>.</p>
354 <hr />
355 <h2 id="section-1">&amp;&amp;</h2>
356 <p>Combinator</p>
357 <p>Short-circuiting Boolean AND</p>
358 <p>Accept two quoted programs, run the first and expect a Boolean value, if it&#x2019;s <code>true</code> pop it and run the second program (which should also return a Boolean value) otherwise pop the second program (leaving <code>false</code> on the stack.)</p>
359 <pre><code>   [A] [B] &amp;&amp;
360 ---------------- true
361         B
362
363
364    [A] [B] &amp;&amp;
365 ---------------- false
366      false</code></pre>
367 <h3 id="definition">Definition</h3>
368 <pre><code>nulco [nullary [false]] dip branch</code></pre>
369 <h3 id="derivation">Derivation</h3>
370 <p>TODO: this is derived in one of the notebooks I think, look it up and link to it, or copy the content here.</p>
371 <h3 id="discussion">Discussion</h3>
372 <p>This is seldom useful, I suspect, but this way you have it.</p>
373 <h3 id="crosslinks">Crosslinks</h3>
374 <p><a href="#section-25">||</a></p>
375 <hr />
376 <h2 id="section-2">*</h2>
377 <p>See <a href="#mul">mul</a>.</p>
378 <hr />
379 <h2 id="section-3">&#x2022;</h2>
380 <p>See <a href="#id">id</a>.</p>
381 <hr />
382 <h2 id="section-4">^</h2>
383 <p>See <a href="#xor">xor</a>.</p>
384 <hr />
385 <h2 id="section-5">=</h2>
386 <p>See <a href="#eq">eq</a>.</p>
387 <hr />
388 <h2 id="section-6">!=</h2>
389 <p>See <a href="#ne">ne</a>.</p>
390 <hr />
391 <h2 id="section-7">!-</h2>
392 <p>Function</p>
393 <p>Not negative.</p>
394 <pre><code>    n !-
395 ----------- n &lt; 0
396    false
397
398
399    n !-
400 ---------- n &gt;= 0
401    true</code></pre>
402 <h3 id="definition-1">Definition</h3>
403 <pre><code>0 \&gt;=</code></pre>
404 <h3 id="discussion-1">Discussion</h3>
405 <p>Return a Boolean value indicating if a number is greater than or equal to zero.</p>
406 <hr />
407 <h2 id="section-8">&gt;</h2>
408 <p>See <a href="#gt">gt</a>.</p>
409 <hr />
410 <h2 id="section-9">&gt;=</h2>
411 <p>See <a href="#ge">ge</a>.</p>
412 <hr />
413 <h2 id="section-10">&gt;&gt;</h2>
414 <p>See <a href="#rshift">rshift</a>.</p>
415 <hr />
416 <h2 id="section-11">-</h2>
417 <p>See <a href="#sub">sub</a>.</p>
418 <hr />
419 <h2 id="section-12">&#x2013;</h2>
420 <p>See <a href="#pred">pred</a>.</p>
421 <hr />
422 <h2 id="section-13">&lt;</h2>
423 <p>See <a href="#lt">lt</a>.</p>
424 <hr />
425 <h2 id="section-14">&lt;=</h2>
426 <p>See <a href="#le">le</a>.</p>
427 <hr />
428 <h2 id="section-15">&lt;&gt;</h2>
429 <p>See <a href="#ne">ne</a>.</p>
430 <hr />
431 <h2 id="section-16">&lt;{}</h2>
432 <p>Function</p>
433 <pre><code>   ... a &lt;{}
434 ----------------
435    ... [] a</code></pre>
436 <h3 id="definition-2">Definition</h3>
437 <pre><code>[] swap</code></pre>
438 <h3 id="discussion-2">Discussion</h3>
439 <p>Tuck an empty list just under the first item on the stack.</p>
440 <h3 id="crosslinks-1">Crosslinks</h3>
441 <p><a href="#section-18">&lt;&lt;{}</a></p>
442 <hr />
443 <h2 id="section-17">&lt;&lt;</h2>
444 <p>See <a href="#lshift">lshift</a>.</p>
445 <hr />
446 <h2 id="section-18">&lt;&lt;{}</h2>
447 <p>Function</p>
448 <pre><code>   ... b a &lt;{}
449 -----------------
450    ... [] b a</code></pre>
451 <h3 id="definition-3">Definition</h3>
452 <pre><code>[] rollup</code></pre>
453 <h3 id="discussion-3">Discussion</h3>
454 <p>Tuck an empty list just under the first two items on the stack.</p>
455 <h3 id="crosslinks-2">Crosslinks</h3>
456 <p><a href="#section-16">&lt;{}</a></p>
457 <hr />
458 <h2 id="section-19">%</h2>
459 <p>See <a href="#mod">mod</a>.</p>
460 <hr />
461 <h2 id="section-20">+</h2>
462 <p>See <a href="#add">add</a>.</p>
463 <hr />
464 <h2 id="section-21">++</h2>
465 <p>See <a href="#succ">succ</a>.</p>
466 <hr />
467 <h2 id="section-22">?</h2>
468 <p>Function</p>
469 <p>Is the item on the top of the stack &#x201C;truthy&#x201D;?</p>
470 <h3 id="definition-4">Definition</h3>
471 <blockquote>
472 <p><a href="#dup">dup</a> <a href="#bool">bool</a></p>
473 </blockquote>
474 <h3 id="discussion-4">Discussion</h3>
475 <p>You often want to test the truth value of an item on the stack without consuming the item.</p>
476 <h3 id="crosslinks-3">Crosslinks</h3>
477 <p><a href="#bool">bool</a></p>
478 <hr />
479 <h2 id="section-23">/</h2>
480 <p>See <a href="#floordiv">floordiv</a>.</p>
481 <hr />
482 <h2 id="section-24">//</h2>
483 <p>See <a href="#floordiv">floordiv</a>.</p>
484 <hr />
485 <h2 id="floor">/floor</h2>
486 <p>See <a href="#floordiv">floordiv</a>.</p>
487 <hr />
488 <h2 id="section-25">||</h2>
489 <p>Combinator</p>
490 <p>Short-circuiting Boolean OR</p>
491 <h3 id="definition-5">Definition</h3>
492 <blockquote>
493 <p><a href="#nulco">nulco</a> [<a href="#nullary">nullary</a>] <a href="#dip">dip</a> [true] <a href="#branch">branch</a></p>
494 </blockquote>
495 <h3 id="discussion-5">Discussion</h3>
496 <p>Accept two quoted programs, run the first and expect a Boolean value, if it&#x2019;s <code>false</code> pop it and run the second program (which should also return a Boolean value) otherwise pop the second program (leaving <code>true</code> on the stack.)</p>
497 <pre><code>   [A] [B] ||
498 ---------------- A -&gt; false
499         B
500
501
502    [A] [B] ||
503 ---------------- A -&gt; true
504       true</code></pre>
505 <h3 id="crosslinks-4">Crosslinks</h3>
506 <p><a href="#section-1">&amp;&amp;</a></p>
507 <hr />
508 <h2 id="abs">abs</h2>
509 <p>Function</p>
510 <p>Return the absolute value of the argument.</p>
511 <h3 id="definition-6">Definition</h3>
512 <blockquote>
513 <p><a href="#dup">dup</a> 0 &lt; [] [<a href="#neg">neg</a>] <a href="#branch">branch</a></p>
514 </blockquote>
515 <hr />
516 <h2 id="add">add</h2>
517 <p>Basis Function</p>
518 <p>Add two numbers together: a + b.</p>
519 <hr />
520 <h2 id="anamorphism">anamorphism</h2>
521 <p>Combinator</p>
522 <p>Build a list of values from a generator program <code>G</code> and a stopping predicate <code>P</code>.</p>
523 <pre><code>           [P] [G] anamorphism
524 -----------------------------------------
525    [P] [pop []] [G] [dip swons] genrec</code></pre>
526 <h3 id="definition-7">Definition</h3>
527 <blockquote>
528 <p>[<a href="#pop">pop</a> []] <a href="#swap">swap</a> [<a href="#dip">dip</a> <a href="#swons">swons</a>] <a href="#genrec">genrec</a></p>
529 </blockquote>
530 <h3 id="example">Example</h3>
531 <p>The <code>range</code> function generates a list of the integers from 0 to n - 1:</p>
532 <blockquote>
533 <p>[0 &lt;=] [-- dup] anamorphism</p>
534 </blockquote>
535 <h3 id="discussion-6">Discussion</h3>
536 <p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
537 <hr />
538 <h2 id="and">and</h2>
539 <p>Basis Function</p>
540 <p>Logical bit-wise AND.</p>
541 <h3 id="crosslinks-5">Crosslinks</h3>
542 <p><a href="#or">or</a> <a href="#xor">xor</a></p>
543 <hr />
544 <h2 id="app1">app1</h2>
545 <p>&#x201C;apply one&#x201D;</p>
546 <p>Combinator</p>
547 <p>Given a quoted program on TOS and anything as the second stack item run the program without disturbing the stack and replace the two args with the first result of the program.</p>
548 <pre><code>         ... x [Q] app1
549 ---------------------------------
550    ... [x ...] [Q] infra first</code></pre>
551 <p>This is the same effect as the <a href="#unary">unary</a> combinator.</p>
552 <h3 id="definition-8">Definition</h3>
553 <blockquote>
554 <p><a href="#nullary">nullary</a> <a href="#popd">popd</a></p>
555 </blockquote>
556 <h3 id="discussion-7">Discussion</h3>
557 <p>Just a specialization of <code>nullary</code> really. Its parallelizable cousins are more useful.</p>
558 <h3 id="crosslinks-6">Crosslinks</h3>
559 <p><a href="#app2">app2</a> <a href="#app3">app3</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
560 <hr />
561 <h2 id="app2">app2</h2>
562 <p>Combinator</p>
563 <p>Like <a href="#app1">app1</a> with two items.</p>
564 <pre><code>   ... y x [Q] . app2
565 -----------------------------------
566    ... [y ...] [Q] . infra first
567        [x ...] [Q]   infra first</code></pre>
568 <h3 id="definition-9">Definition</h3>
569 <blockquote>
570 <p>[<a href="#grba">grba</a> <a href="#swap">swap</a> <a href="#grba">grba</a> <a href="#swap">swap</a>] <a href="#dip">dip</a> [<a href="#infrst">infrst</a>] <a href="#cons">cons</a> <a href="#ii">ii</a></p>
571 </blockquote>
572 <h3 id="discussion-8">Discussion</h3>
573 <p>Unlike <a href="#app1">app1</a>, which is essentially an alias for <a href="#unary">unary</a>, this function is not the same as <a href="#binary">binary</a>. Instead of running one program using exactly two items from the stack and pushing one result (as <a href="#binary">binary</a> does) this function takes two items from the stack and runs the program twice, separately for each of the items, then puts both results onto the stack.</p>
574 <p>This is not currently implemented as parallel processes but it can (and should) be done.</p>
575 <h3 id="crosslinks-7">Crosslinks</h3>
576 <p><a href="#app1">app1</a> <a href="#app3">app3</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
577 <hr />
578 <h2 id="app3">app3</h2>
579 <p>Combinator</p>
580 <p>Like <a href="#app1">app1</a> with three items.</p>
581 <pre><code>     ... z y x [Q] . app3
582 -----------------------------------
583    ... [z ...] [Q] . infra first
584        [y ...] [Q]   infra first
585        [x ...] [Q]   infra first</code></pre>
586 <h3 id="definition-10">Definition</h3>
587 <blockquote>
588 <p>3 <a href="#appn">appN</a></p>
589 </blockquote>
590 <h3 id="discussion-9">Discussion</h3>
591 <p>See <a href="#app2">app2</a>.</p>
592 <h3 id="crosslinks-8">Crosslinks</h3>
593 <p><a href="#app1">app1</a> <a href="#app2">app2</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
594 <hr />
595 <h2 id="appn">appN</h2>
596 <p>Combinator</p>
597 <p>Like <a href="#app1">app1</a> with any number of items.</p>
598 <pre><code>   ... xN ... x2 x1 x0 [Q] n . appN
599 --------------------------------------
600    ... [xN ...] [Q] . infra first
601                    ...
602        [x2 ...] [Q]   infra first
603        [x1 ...] [Q]   infra first
604        [x0 ...] [Q]   infra first</code></pre>
605 <h3 id="definition-11">Definition</h3>
606 <blockquote>
607 <p>[<a href="#grabn">grabN</a>] <a href="#codi">codi</a> <a href="#map">map</a> <a href="#disenstacken">disenstacken</a></p>
608 </blockquote>
609 <h3 id="discussion-10">Discussion</h3>
610 <p>This function takes a quoted function <code>Q</code> and an integer and runs the function that many times on that many stack items. See also <a href="#app2">app2</a>.</p>
611 <h3 id="crosslinks-9">Crosslinks</h3>
612 <p><a href="#app1">app1</a> <a href="#app2">app2</a> <a href="#app3">app3</a> <a href="#unary">unary</a></p>
613 <hr />
614 <h2 id="at">at</h2>
615 <p>See <a href="#getitem">getitem</a>.</p>
616 <hr />
617 <h2 id="average">average</h2>
618 <p>Function</p>
619 <p>Compute the average of a list of numbers. (Currently broken until I can figure out what to do about &#x201C;numeric tower&#x201D; in Thun.)</p>
620 <h3 id="definition-12">Definition</h3>
621 <blockquote>
622 <p>[<a href="#sum">sum</a>] [<a href="#size">size</a>] <a href="#cleave">cleave</a> <a href="#section-23">/</a></p>
623 </blockquote>
624 <h3 id="discussion-11">Discussion</h3>
625 <p>Theoretically this function would compute the sum and the size in two separate threads, then divide. This works but a compiled version would probably do better to sum and count the list once, in one thread, eh?</p>
626 <p>As an exercise in Functional Programming in Joy it would be fun to convert this into a catamorphism. See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
627 <hr />
628 <h2 id="b">b</h2>
629 <p>Combinator</p>
630 <p>Run two quoted programs</p>
631 <pre><code>   [P] [Q] b
632 ---------------
633       P Q</code></pre>
634 <h3 id="definition-13">Definition</h3>
635 <blockquote>
636 <p>[<a href="#i">i</a>] <a href="#dip">dip</a> <a href="#i">i</a></p>
637 </blockquote>
638 <h3 id="discussion-12">Discussion</h3>
639 <p>This combinator may seem trivial but it comes in handy.</p>
640 <h3 id="crosslinks-10">Crosslinks</h3>
641 <p><a href="#dupdip">dupdip</a> <a href="#ii">ii</a></p>
642 <hr />
643 <h2 id="binary">binary</h2>
644 <p>Combinator</p>
645 <p>Run a quoted program using exactly two stack values and leave the first item of the result on the stack.</p>
646 <pre><code>   ... y x [P] binary
647 -----------------------
648         ... a</code></pre>
649 <h3 id="definition-14">Definition</h3>
650 <blockquote>
651 <p><a href="#unary">unary</a> <a href="#popd">popd</a></p>
652 </blockquote>
653 <h3 id="discussion-13">Discussion</h3>
654 <p>Runs any other quoted function and returns its first result while consuming exactly two items from the stack.</p>
655 <h3 id="crosslinks-11">Crosslinks</h3>
656 <p><a href="#nullary">nullary</a> <a href="#ternary">ternary</a> <a href="#unary">unary</a></p>
657 <hr />
658 <h2 id="bool">bool</h2>
659 <p>Basis Function</p>
660 <p>Convert the item on the top of the stack to a Boolean value.</p>
661 <h3 id="discussion-14">Discussion</h3>
662 <p>For integers 0 is <code>false</code> and any other number is <code>true</code>; for lists the empty list is <code>false</code> and all other lists are <code>true</code>.</p>
663 <h3 id="crosslinks-12">Crosslinks</h3>
664 <p><a href="#not">not</a></p>
665 <hr />
666 <h2 id="branch">branch</h2>
667 <p>Basis Combinator</p>
668 <p>Use a Boolean value to select and run one of two quoted programs.</p>
669 <pre><code>   false [F] [T] branch
670 --------------------------
671           F
672
673    true [F] [T] branch
674 -------------------------
675              T</code></pre>
676 <h3 id="definition-15">Definition</h3>
677 <blockquote>
678 <p><a href="#rolldown">rolldown</a> <a href="#choice">choice</a> <a href="#i">i</a></p>
679 </blockquote>
680 <h3 id="discussion-15">Discussion</h3>
681 <p>This is one of the fundamental operations (although it can be defined in terms of <a href="#choice">choice</a> as above). The more common &#x201C;if..then..else&#x201D; construct <a href="#ifte">ifte</a> adds a predicate function that is evaluated <a href="#nullary">nullary</a>.</p>
682 <h3 id="crosslinks-13">Crosslinks</h3>
683 <p><a href="#choice">choice</a> <a href="#ifte">ifte</a> <a href="#select">select</a></p>
684 <hr />
685 <h2 id="ccccons">ccccons</h2>
686 <p>Function</p>
687 <pre><code>   a b c d [...] ccccons
688 ---------------------------
689        [a b c d ...]</code></pre>
690 <p>Do <a href="#cons">cons</a> four times.</p>
691 <h3 id="definition-16">Definition</h3>
692 <blockquote>
693 <p><a href="#ccons">ccons</a> <a href="#ccons">ccons</a></p>
694 </blockquote>
695 <h3 id="crosslinks-14">Crosslinks</h3>
696 <p><a href="#ccons">ccons</a> <a href="#cons">cons</a> <a href="#times">times</a></p>
697 <hr />
698 <h2 id="ccons">ccons</h2>
699 <p>Function</p>
700 <pre><code>   a b [...] ccons
701 ---------------------
702       [a b ...]</code></pre>
703 <p>Do <a href="#cons">cons</a> two times.</p>
704 <h3 id="definition-17">Definition</h3>
705 <blockquote>
706 <p><a href="#cons">cons</a> <a href="#cons">cons</a></p>
707 </blockquote>
708 <h3 id="crosslinks-15">Crosslinks</h3>
709 <p><a href="#cons">cons</a> <a href="#ccons">ccons</a></p>
710 <hr />
711 <h2 id="choice">choice</h2>
712 <p>Basis Function</p>
713 <p>Use a Boolean value to select one of two items.</p>
714 <pre><code>   a b false choice
715 ----------------------
716           a
717
718    a b true choice
719 ---------------------
720           b</code></pre>
721 <h3 id="definition-18">Definition</h3>
722 <blockquote>
723 <p>[<a href="#pop">pop</a>] [<a href="#popd">popd</a>] <a href="#branch">branch</a></p>
724 </blockquote>
725 <h3 id="discussion-16">Discussion</h3>
726 <p>It&#x2019;s a matter of taste whether you implement this in terms of <a href="#branch">branch</a> or the other way around.</p>
727 <h3 id="crosslinks-16">Crosslinks</h3>
728 <p><a href="#branch">branch</a> <a href="#select">select</a></p>
729 <hr />
730 <h2 id="clear">clear</h2>
731 <p>Basis Function</p>
732 <p>Clear everything from the stack.</p>
733 <h3 id="definition-19">Definition</h3>
734 <blockquote>
735 <p><a href="#stack">stack</a> <a href="#bool">bool</a> [<a href="#pop">pop</a> <a href="#stack">stack</a> <a href="#bool">bool</a>] <a href="#loop">loop</a></p>
736 </blockquote>
737 <h3 id="crosslinks-17">Crosslinks</h3>
738 <p><a href="#stack">stack</a> <a href="#swaack">swaack</a></p>
739 <hr />
740 <h2 id="cleave">cleave</h2>
741 <p>Combinator</p>
742 <p>Run two programs in parallel, consuming one additional item, and put their results on the stack.</p>
743 <pre><code>   ... x [A] [B] cleave
744 ------------------------
745         ... a b</code></pre>
746 <h3 id="derivation-1">Derivation</h3>
747 <blockquote>
748 <p><a href="#fork">fork</a> <a href="#popdd">popdd</a></p>
749 </blockquote>
750 <h3 id="example-1">Example</h3>
751 <pre><code>   1 2 3 [+] [-] cleave
752 --------------------------
753          1 2 5 -1</code></pre>
754 <h3 id="discussion-17">Discussion</h3>
755 <p>One of a handful of useful parallel combinators.</p>
756 <h3 id="crosslinks-18">Crosslinks</h3>
757 <p><a href="#clop">clop</a> <a href="#fork">fork</a> <a href="#map">map</a></p>
758 <hr />
759 <h2 id="clop">clop</h2>
760 <p>Combinator</p>
761 <p>Run two programs in parallel, consuming two additional items, and put their results on the stack.</p>
762 <pre><code>   ... x y [A] [B] clop
763 --------------------------
764         ... a b</code></pre>
765 <h3 id="definition-20">Definition</h3>
766 <blockquote>
767 <p><a href="#cleave">cleave</a> <a href="#popdd">popdd</a></p>
768 </blockquote>
769 <h3 id="discussion-18">Discussion</h3>
770 <p>Like <a href="#cleave">cleave</a> but consumes an additional item from the stack.</p>
771 <pre><code>   1 2 3 4 [+] [-] clop
772 --------------------------
773          1 2 7 -1</code></pre>
774 <h3 id="crosslinks-19">Crosslinks</h3>
775 <p><a href="#cleave">cleave</a> <a href="#fork">fork</a> <a href="#map">map</a></p>
776 <hr />
777 <h2 id="cmp">cmp</h2>
778 <p>Combinator</p>
779 <p>Take two values and three quoted programs on the stack and run one of the three depending on the results of comparing the two values.</p>
780 <pre><code>   a b [G] [E] [L] cmp
781 ------------------------- a &gt; b
782         G
783
784    a b [G] [E] [L] cmp
785 ------------------------- a = b
786             E
787
788    a b [G] [E] [L] cmp
789 ------------------------- a &lt; b
790                 L</code></pre>
791 <h3 id="discussion-19">Discussion</h3>
792 <p>This is useful sometimes, and you can <a href="#dup">dup</a> or <a href="#dupd">dupd</a> with two quoted programs to handle the cases when you just want to deal with <a href="#section-14">&lt;=</a> or <a href="#section-9">&gt;=</a> and not all three possibilities, e.g.:</p>
793 <pre><code>[G] [EL] dup cmp
794
795 [GE] [L] dupd cmp</code></pre>
796 <p>Or even:</p>
797 <pre><code>[GL] [E] over cmp</code></pre>
798 <h3 id="crosslinks-20">Crosslinks</h3>
799 <p>TODO: link to tree notebooks where this was used.</p>
800 <hr />
801 <h2 id="codi">codi</h2>
802 <p>Combinator</p>
803 <p>Take a quoted program from the stack, <a href="#cons">cons</a> the next item onto it, then <a href="#dip">dip</a> the whole thing under what was the third item on the stack.</p>
804 <pre><code>   a b [F] . codi
805 --------------------
806          b . F a</code></pre>
807 <h3 id="definition-21">Definition</h3>
808 <blockquote>
809 <p><a href="#cons">cons</a> <a href="#dip">dip</a></p>
810 </blockquote>
811 <h3 id="discussion-20">Discussion</h3>
812 <p>This is one of those weirdly specific functions that turns out to be useful in a few places.</p>
813 <h3 id="crosslinks-21">Crosslinks</h3>
814 <p><a href="#appn">appN</a> <a href="#codireco">codireco</a></p>
815 <hr />
816 <h2 id="codireco">codireco</h2>
817 <p>Combinator</p>
818 <p>This is part of the <a href="#make_generator">make_generator</a> function. You would not use this combinator directly.</p>
819 <h3 id="definition-22">Definition</h3>
820 <blockquote>
821 <p><a href="#codi">codi</a> <a href="#reco">reco</a></p>
822 </blockquote>
823 <h3 id="discussion-21">Discussion</h3>
824 <p>See <a href="#make_generator">make_generator</a> and the <a href="https://joypy.osdn.io/notebooks/Generator_Programs.html#an-interesting-variation">&#x201C;Using <code>x</code> to Generate Values&#x201D; notebook</a> as well as <a href="https://www.kevinalbrecht.com/code/joy-mirror/j05cmp.html">Recursion Theory and Joy</a> by Manfred von Thun.</p>
825 <h3 id="crosslinks-22">Crosslinks</h3>
826 <p><a href="#make_generator">make_generator</a></p>
827 <hr />
828 <h2 id="concat">concat</h2>
829 <p>Function</p>
830 <p>Concatinate two lists.</p>
831 <pre><code>   [a b c] [d e f] concat
832 ----------------------------
833        [a b c d e f]</code></pre>
834 <h3 id="crosslinks-23">Crosslinks</h3>
835 <p><a href="#first">first</a> <a href="#first_two">first_two</a> <a href="#flatten">flatten</a> <a href="#fourth">fourth</a> <a href="#getitem">getitem</a> <a href="#remove">remove</a> <a href="#rest">rest</a> <a href="#reverse">reverse</a> <a href="#rrest">rrest</a> <a href="#second">second</a> <a href="#shift">shift</a> <a href="#shunt">shunt</a> <a href="#size">size</a> <a href="#sort">sort</a> <a href="#split_at">split_at</a> <a href="#split_list">split_list</a> <a href="#swaack">swaack</a> <a href="#third">third</a> <a href="#zip">zip</a></p>
836 <hr />
837 <h2 id="cond">cond</h2>
838 <p>Combinator</p>
839 <p>This combinator works like a case statement. It expects a single quote on the stack that must contain zero or more condition quotes and a default quote. Each condition quote should contain a quoted predicate followed by the function expression to run if that predicate returns <code>true</code>. If no predicates return <code>true</code> the default function runs.</p>
840 <pre><code>[
841     [ [Predicate0] Function0 ]
842     [ [Predicate1] Function1 ]
843     ...
844     [ [PredicateN] FunctionN ]
845     [Default]
846 ]
847 cond</code></pre>
848 <h3 id="discussion-22">Discussion</h3>
849 <p>It works by rewriting into a chain of nested <span class="title-ref">ifte</span> expressions, e.g.:</p>
850 <pre><code>      [[[B0] T0] [[B1] T1] [D]] cond
851 -----------------------------------------
852    [B0] [T0] [[B1] [T1] [D] ifte] ifte</code></pre>
853 <h3 id="crosslinks-24">Crosslinks</h3>
854 <p><a href="#ifte">ifte</a></p>
855 <hr />
856 <h2 id="cons">cons</h2>
857 <p>Basis Function</p>
858 <p>Given an item and a list, append the item to the list to make a new list.</p>
859 <pre><code>   a [...] cons
860 ------------------
861      [a ...]</code></pre>
862 <h3 id="discussion-23">Discussion</h3>
863 <p>Cons is a <a href="https://en.wikipedia.org/wiki/Cons#Lists">venerable old function from Lisp</a>. Its inverse operation is <a href="#uncons">uncons</a>.</p>
864 <h3 id="crosslinks-25">Crosslinks</h3>
865 <p><a href="#uncons">uncons</a></p>
866 <hr />
867 <h2 id="dinfrirst">dinfrirst</h2>
868 <p>Combinator</p>
869 <p>Specialist function (that means I forgot what it does and why.)</p>
870 <h3 id="definition-23">Definition</h3>
871 <blockquote>
872 <p><a href="#dip">dip</a> <a href="#infrst">infrst</a></p>
873 </blockquote>
874 <hr />
875 <h2 id="dip">dip</h2>
876 <p>Basis Combinator</p>
877 <p>The <code>dip</code> combinator expects a quoted program on the stack and below it some item, it hoists the item into the expression and runs the program on the rest of the stack.</p>
878 <pre><code>   ... x [Q] . dip
879 ---------------------
880          ... . Q x</code></pre>
881 <h3 id="discussion-24">Discussion</h3>
882 <p>This along with <a href="#infra">infra</a> are enough to update any datastructure. See the <a href="https://joypy.osdn.io/notebooks/Zipper.html">&#x201C;Traversing Datastructures with Zippers&#x201D; notebook</a>.</p>
883 <p>Note that the item that was on the top of the stack (<code>x</code> in the example above) will not be treated specially by the interpreter when it is reached again. This is something of a footgun. My advice is to avoid putting bare unquoted symbols onto the stack, but then you can&#x2019;t use symbols as &#x201C;atoms&#x201D; and also use <code>dip</code> and <code>infra</code> to operate on compound datastructures with atoms in them. This is a kind of side-effect of the Continuation-Passing Style. The <code>dip</code> combinator could &#x201C;set aside&#x201D; the item and replace it after running <code>Q</code> but that means that there is an &#x201C;extra space&#x201D; where the item resides while <code>Q</code> runs. One of the nice things about CPS is that the whole state is recorded in the stack and pending expression (not counting modifications to the dictionary.)</p>
884 <h3 id="crosslinks-26">Crosslinks</h3>
885 <p><a href="#dipd">dipd</a> <a href="#dipdd">dipdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a> <a href="#infra">infra</a></p>
886 <hr />
887 <h2 id="dipd">dipd</h2>
888 <p>Combinator</p>
889 <p>Like <a href="#dip">dip</a> but expects two items.</p>
890 <pre><code>   ... y x [Q] . dipd
891 -------------------------
892            ... . Q y x</code></pre>
893 <h3 id="discussion-25">Discussion</h3>
894 <p>See <a href="#dip">dip</a>.</p>
895 <h3 id="crosslinks-27">Crosslinks</h3>
896 <p><a href="#dip">dip</a> <a href="#dipdd">dipdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a> <a href="#infra">infra</a></p>
897 <hr />
898 <h2 id="dipdd">dipdd</h2>
899 <p>Combinator</p>
900 <p>Like <a href="#dip">dip</a> but expects three items. :</p>
901 <pre><code>   ... z y x [Q] . dip
902 -----------------------------
903              ... . Q z y x</code></pre>
904 <h3 id="discussion-26">Discussion</h3>
905 <p>See <a href="#dip">dip</a>.</p>
906 <h3 id="crosslinks-28">Crosslinks</h3>
907 <p><a href="#dip">dip</a> <a href="#dipd">dipd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a> <a href="#infra">infra</a></p>
908 <hr />
909 <h2 id="disenstacken">disenstacken</h2>
910 <p>Function</p>
911 <p>The <code>disenstacken</code> function expects a list on top of the stack and makes that the stack discarding the rest of the stack.</p>
912 <pre><code>   1 2 3 [4 5 6] disenstacken
913 --------------------------------
914             6 5 4</code></pre>
915 <h3 id="definition-24">Definition</h3>
916 <blockquote>
917 <p>[<a href="#clear">clear</a>] <a href="#dip">dip</a> <a href="#reverse">reverse</a> <a href="#unstack">unstack</a></p>
918 </blockquote>
919 <h3 id="discussion-27">Discussion</h3>
920 <p>Note that the order of the list is not changed, it just looks that way because the stack is printed with the top on the right while lists are printed with the top or head on the left.</p>
921 <h3 id="crosslinks-29">Crosslinks</h3>
922 <p><a href="#enstacken">enstacken</a> <a href="#stack">stack</a> <a href="#unstack">unstack</a></p>
923 <hr />
924 <h2 id="div">div</h2>
925 <p>See <a href="#floordiv">floordiv</a>.</p>
926 <hr />
927 <h2 id="divmod">divmod</h2>
928 <p>Function</p>
929 <pre><code>    x y divmod
930 ------------------
931      q      r
932    (x/y)  (x%y)</code></pre>
933 <p>Invariant: <code>qy + r = x</code>.</p>
934 <h3 id="definition-25">Definition</h3>
935 <blockquote>
936 <p>[<a href="#floordiv">floordiv</a>] [<a href="#mod">mod</a>] <a href="#clop">clop</a></p>
937 </blockquote>
938 <hr />
939 <h2 id="down_to_zero">down_to_zero</h2>
940 <p>Function</p>
941 <p>Given a number greater than zero put all the Natural numbers (including zero) less than that onto the stack.</p>
942 <h3 id="example-2">Example</h3>
943 <pre><code>   3 down_to_zero
944 --------------------
945       3 2 1 0</code></pre>
946 <h3 id="definition-26">Definition</h3>
947 <blockquote>
948 <p>[0 &gt;] [<a href="#dup">dup</a> <a href="#section-12">&#x2013;</a>] <a href="#while">while</a></p>
949 </blockquote>
950 <h3 id="crosslinks-30">Crosslinks</h3>
951 <p><a href="#range">range</a></p>
952 <hr />
953 <h2 id="drop">drop</h2>
954 <p>Function</p>
955 <p>Expects an integer and a quote on the stack and returns the quote with n items removed off the top.</p>
956 <h3 id="example-3">Example</h3>
957 <pre><code>   [a b c d] 2 drop
958 ----------------------
959        [c d]</code></pre>
960 <h3 id="definition-27">Definition</h3>
961 <blockquote>
962 <p>[<a href="#rest">rest</a>] <a href="#times">times</a></p>
963 </blockquote>
964 <h3 id="crosslinks-31">Crosslinks</h3>
965 <p><a href="#take">take</a></p>
966 <hr />
967 <h2 id="dup">dup</h2>
968 <p>Basis Function</p>
969 <p>&#x201C;Dup&#x201D;licate the top item on the stack.</p>
970 <pre><code>   a dup
971 -----------
972     a a</code></pre>
973 <h3 id="crosslinks-32">Crosslinks</h3>
974 <p><a href="#dupd">dupd</a> <a href="#dupdd">dupdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
975 <hr />
976 <h2 id="dupd">dupd</h2>
977 <p>Function</p>
978 <p><a href="#dup">dup</a> the second item down on the stack.</p>
979 <pre><code>   a b dupd
980 --------------
981     a a b</code></pre>
982 <h3 id="definition-28">Definition</h3>
983 <blockquote>
984 <p>[<a href="#dup">dup</a>] <a href="#dip">dip</a></p>
985 </blockquote>
986 <h3 id="crosslinks-33">Crosslinks</h3>
987 <p><a href="#dup">dup</a> <a href="#dupdd">dupdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
988 <hr />
989 <h2 id="dupdd">dupdd</h2>
990 <p>Function</p>
991 <p><a href="#dup">dup</a> the third item down on the stack.</p>
992 <pre><code>   a b c dupdd
993 -----------------
994      a a b c</code></pre>
995 <h3 id="definition-29">Definition</h3>
996 <blockquote>
997 <p>[<a href="#dup">dup</a>] <a href="#dipd">dipd</a></p>
998 </blockquote>
999 <h3 id="crosslinks-34">Crosslinks</h3>
1000 <p><a href="#dup">dup</a> <a href="#dupd">dupd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
1001 <hr />
1002 <h2 id="dupdip">dupdip</h2>
1003 <p>Combinator</p>
1004 <p>Apply a function <code>F</code> and <a href="#dup">dup</a> the item under it on the stack.</p>
1005 <pre><code>   a [F] dupdip
1006 ------------------
1007       a F a</code></pre>
1008 <h3 id="definition-30">Definition</h3>
1009 <blockquote>
1010 <p><a href="#dupd">dupd</a> <a href="#dip">dip</a></p>
1011 </blockquote>
1012 <h3 id="derivation-2">Derivation</h3>
1013 <pre><code>a [F] dupdip
1014 a [F] dupd dip
1015 a [F] [dup] dip dip
1016 a dup [F] dip
1017 a a [F] dip
1018 a F a</code></pre>
1019 <h3 id="discussion-28">Discussion</h3>
1020 <p>A very common and useful combinator.</p>
1021 <h3 id="crosslinks-35">Crosslinks</h3>
1022 <p><a href="#dupdipd">dupdipd</a></p>
1023 <hr />
1024 <h2 id="dupdipd">dupdipd</h2>
1025 <p>Combinator</p>
1026 <p>Run a copy of program <code>F</code> under the next item down on the stack.</p>
1027 <pre><code>   a [F] dupdipd
1028 -------------------
1029       F a [F]</code></pre>
1030 <h3 id="definition-31">Definition</h3>
1031 <blockquote>
1032 <p><a href="#dup">dup</a> <a href="#dipd">dipd</a></p>
1033 </blockquote>
1034 <h3 id="crosslinks-36">Crosslinks</h3>
1035 <p><a href="#dupdip">dupdip</a></p>
1036 <hr />
1037 <h2 id="enstacken">enstacken</h2>
1038 <p>Function</p>
1039 <p>Put the stack onto the stack replacing the contents of the stack.</p>
1040 <pre><code>   ... a b c enstacken
1041 -------------------------
1042        [c b a ...]</code></pre>
1043 <h3 id="definition-32">Definition</h3>
1044 <blockquote>
1045 <p><a href="#stack">stack</a> [<a href="#clear">clear</a>] <a href="#dip">dip</a></p>
1046 </blockquote>
1047 <h3 id="discussion-29">Discussion</h3>
1048 <p>This is a destructive version of <a href="#stack">stack</a>. See the note under <a href="#disenstacken">disenstacken</a> about the apparent but illusory reversal of the stack.</p>
1049 <h3 id="crosslinks-37">Crosslinks</h3>
1050 <p><a href="#stack">stack</a> [unstack] <a href="#disenstacken">disenstacken</a></p>
1051 <hr />
1052 <h2 id="eq">eq</h2>
1053 <p>Basis Function</p>
1054 <p>Compare the two items on the top of the stack for equality and replace them with a Boolean value.</p>
1055 <pre><code>   a b eq
1056 -------------
1057    Boolean
1058    (a = b)</code></pre>
1059 <h3 id="crosslinks-38">Crosslinks</h3>
1060 <p><a href="#cmp">cmp</a> <a href="#ge">ge</a> <a href="#gt">gt</a> <a href="#le">le</a> <a href="#lt">lt</a> <a href="#ne">ne</a></p>
1061 <hr />
1062 <h2 id="first">first</h2>
1063 <p>Function</p>
1064 <p>Replace a list with its first item.</p>
1065 <pre><code>   [a ...]
1066 --------------
1067       a</code></pre>
1068 <h3 id="definition-33">Definition</h3>
1069 <blockquote>
1070 <p><a href="#uncons">uncons</a> <a href="#pop">pop</a></p>
1071 </blockquote>
1072 <h3 id="crosslinks-39">Crosslinks</h3>
1073 <p><a href="#second">second</a> <a href="#third">third</a> <a href="#fourth">fourth</a> <a href="#rest">rest</a></p>
1074 <hr />
1075 <h2 id="first_two">first_two</h2>
1076 <p>Function</p>
1077 <p>Replace a list with its first two items.</p>
1078 <pre><code>   [a b ...] first_two
1079 -------------------------
1080            a b</code></pre>
1081 <h3 id="definition-34">Definition</h3>
1082 <blockquote>
1083 <p><a href="#uncons">uncons</a> <a href="#first">first</a></p>
1084 </blockquote>
1085 <h3 id="crosslinks-40">Crosslinks</h3>
1086 <p><a href="#first">first</a> <a href="#second">second</a> <a href="#third">third</a> <a href="#fourth">fourth</a> <a href="#rest">rest</a></p>
1087 <hr />
1088 <h2 id="flatten">flatten</h2>
1089 <p>Function</p>
1090 <p>Given a list of lists, concatinate them.</p>
1091 <h3 id="example-4">Example</h3>
1092 <pre><code>   [[1 2] [3 [4] 5] [6 7]] flatten
1093 -------------------------------------
1094           [1 2 3 [4] 5 6 7]</code></pre>
1095 <h3 id="definition-35">Definition</h3>
1096 <blockquote>
1097 <p><a href="#section-16">&lt;{}</a> [<a href="#concat">concat</a>] <a href="#step">step</a></p>
1098 </blockquote>
1099 <h3 id="discussion-30">Discussion</h3>
1100 <p>Note that only one &#x201C;level&#x201D; of lists is flattened. In the example above <code>[4]</code> is not unquoted.</p>
1101 <h3 id="crosslinks-41">Crosslinks</h3>
1102 <p><a href="#concat">concat</a> <a href="#first">first</a> <a href="#first_two">first_two</a> <a href="#fourth">fourth</a> <a href="#getitem">getitem</a> <a href="#remove">remove</a> <a href="#rest">rest</a> <a href="#reverse">reverse</a> <a href="#rrest">rrest</a> <a href="#second">second</a> <a href="#shift">shift</a> <a href="#shunt">shunt</a> <a href="#size">size</a> <a href="#sort">sort</a> <a href="#split_at">split_at</a> <a href="#split_list">split_list</a> <a href="#swaack">swaack</a> <a href="#third">third</a> <a href="#zip">zip</a></p>
1103 <hr />
1104 <h2 id="floor-1">floor</h2>
1105 <p>Basis Function</p>
1106 <p>Return the largest integer &lt;= x.</p>
1107 <h3 id="discussion-31">Discussion</h3>
1108 <p>This function doesn&#x2019;t make sense (yet) to have because there are (as yet) only integers in the system.</p>
1109 <hr />
1110 <h2 id="floordiv">floordiv</h2>
1111 <p>Basis Function</p>
1112 <p>I don&#x2019;t know why this is called &#x201C;floor&#x201D; div, I think it rounds its result down (not towards zero or up.)</p>
1113 <pre><code>   a b floordiv
1114 ------------------
1115       (a/b)</code></pre>
1116 <h3 id="discussion-32">Discussion</h3>
1117 <p>All the division commands need to be revisited when the &#x201C;numeric tower&#x201D; for Thun gets nailed down.</p>
1118 <h3 id="crosslinks-42">Crosslinks</h3>
1119 <p><a href="#divmod">divmod</a></p>
1120 <hr />
1121 <h2 id="fork">fork</h2>
1122 <p>Combinator</p>
1123 <p>Run two quoted programs in parallel and replace them with their results.</p>
1124 <pre><code>   ... [F] [G] fork
1125 ----------------------
1126        ... f g</code></pre>
1127 <h3 id="definition-36">Definition</h3>
1128 <blockquote>
1129 <p>[<a href="#i">i</a>] <a href="#app2">app2</a></p>
1130 </blockquote>
1131 <h3 id="discussion-33">Discussion</h3>
1132 <p>The basic parallelism combinator, the two programs are run independently.</p>
1133 <h3 id="crosslinks-43">Crosslinks</h3>
1134 <p><a href="#cleave">cleave</a> <a href="#clop">clop</a> <a href="#map">map</a></p>
1135 <hr />
1136 <h2 id="fourth">fourth</h2>
1137 <p>Function</p>
1138 <p>Replace a list with its fourth item.</p>
1139 <pre><code>   [a b c d ...] fourth
1140 --------------------------
1141           d</code></pre>
1142 <h3 id="definition-37">Definition</h3>
1143 <blockquote>
1144 <p><a href="#rest">rest</a> <a href="#third">third</a></p>
1145 </blockquote>
1146 <h3 id="crosslinks-44">Crosslinks</h3>
1147 <p><a href="#first">first</a> <a href="#second">second</a> <a href="#third">third</a> <a href="#rest">rest</a></p>
1148 <hr />
1149 <h2 id="gcd">gcd</h2>
1150 <p>Function</p>
1151 <p>Take two integers from the stack and replace them with their Greatest Common Denominator.</p>
1152 <h3 id="definition-38">Definition</h3>
1153 <blockquote>
1154 <p>true [<a href="#tuck">tuck</a> <a href="#mod">mod</a> <a href="#dup">dup</a> 0 <a href="#section-8">&gt;</a>] <a href="#loop">loop</a> <a href="#pop">pop</a></p>
1155 </blockquote>
1156 <h3 id="discussion-34">Discussion</h3>
1157 <p>Euclid&#x2019;s Algorithm</p>
1158 <hr />
1159 <h2 id="gcd2">gcd2</h2>
1160 <p>Function</p>
1161 <p>Compiled GCD function.</p>
1162 <h3 id="discussion-35">Discussion</h3>
1163 <p>See <a href="#gcd">gcd</a>.</p>
1164 <h3 id="crosslinks-45">Crosslinks</h3>
1165 <p><a href="#gcd">gcd</a></p>
1166 <hr />
1167 <h2 id="ge">ge</h2>
1168 <p>Basis Function</p>
1169 <p>Greater-than-or-equal-to comparison of two numbers.</p>
1170 <pre><code>   a b ge
1171 --------------
1172    Boolean
1173    (a &gt;= b)</code></pre>
1174 <h3 id="crosslinks-46">Crosslinks</h3>
1175 <p><a href="#cmp">cmp</a> <a href="#eq">eq</a> <a href="#gt">gt</a> <a href="#le">le</a> <a href="#lt">lt</a> <a href="#ne">ne</a></p>
1176 <hr />
1177 <h2 id="genrec">genrec</h2>
1178 <p>Combinator</p>
1179 <p><strong>Gen</strong>eral <strong>Rec</strong>ursion Combinator.</p>
1180 <pre><code>                      [if] [then] [rec1] [rec2] genrec
1181 ---------------------------------------------------------------------
1182    [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte</code></pre>
1183 <h3 id="definition-39">Definition</h3>
1184 <blockquote>
1185 <p>[[<a href="#genrec">genrec</a>] <a href="#ccccons">ccccons</a>] <a href="#nullary">nullary</a> <a href="#swons">swons</a> <a href="#concat">concat</a> <a href="#ifte">ifte</a></p>
1186 </blockquote>
1187 <p>(Note that this definition includes the <code>genrec</code> symbol itself, it is self-referential. This is possible because the definition machinery does not check that symbols in defs are in the dictionary. <code>genrec</code> is the only self-referential definition.)</p>
1188 <h3 id="discussion-36">Discussion</h3>
1189 <p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
1190 <p>From <a href="https://www.kevinalbrecht.com/code/joy-mirror/j05cmp.html">&#x201C;Recursion Theory and Joy&#x201D;</a> by Manfred von Thun:</p>
1191 <blockquote>
1192 <p>&#x201C;The genrec combinator takes four program parameters in addition to whatever data parameters it needs. Fourth from the top is an if-part, followed by a then-part. If the if-part yields true, then the then-part is executed and the combinator terminates. The other two parameters are the rec1-part and the rec2-part. If the if-part yields false, the rec1-part is executed. Following that the four program parameters and the combinator are again pushed onto the stack bundled up in a quoted form. Then the rec2-part is executed, where it will find the bundled form. Typically it will then execute the bundled form, either with i or with app2, or some other combinator.&#x201D;</p>
1193 </blockquote>
1194 <p>The way to design one of these is to fix your base case <code>[then]</code> and the test <code>[if]</code>, and then treat <code>rec1</code> and <code>rec2</code> as an else-part &#x201C;sandwiching&#x201D; a quotation of the whole function.</p>
1195 <p>For example, given a (general recursive) function <code>F</code>:</p>
1196 <pre><code>F == [I] [T] [R1] [R2] genrec</code></pre>
1197 <p>If the <code>[I]</code> if-part fails you must derive <code>R1</code> and <code>R2</code> from: :</p>
1198 <pre><code>... R1 [F] R2</code></pre>
1199 <p>Just set the stack arguments in front, and figure out what <code>R1</code> and <code>R2</code> have to do to apply the quoted <code>[F]</code> in the proper way. In effect, the <code>genrec</code> combinator turns into an <a href="#ifte">ifte</a> combinator with a quoted copy of the original definition in the else-part:</p>
1200 <pre><code>F == [I] [T] [R1]   [R2] genrec
1201   == [I] [T] [R1 [F] R2] ifte</code></pre>
1202 <p>Tail recursive functions are those where <code>R2</code> is the <code>i</code> combinator:</p>
1203 <pre><code>P == [I] [T] [R] tailrec
1204   == [I] [T] [R [P] i] ifte
1205   == [I] [T] [R P] ifte</code></pre>
1206 <h3 id="crosslinks-47">Crosslinks</h3>
1207 <p><a href="#anamorphism">anamorphism</a> <a href="#tailrec">tailrec</a> <a href="#x">x</a></p>
1208 <hr />
1209 <h2 id="getitem">getitem</h2>
1210 <p>Function</p>
1211 <p>Expects an integer and a quote on the stack and returns the item at the nth position in the quote counting from 0.</p>
1212 <h3 id="example-5">Example</h3>
1213 <pre><code>   [a b c d] 2 getitem
1214 -------------------------
1215         c</code></pre>
1216 <h3 id="definition-40">Definition</h3>
1217 <blockquote>
1218 <p><a href="#drop">drop</a> <a href="#first">first</a></p>
1219 </blockquote>
1220 <h3 id="discussion-37">Discussion</h3>
1221 <p>If the number isn&#x2019;t a valid index into the quote <code>getitem</code> will cause some sort of problem (the exact nature of which is implementation-dependant.)</p>
1222 <h3 id="crosslinks-48">Crosslinks</h3>
1223 <p><a href="#concat">concat</a> <a href="#first">first</a> <a href="#first_two">first_two</a> <a href="#flatten">flatten</a> <a href="#fourth">fourth</a> <a href="#remove">remove</a> <a href="#rest">rest</a> <a href="#reverse">reverse</a> <a href="#rrest">rrest</a> <a href="#second">second</a> <a href="#shift">shift</a> <a href="#shunt">shunt</a> <a href="#size">size</a> <a href="#sort">sort</a> <a href="#split_at">split_at</a> <a href="#split_list">split_list</a> <a href="#swaack">swaack</a> <a href="#third">third</a> <a href="#zip">zip</a></p>
1224 <hr />
1225 <h2 id="grabn">grabN</h2>
1226 <p>Function</p>
1227 <p>Expect a number on the top of the stack and <a href="#cons">cons</a> that many items from under it onto a new list.</p>
1228 <h3 id="example-6">Example</h3>
1229 <pre><code>   a b c d e 3 grabN
1230 -----------------------
1231       a b [c d e]</code></pre>
1232 <h3 id="definition-41">Definition</h3>
1233 <blockquote>
1234 <p><a href="#section-16">&lt;{}</a> [<a href="#cons">cons</a>] <a href="#times">times</a></p>
1235 </blockquote>
1236 <hr />
1237 <h2 id="grba">grba</h2>
1238 <p>Function</p>
1239 <p>A weird function used in <a href="#app2">app2</a> that does this:</p>
1240 <pre><code>      ... 1 2 3 4 5 grba
1241 -------------------------------
1242    ... 1 2 3 [4 3 2 1 ...] 5</code></pre>
1243 <p>It grabs the stack under the top item, and substitutes it for the second item down on the stack.</p>
1244 <h3 id="definition-42">Definition</h3>
1245 <blockquote>
1246 <p>[<a href="#stack">stack</a> <a href="#popd">popd</a>] <a href="#dip">dip</a></p>
1247 </blockquote>
1248 <h3 id="discussion-38">Discussion</h3>
1249 <p>This function &#x201C;grabs&#x201D; an item from the stack along with a copy of the stack. It&#x2019;s part of the <a href="#app2">app2</a> definition.</p>
1250 <h3 id="crosslinks-49">Crosslinks</h3>
1251 <p><a href="#app2">app2</a></p>
1252 <hr />
1253 <h2 id="gt">gt</h2>
1254 <p>Basis Function</p>
1255 <p>Greater-than comparison of two numbers.</p>
1256 <pre><code>   a b gt
1257 --------------
1258    Boolean
1259    (a &gt; b)</code></pre>
1260 <h3 id="crosslinks-50">Crosslinks</h3>
1261 <p><a href="#cmp">cmp</a> <a href="#eq">eq</a> <a href="#ge">ge</a> <a href="#le">le</a> <a href="#lt">lt</a> <a href="#ne">ne</a></p>
1262 <hr />
1263 <h2 id="help">help</h2>
1264 <p>Function</p>
1265 <p>Accepts a quoted symbol on the top of the stack and prints its documentation.</p>
1266 <pre><code>   [foo] help
1267 ----------------</code></pre>
1268 <h3 id="discussion-39">Discussion</h3>
1269 <p>Technically this is equivalent to <code>pop</code>, but it will only work if the item on the top of the stack is a quoted symbol.</p>
1270 <hr />
1271 <h2 id="hypot">hypot</h2>
1272 <p>Function</p>
1273 <pre><code>         x y hypot
1274 ---------------------------
1275    sqrt(sqr(x) + sqr(y))</code></pre>
1276 <h3 id="definition-43">Definition</h3>
1277 <blockquote>
1278 <p>[<a href="#sqr">sqr</a>] <a href="#ii">ii</a> <a href="#section-20">+</a> <a href="#sqrt">sqrt</a></p>
1279 </blockquote>
1280 <h3 id="discussion-40">Discussion</h3>
1281 <p>This is another function that has to wait on the numeric tower.</p>
1282 <h3 id="crosslinks-51">Crosslinks</h3>
1283 <p><a href="#sqrt">sqrt</a></p>
1284 <hr />
1285 <h2 id="i">i</h2>
1286 <p>Basis Combinator</p>
1287 <p>Append a quoted expression onto the pending expression.</p>
1288 <pre><code>   [Q] . i
1289 -------------
1290        . Q</code></pre>
1291 <h3 id="discussion-41">Discussion</h3>
1292 <p>This is a fundamental combinator. It is used in all kinds of places. For example, the <a href="#x">x</a> combinator can be defined as <code>dup i</code>.</p>
1293 <hr />
1294 <h2 id="id">id</h2>
1295 <p>Basis Function</p>
1296 <p>The identity function.</p>
1297 <h3 id="discussion-42">Discussion</h3>
1298 <p>Does nothing. It&#x2019;s kind of a mathematical thing, but it occasionally comes in handy.</p>
1299 <hr />
1300 <h2 id="ifte">ifte</h2>
1301 <p>Combinator</p>
1302 <p>If-Then-Else combinator, a common and convenient specialization of <a href="#branch">branch</a>.</p>
1303 <pre><code>        [if] [then] [else] ifte
1304 ---------------------------------------
1305    [if] nullary [else] [then] branch</code></pre>
1306 <h3 id="definition-44">Definition</h3>
1307 <blockquote>
1308 <p>[<a href="#nullary">nullary</a>] <a href="#dipd">dipd</a> <a href="#swap">swap</a> <a href="#branch">branch</a></p>
1309 </blockquote>
1310 <h3 id="crosslinks-52">Crosslinks</h3>
1311 <p><a href="#branch">branch</a> <a href="#loop">loop</a> <a href="#while">while</a></p>
1312 <hr />
1313 <h2 id="ii">ii</h2>
1314 <p>Combinator</p>
1315 <p>Take a quoted program from the stack and run it twice, first under the top item, then again with the top item.</p>
1316 <pre><code>... a [Q] ii
1317 ------------------
1318  ... Q a Q</code></pre>
1319 <h3 id="definition-45">Definition</h3>
1320 <blockquote>
1321 <p>[<a href="#dip">dip</a>] <a href="#dupdip">dupdip</a> <a href="#i">i</a></p>
1322 </blockquote>
1323 <h3 id="example-7">Example</h3>
1324 <p>It&#x2019;s a little tricky to understand how this works so here&#x2019;s an example trace:</p>
1325 <pre><code>      1 2 3 4 [++] &#x2022; [dip] dupdip i
1326 1 2 3 4 [++] [dip] &#x2022; dupdip i
1327       1 2 3 4 [++] &#x2022; dip [++] i
1328              1 2 3 &#x2022; ++ 4 [++] i
1329              1 2 4 &#x2022; 4 [++] i
1330            1 2 4 4 &#x2022; [++] i
1331       1 2 4 4 [++] &#x2022; i
1332            1 2 4 4 &#x2022; ++
1333            1 2 4 5 &#x2022;</code></pre>
1334 <h3 id="discussion-43">Discussion</h3>
1335 <p>In some cases (like the example above) this is the same effect as using <a href="#app2">app2</a> but most of the time it&#x2019;s not:</p>
1336 <pre><code>   1 2 3 4 [+] ii
1337 --------------------
1338         1 9
1339
1340    1 2 3 4 [+] app2
1341 ----------------------
1342        1 2 5 6</code></pre>
1343 <h3 id="crosslinks-53">Crosslinks</h3>
1344 <p><a href="#app2">app2</a> <a href="#b">b</a></p>
1345 <hr />
1346 <h2 id="infra">infra</h2>
1347 <p>Combinator</p>
1348 <p>Accept a quoted program and a list on the stack and run the program with the list as its stack. Does not affect the stack (below the list.)</p>
1349 <pre><code>   ... x y z [a b c] [Q] infra
1350 ---------------------------------
1351     c b a Q [z y x ...] swaack</code></pre>
1352 <h3 id="definition-46">Definition</h3>
1353 <blockquote>
1354 <p><a href="#swons">swons</a> <a href="#swaack">swaack</a> [<a href="#i">i</a>] <a href="#dip">dip</a> <a href="#swaack">swaack</a></p>
1355 </blockquote>
1356 <h3 id="discussion-44">Discussion</h3>
1357 <p>This is one of the more useful combinators. It allows a quoted expression to serve as a stack for a program, effectively running it in a kind of &#x201C;pocket universe&#x201D;. If the list represents a datastructure then <code>infra</code> lets you work on its internal structure.</p>
1358 <h3 id="crosslinks-54">Crosslinks</h3>
1359 <p><a href="#swaack">swaack</a></p>
1360 <hr />
1361 <h2 id="infrst">infrst</h2>
1362 <p>Combinator</p>
1363 <p>Does <a href="#infra">infra</a> and then extracts the <a href="#first">first</a> item from the resulting list.</p>
1364 <h3 id="definition-47">Definition</h3>
1365 <blockquote>
1366 <p><a href="#infra">infra</a> <a href="#first">first</a></p>
1367 </blockquote>
1368 <hr />
1369 <h2 id="inscribe">inscribe</h2>
1370 <p>Create a new Joy function definition in the Joy dictionary. A definition is given as a quote with a name followed by a Joy expression.</p>
1371 <h3 id="example-8">Example</h3>
1372 <pre><code>[sqr dup mul] inscribe</code></pre>
1373 <h3 id="discussion-45">Discussion</h3>
1374 <p>This is the only function that modifies the dictionary. It&#x2019;s provided as a convenience, for tinkering with new definitions before entering them into the <code>defs.txt</code> file. It can be abused, which you should avoid unless you know what you&#x2019;re doing.</p>
1375 <hr />
1376 <h2 id="le">le</h2>
1377 <p>Basis Function</p>
1378 <p>Less-Than-or-Equal-to comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
1379 <pre><code>   a b le
1380 -------------
1381    Boolean
1382    (a &lt;= b)</code></pre>
1383 <h3 id="crosslinks-55">Crosslinks</h3>
1384 <p><a href="#cmp">cmp</a> <a href="#eq">eq</a> <a href="#ge">ge</a> <a href="#gt">gt</a> <a href="#lt">lt</a> <a href="#ne">ne</a></p>
1385 <hr />
1386 <h2 id="loop">loop</h2>
1387 <p>Basis Combinator</p>
1388 <p>Expect a quoted program <code>Q</code> and a Boolean value on the stack. If the value is false discard the quoted program, otherwise run a copy of <code>Q</code> and <code>loop</code> again.</p>
1389 <pre><code>   false [Q] loop
1390 --------------------
1391
1392
1393    true [Q] . loop
1394 --------------------------
1395             . Q [Q] loop</code></pre>
1396 <h3 id="discussion-46">Discussion</h3>
1397 <p>This, along with <a href="#branch">branch</a> and <a href="#fork">fork</a>, is one of the four main combinators of all programming. The fourth, sequence, is implied by juxtaposition. That is to say, in Joy <code>F G</code> is like <code>G(F(...))</code> in a language bassed on function application. Or again, to quote the <a href="https://en.wikipedia.org/wiki/Joy_(programming_language)#Mathematical_purity">Joy Wikipedia entry</a>,</p>
1398 <blockquote>
1399 <p>In Joy, the meaning function is a homomorphism from the syntactic monoid onto the semantic monoid. That is, the syntactic relation of concatenation of symbols maps directly onto the semantic relation of composition of functions.</p>
1400 </blockquote>
1401 <p>Anyway, <a href="#branch">branch</a>, <a href="#fork">fork</a>, amd <a href="#loop">loop</a> are the fundamental combinators in Joy. Just as <a href="#branch">branch</a> has it&#x2019;s more common and convenient form <a href="#ifte">ifte</a>, <a href="#loop">loop</a> has <a href="#while">while</a>.</p>
1402 <h3 id="crosslinks-56">Crosslinks</h3>
1403 <p><a href="#branch">branch</a> <a href="#fork">fork</a> <a href="#while">while</a></p>
1404 <hr />
1405 <h2 id="lshift">lshift</h2>
1406 <p>Basis Function</p>
1407 <p><a href="https://en.wikipedia.org/wiki/Logical_shift">Logical Left-Shift</a></p>
1408 <pre><code>   a n lshift
1409 ----------------
1410      (a&#xD7;2&#x207F;)</code></pre>
1411 <h3 id="crosslinks-57">Crosslinks</h3>
1412 <p><a href="#rshift">rshift</a></p>
1413 <hr />
1414 <h2 id="lt">lt</h2>
1415 <p>Basis Function</p>
1416 <p>Less-Than comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
1417 <pre><code>   a b lt
1418 -------------
1419    Boolean
1420    (a &lt; b)</code></pre>
1421 <h3 id="crosslinks-58">Crosslinks</h3>
1422 <p><a href="#cmp">cmp</a> <a href="#eq">eq</a> <a href="#ge">ge</a> <a href="#gt">gt</a> <a href="#le">le</a> <a href="#ne">ne</a></p>
1423 <hr />
1424 <h2 id="make_generator">make_generator</h2>
1425 <p>Function</p>
1426 <p>Given an initial state value and a quoted generator function build a generator quote.</p>
1427 <pre><code>   state [generator function] make_generator
1428 -----------------------------------------------
1429      [state [generator function] codireco]</code></pre>
1430 <h3 id="example-9">Example</h3>
1431 <pre><code>   230 [dup ++] make_generator
1432 ---------------------------------
1433      [230 [dup ++] codireco]</code></pre>
1434 <p>And then:</p>
1435 <pre><code>   [230 [dup ++] codireco] 5 [x] times pop
1436 ---------------------------------------------
1437              230 231 232 233 234</code></pre>
1438 <h3 id="definition-48">Definition</h3>
1439 <blockquote>
1440 <p>[<a href="#codireco">codireco</a>] <a href="#ccons">ccons</a></p>
1441 </blockquote>
1442 <h3 id="discussion-47">Discussion</h3>
1443 <p>See the <a href="https://joypy.osdn.io/notebooks/Generator_Programs.html#an-interesting-variation">&#x201C;Using <code>x</code> to Generate Values&#x201D; notebook</a>.</p>
1444 <h3 id="crosslinks-59">Crosslinks</h3>
1445 <p><a href="#codireco">codireco</a></p>
1446 <hr />
1447 <h2 id="map">map</h2>
1448 <p>Combinator</p>
1449 <p>Given a list of items and a quoted program run the program for each item in the list (with the rest of the stack) and replace the old list and the program with a list of the results.</p>
1450 <h3 id="example-10">Example</h3>
1451 <pre><code>   5 [1 2 3] [++ *] map
1452 --------------------------
1453        5 [10 15 20]</code></pre>
1454 <h3 id="discussion-48">Discussion</h3>
1455 <p>This is a common operation in many languages. In Joy it can be a parallelism combinator due to the &#x201C;pure&#x201D; nature of the language.</p>
1456 <h3 id="crosslinks-60">Crosslinks</h3>
1457 <p><a href="#app1">app1</a> <a href="#app2">app2</a> <a href="#app3">app3</a> <a href="#appn">appN</a> <a href="#fork">fork</a></p>
1458 <hr />
1459 <h2 id="max">max</h2>
1460 <p>Basis Function</p>
1461 <p>Given a list find the maximum.</p>
1462 <h3 id="example-11">Example</h3>
1463 <pre><code>   [1 2 3 4] max
1464 -------------------
1465          4</code></pre>
1466 <h3 id="crosslinks-61">Crosslinks</h3>
1467 <p><a href="#min">min</a> <a href="#size">size</a> <a href="#sum">sum</a></p>
1468 <hr />
1469 <h2 id="min">min</h2>
1470 <p>Basis Function</p>
1471 <p>Given a list find the minimum.</p>
1472 <h3 id="example-12">Example</h3>
1473 <pre><code>   [1 2 3 4] min
1474 -------------------
1475          1 </code></pre>
1476 <h3 id="crosslinks-62">Crosslinks</h3>
1477 <p><a href="#max">max</a> <a href="#size">size</a> <a href="#sum">sum</a></p>
1478 <hr />
1479 <h2 id="mod">mod</h2>
1480 <p>Basis Function</p>
1481 <p>Return the remainder of <code>a</code> divided by <code>b</code>.</p>
1482 <pre><code>   a b mod
1483 -------------
1484     (a%b)</code></pre>
1485 <h3 id="crosslinks-63">Crosslinks</h3>
1486 <p><a href="#divmod">divmod</a> <a href="#mul">mul</a></p>
1487 <hr />
1488 <h2 id="modulus">modulus</h2>
1489 <p>See <a href="#mod">mod</a>.</p>
1490 <hr />
1491 <h2 id="mul">mul</h2>
1492 <p>Basis Function</p>
1493 <p>Multiply two numbers.</p>
1494 <pre><code>   a b mul
1495 -------------
1496     (a&#xD7;b)</code></pre>
1497 <h3 id="crosslinks-64">Crosslinks</h3>
1498 <p><a href="#div">div</a> <a href="#product">product</a></p>
1499 <hr />
1500 <h2 id="ne">ne</h2>
1501 <p>Basis Function</p>
1502 <p>Not-Equal comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
1503 <pre><code>   a b ne
1504 -------------
1505    Boolean
1506    (a = b)</code></pre>
1507 <h3 id="crosslinks-65">Crosslinks</h3>
1508 <p><a href="#cmp">cmp</a> <a href="#eq">eq</a> <a href="#ge">ge</a> <a href="#gt">gt</a> <a href="#le">le</a> <a href="#lt">lt</a></p>
1509 <hr />
1510 <h2 id="neg">neg</h2>
1511 <p>Function</p>
1512 <p>Invert the sign of a number.</p>
1513 <pre><code>   a neg
1514 -----------
1515     -a</code></pre>
1516 <h3 id="definition-49">Definition</h3>
1517 <blockquote>
1518 <p>0 <a href="#swap">swap</a> <a href="#section-11">-</a></p>
1519 </blockquote>
1520 <hr />
1521 <h2 id="not">not</h2>
1522 <p>Function</p>
1523 <p>Like <a href="#bool">bool</a> but convert the item on the top of the stack to the inverse Boolean value.</p>
1524 <pre><code>   true not
1525 --------------
1526     false
1527
1528    false not
1529 ---------------
1530      true</code></pre>
1531 <h3 id="definition-50">Definition</h3>
1532 <blockquote>
1533 <p><a href="#bool">bool</a> [true] [false] <a href="#branch">branch</a></p>
1534 </blockquote>
1535 <h3 id="crosslinks-66">Crosslinks</h3>
1536 <p><a href="#bool">bool</a></p>
1537 <hr />
1538 <h2 id="nulco">nulco</h2>
1539 <p>Function</p>
1540 <p>Take the item on the top of the stack and <a href="#cons">cons</a> it onto <code>[nullary]</code>.</p>
1541 <pre><code>     [F] nulco
1542 -------------------
1543    [[F] nullary]</code></pre>
1544 <h3 id="definition-51">Definition</h3>
1545 <blockquote>
1546 <p>[<a href="#nullary">nullary</a>] <a href="#cons">cons</a></p>
1547 </blockquote>
1548 <h3 id="discussion-49">Discussion</h3>
1549 <p>Helper function for <a href="#section-25">||</a> and <a href="#section-1">&amp;&amp;</a>.</p>
1550 <h3 id="crosslinks-67">Crosslinks</h3>
1551 <p><a href="#section-1">&amp;&amp;</a> <a href="#section-25">||</a></p>
1552 <hr />
1553 <h2 id="nullary">nullary</h2>
1554 <p>Combinator</p>
1555 <p>Run a quoted program without using any stack values and leave the first item of the result on the stack.</p>
1556 <pre><code>   ... [P] nullary
1557 ---------------------
1558         ... a</code></pre>
1559 <h3 id="definition-52">Definition</h3>
1560 <blockquote>
1561 <p>[<a href="#stack">stack</a>] <a href="#dip">dip</a> <a href="#infra">infra</a> <a href="#first">first</a></p>
1562 </blockquote>
1563 <h3 id="example-13">Example</h3>
1564 <pre><code>... [P] nullary
1565 ... [P] [stack] dip infra first
1566 ... stack [P] infra first
1567 ... [...] [P] infra first
1568 ... [a ...] first
1569 ...  a</code></pre>
1570 <h3 id="discussion-50">Discussion</h3>
1571 <p>A very useful function that runs any other quoted function and returns it&#x2019;s first result without disturbing the stack (under the quoted program.)</p>
1572 <h3 id="crosslinks-68">Crosslinks</h3>
1573 <p><a href="#unary">unary</a> <a href="#binary">binary</a> <a href="#ternary">ternary</a></p>
1574 <hr />
1575 <h2 id="of">of</h2>
1576 <p>Function</p>
1577 <p>Like <a href="#getitem">getitem</a> but <a href="#swap">swap</a>s the order of arguments.</p>
1578 <h3 id="example-14">Example</h3>
1579 <pre><code>   2 [a b c d] of
1580 --------------------
1581          c</code></pre>
1582 <h3 id="definition-53">Definition</h3>
1583 <blockquote>
1584 <p><a href="#swap">swap</a> <a href="#getitem">getitem</a></p>
1585 </blockquote>
1586 <h3 id="crosslinks-69">Crosslinks</h3>
1587 <p><a href="#getitem">getitem</a></p>
1588 <hr />
1589 <h2 id="or">or</h2>
1590 <p>Basis Function</p>
1591 <p>Logical bit-wise OR.</p>
1592 <h3 id="crosslinks-70">Crosslinks</h3>
1593 <p><a href="#and">and</a> <a href="#xor">xor</a></p>
1594 <hr />
1595 <h2 id="over">over</h2>
1596 <p>Function</p>
1597 <p><a href="#dup">dup</a> the second item on the stack <code>over</code> the first.</p>
1598 <pre><code>   a b over
1599 --------------
1600     a b a</code></pre>
1601 <h3 id="definition-54">Definition</h3>
1602 <p>There are many many ways to define this function.</p>
1603 <blockquote>
1604 <p><a href="#swap">swap</a> <a href="#tuck">tuck</a></p>
1605 </blockquote>
1606 <blockquote>
1607 <p>[<a href="#pop">pop</a>] <a href="#nullary">nullary</a></p>
1608 </blockquote>
1609 <blockquote>
1610 <p>[<a href="#dup">dup</a>] <a href="#dip">dip</a> <a href="#swap">swap</a></p>
1611 </blockquote>
1612 <blockquote>
1613 <p><a href="#unit">unit</a> <a href="#dupdip">dupdip</a></p>
1614 </blockquote>
1615 <blockquote>
1616 <p><a href="#unit">unit</a> <a href="#dupdipd">dupdipd</a> <a href="#first">first</a></p>
1617 </blockquote>
1618 <p>And so on&#x2026;</p>
1619 <h3 id="discussion-51">Discussion</h3>
1620 <p>A fine old word from Forth.</p>
1621 <h3 id="crosslinks-71">Crosslinks</h3>
1622 <p><a href="#tuck">tuck</a></p>
1623 <hr />
1624 <h2 id="pam">pam</h2>
1625 <p>Combinator</p>
1626 <p>Take a list of quoted functions from the stack and replace it with a list of the <a href="#first">first</a> results from running those functions (on copies of the rest of the stack.)</p>
1627 <h3 id="example-15">Example</h3>
1628 <pre><code>   5 7 [[+][-][*][/][%]] pam
1629 -------------------------------
1630       5 7 [12 -2 35 0 5]</code></pre>
1631 <h3 id="definition-55">Definition</h3>
1632 <blockquote>
1633 <p>[<a href="#i">i</a>] <a href="#map">map</a></p>
1634 </blockquote>
1635 <h3 id="discussion-52">Discussion</h3>
1636 <p>A specialization of <a href="#map">map</a> that runs a list of functions in parallel (if the underlying <a href="#map">map</a> function is so implemented, of course.)</p>
1637 <h3 id="crosslinks-72">Crosslinks</h3>
1638 <p><a href="#map">map</a></p>
1639 <hr />
1640 <h2 id="pick">pick</h2>
1641 <p>See <a href="#getitem">getitem</a>.</p>
1642 <hr />
1643 <h2 id="pm">pm</h2>
1644 <p>Function</p>
1645 <p>Plus or minus. Replace two numbers with their sum and difference.</p>
1646 <pre><code>      a b pm
1647 -----------------
1648    (a+b) (a-b)</code></pre>
1649 <h3 id="definition-56">Definition</h3>
1650 <blockquote>
1651 <p>[+] [-] <a href="#clop">clop</a></p>
1652 </blockquote>
1653 <hr />
1654 <h2 id="pop">pop</h2>
1655 <p>Basis Function</p>
1656 <p>Pop the top item from the stack and discard it.</p>
1657 <pre><code>   a pop
1658 -----------</code></pre>
1659 <h3 id="crosslinks-73">Crosslinks</h3>
1660 <p><a href="#popd">popd</a> <a href="#popdd">popdd</a> <a href="#popop">popop</a> <a href="#popopd">popopd</a> <a href="#popopdd">popopdd</a> <a href="#popopop">popopop</a></p>
1661 <hr />
1662 <h2 id="popd">popd</h2>
1663 <p>Function</p>
1664 <p><a href="#pop">pop</a> the second item down on the stack.</p>
1665 <pre><code>   a b popd
1666 --------------
1667       b</code></pre>
1668 <h3 id="definition-57">Definition</h3>
1669 <blockquote>
1670 <p><a href="#swap">swap</a> <a href="#pop">pop</a></p>
1671 </blockquote>
1672 <h3 id="crosslinks-74">Crosslinks</h3>
1673 <p><a href="#pop">pop</a> <a href="#popdd">popdd</a> <a href="#popop">popop</a> <a href="#popopd">popopd</a> <a href="#popopdd">popopdd</a> <a href="#popopop">popopop</a></p>
1674 <hr />
1675 <h2 id="popdd">popdd</h2>
1676 <p>Function</p>
1677 <p><a href="#pop">pop</a> the third item on the stack.</p>
1678 <pre><code>   a b c popdd
1679 -----------------
1680        b c</code></pre>
1681 <h3 id="definition-58">Definition</h3>
1682 <blockquote>
1683 <p><a href="#rolldown">rolldown</a> <a href="#pop">pop</a></p>
1684 </blockquote>
1685 <h3 id="crosslinks-75">Crosslinks</h3>
1686 <p><a href="#pop">pop</a> <a href="#popd">popd</a> <a href="#popop">popop</a> <a href="#popopd">popopd</a> <a href="#popopdd">popopdd</a> <a href="#popopop">popopop</a></p>
1687 <hr />
1688 <h2 id="popop">popop</h2>
1689 <p>Function</p>
1690 <p><a href="#pop">pop</a> two items from the stack.</p>
1691 <pre><code>   a b popop
1692 ---------------</code></pre>
1693 <h3 id="definition-59">Definition</h3>
1694 <blockquote>
1695 <p><a href="#pop">pop</a> <a href="#pop">pop</a></p>
1696 </blockquote>
1697 <h3 id="crosslinks-76">Crosslinks</h3>
1698 <p><a href="#pop">pop</a> <a href="#popd">popd</a> <a href="#popdd">popdd</a> <a href="#popopd">popopd</a> <a href="#popopdd">popopdd</a> <a href="#popopop">popopop</a></p>
1699 <hr />
1700 <h2 id="popopd">popopd</h2>
1701 <p>Function</p>
1702 <p><a href="#pop">pop</a> the second and third items from the stack.</p>
1703 <pre><code>   a b c popopd
1704 ------------------
1705         c</code></pre>
1706 <h3 id="definition-60">Definition</h3>
1707 <blockquote>
1708 <p><a href="#rollup">rollup</a> <a href="#popop">popop</a></p>
1709 </blockquote>
1710 <h3 id="crosslinks-77">Crosslinks</h3>
1711 <p><a href="#pop">pop</a> <a href="#popd">popd</a> <a href="#popdd">popdd</a> <a href="#popop">popop</a> <a href="#popopdd">popopdd</a> <a href="#popopop">popopop</a></p>
1712 <hr />
1713 <h2 id="popopdd">popopdd</h2>
1714 <p>Function</p>
1715 <pre><code>   a b c d popopdd
1716 ---------------------
1717         c d</code></pre>
1718 <h3 id="definition-61">Definition</h3>
1719 <blockquote>
1720 <p>[<a href="#popop">popop</a>] <a href="#dipd">dipd</a></p>
1721 </blockquote>
1722 <h3 id="crosslinks-78">Crosslinks</h3>
1723 <p><a href="#pop">pop</a> <a href="#popd">popd</a> <a href="#popdd">popdd</a> <a href="#popop">popop</a> <a href="#popopd">popopd</a> <a href="#popopop">popopop</a></p>
1724 <hr />
1725 <h2 id="popopop">popopop</h2>
1726 <p>Function</p>
1727 <p><a href="#pop">pop</a> three items from the stack.</p>
1728 <pre><code>   a b c popopop
1729 -------------------</code></pre>
1730 <h3 id="definition-62">Definition</h3>
1731 <blockquote>
1732 <p><a href="#pop">pop</a> <a href="#popop">popop</a></p>
1733 </blockquote>
1734 <h3 id="crosslinks-79">Crosslinks</h3>
1735 <p><a href="#pop">pop</a> <a href="#popd">popd</a> <a href="#popdd">popdd</a> <a href="#popop">popop</a> <a href="#popopd">popopd</a> <a href="#popopdd">popopdd</a></p>
1736 <hr />
1737 <h2 id="pow">pow</h2>
1738 <p>Basis Function</p>
1739 <p>Take two numbers <code>a</code> and <code>b</code> from the stack and raise <code>a</code> to the <code>n</code>th power. (<code>b</code> is on the top of the stack.)</p>
1740 <pre><code>   a n pow
1741 -------------
1742     (a&#x207F;)</code></pre>
1743 <h3 id="example-16">Example</h3>
1744 <pre><code>   2 [2 3 4 5 6 7 8 9] [pow] map
1745 -----------------------------------
1746     2 [4 8 16 32 64 128 256 512]</code></pre>
1747 <hr />
1748 <h2 id="pred">pred</h2>
1749 <p>Function</p>
1750 <p>Predecessor. Decrement TOS.</p>
1751 <h3 id="definition-63">Definition</h3>
1752 <blockquote>
1753 <p>1 -</p>
1754 </blockquote>
1755 <h3 id="crosslinks-80">Crosslinks</h3>
1756 <p><a href="#succ">succ</a></p>
1757 <hr />
1758 <h2 id="primrec">primrec</h2>
1759 <p>Combinator</p>
1760 <p>From the <a href="https://www.kevinalbrecht.com/code/joy-mirror/j00ovr.html">&#x201C;Overview of the language JOY&#x201D;</a></p>
1761 <blockquote>
1762 <p>The primrec combinator expects two quoted programs in addition to a 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>
1763 </blockquote>
1764 <blockquote>
1765 <p>5 [1] [*] primrec</p>
1766 </blockquote>
1767 <blockquote>
1768 <p>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>
1769 </blockquote>
1770 <pre><code>   0 [Base] [Recur] primrec
1771 ------------------------------
1772       Base
1773
1774          n [Base] [Recur] primrec
1775 ------------------------------------------ n &gt; 0
1776    n (n-1) [Base] [Recur] primrec Recur</code></pre>
1777 <h3 id="discussion-53">Discussion</h3>
1778 <p>Simple and useful specialization of the <a href="#genrec">genrec</a> combinator from the <a href="https://www.kevinalbrecht.com/code/joy-mirror/index.html">original Joy system</a>.</p>
1779 <h3 id="crosslinks-81">Crosslinks</h3>
1780 <p><a href="#genrec">genrec</a> <a href="#tailrec">tailrec</a></p>
1781 <hr />
1782 <h2 id="product">product</h2>
1783 <p>Function</p>
1784 <p>Just as <a href="#sum">sum</a> sums a list of numbers, this function multiplies them together.</p>
1785 <h3 id="definition-64">Definition</h3>
1786 <blockquote>
1787 <p>1 <a href="#swap">swap</a> [<a href="#mul">mul</a>] <a href="#step">step</a></p>
1788 </blockquote>
1789 <p>Or,</p>
1790 <blockquote>
1791 <p>[1] [<a href="#mul">mul</a>] <a href="#primrec">primrec</a></p>
1792 </blockquote>
1793 <hr />
1794 <h2 id="quoted">quoted</h2>
1795 <p>Function</p>
1796 <p>&#x201C;Quote D&#x201D; Wrap the second item on the stack in a list.</p>
1797 <pre><code>   a b quoted
1798 ----------------
1799      [a] b</code></pre>
1800 <h3 id="definition-65">Definition</h3>
1801 <blockquote>
1802 <p>[<a href="#unit">unit</a>] <a href="#dip">dip</a></p>
1803 </blockquote>
1804 <h3 id="discussion-54">Discussion</h3>
1805 <p>This comes from the original Joy stuff.</p>
1806 <h3 id="crosslinks-82">Crosslinks</h3>
1807 <p><a href="#unit">unit</a></p>
1808 <hr />
1809 <h2 id="range">range</h2>
1810 <p>Function</p>
1811 <p>Expect a number <code>n</code> on the stack and replace it with a list: <code>[(n-1)...0]</code>.</p>
1812 <h3 id="example-17">Example</h3>
1813 <pre><code>     5 range
1814 -----------------
1815    [4 3 2 1 0]
1816
1817    -5 range
1818 --------------
1819       []</code></pre>
1820 <h3 id="definition-66">Definition</h3>
1821 <blockquote>
1822 <p>[0 &lt;=] [1 - <a href="#dup">dup</a>] <a href="#anamorphism">anamorphism</a></p>
1823 </blockquote>
1824 <h3 id="discussion-55">Discussion</h3>
1825 <p>If <code>n</code> is less than 1 the resulting list is empty.</p>
1826 <h3 id="crosslinks-83">Crosslinks</h3>
1827 <p><a href="#range_to_zero">range_to_zero</a></p>
1828 <hr />
1829 <h2 id="range_to_zero">range_to_zero</h2>
1830 <p>Function</p>
1831 <p>Take a number <code>n</code> from the stack and replace it with a list <code>[0...n]</code>.</p>
1832 <h3 id="example-18">Example</h3>
1833 <pre><code>   5 range_to_zero
1834 ---------------------
1835     [0 1 2 3 4 5]</code></pre>
1836 <h3 id="definition-67">Definition</h3>
1837 <blockquote>
1838 <p><a href="#unit">unit</a> [<a href="#down_to_zero">down_to_zero</a>] <a href="#infra">infra</a></p>
1839 </blockquote>
1840 <h3 id="discussion-56">Discussion</h3>
1841 <p>Note that the order is reversed compared to <a href="#range">range</a>.</p>
1842 <h3 id="crosslinks-84">Crosslinks</h3>
1843 <p><a href="#down_to_zero">down_to_zero</a> <a href="#range">range</a></p>
1844 <hr />
1845 <h2 id="reco">reco</h2>
1846 <p>Function</p>
1847 <p>Replace the first item in a list with the item under it.</p>
1848 <pre><code>   a [b ...] reco
1849 --------------------
1850      [a ...]</code></pre>
1851 <h3 id="definition-68">Definition</h3>
1852 <blockquote>
1853 <p><a href="#rest">rest</a> <a href="#cons">cons</a></p>
1854 </blockquote>
1855 <h3 id="crosslinks-85">Crosslinks</h3>
1856 <p><a href="#codireco">codireco</a> <a href="#make_generator">make_generator</a></p>
1857 <hr />
1858 <h2 id="rem">rem</h2>
1859 <p>See <a href="#mod">mod</a>.</p>
1860 <hr />
1861 <h2 id="remainder">remainder</h2>
1862 <p>See <a href="#mod">mod</a>.</p>
1863 <hr />
1864 <h2 id="remove">remove</h2>
1865 <p>Function</p>
1866 <p>Expects an item on the stack and a quote under it and removes that item from the the quote. The item is only removed once. If the list is empty or the item isn&#x2019;t in the list then the list is unchanged.</p>
1867 <pre><code>   [1 2 3 1] 1 remove
1868 ------------------------
1869         [2 3 1]</code></pre>
1870 <h3 id="definition-69">Definition</h3>
1871 <p>See the <a href="https://osdn.net/projects/joypy/scm/git/Thun/blobs/master/docs/notebooks/Remove-Function.ipynb">&#x201C;Remove Function&#x201D; notebook</a>.</p>
1872 <hr />
1873 <h2 id="rest">rest</h2>
1874 <p>Basis Function</p>
1875 <pre><code>   [a ...] rest
1876 ------------------
1877       [...]</code></pre>
1878 <h3 id="crosslinks-86">Crosslinks</h3>
1879 <p><a href="#first">first</a> <a href="#uncons">uncons</a></p>
1880 <hr />
1881 <h2 id="reverse">reverse</h2>
1882 <p>Function</p>
1883 <p>Reverse the list on the top of the stack.</p>
1884 <h3 id="example-19">Example</h3>
1885 <pre><code>   [1 2 3] reverse
1886 ---------------------
1887        [3 2 1]</code></pre>
1888 <h3 id="definition-70">Definition</h3>
1889 <blockquote>
1890 <p><a href="#section-16">&lt;{}</a> <a href="#shunt">shunt</a></p>
1891 </blockquote>
1892 <hr />
1893 <h2 id="rolldown">rolldown</h2>
1894 <p>Function</p>
1895 <pre><code>   a b c rolldown
1896 --------------------
1897        b c a</code></pre>
1898 <h3 id="definition-71">Definition</h3>
1899 <blockquote>
1900 <p><a href="#swapd">swapd</a> <a href="#swap">swap</a></p>
1901 </blockquote>
1902 <h3 id="crosslinks-87">Crosslinks</h3>
1903 <p><a href="#rollup">rollup</a></p>
1904 <hr />
1905 <h2 id="rollup">rollup</h2>
1906 <p>Function</p>
1907 <pre><code>   a b c rollup
1908 ------------------
1909       c a b</code></pre>
1910 <h3 id="definition-72">Definition</h3>
1911 <blockquote>
1912 <p><a href="#swap">swap</a> <a href="#swapd">swapd</a></p>
1913 </blockquote>
1914 <h3 id="crosslinks-88">Crosslinks</h3>
1915 <p><a href="#rolldown">rolldown</a></p>
1916 <hr />
1917 <h2 id="roll">roll&gt;</h2>
1918 <p>See <a href="#rollup">rollup</a>.</p>
1919 <hr />
1920 <h2 id="roll-1">roll&lt;</h2>
1921 <p>See <a href="#rolldown">rolldown</a>.</p>
1922 <hr />
1923 <h2 id="round">round</h2>
1924 <p>Function</p>
1925 <p>Round a number to a given precision in decimal digits.</p>
1926 <h3 id="discussion-57">Discussion</h3>
1927 <p>Another one that won&#x2019;t make sense until the &#x201C;numeric tower&#x201D; is nailed down.</p>
1928 <hr />
1929 <h2 id="rrest">rrest</h2>
1930 <p>Function</p>
1931 <pre><code>   [a b ...] rrest
1932 ---------------------
1933         [...]</code></pre>
1934 <h3 id="definition-73">Definition</h3>
1935 <blockquote>
1936 <p><a href="#rest">rest</a> <a href="#rest">rest</a></p>
1937 </blockquote>
1938 <h3 id="crosslinks-89">Crosslinks</h3>
1939 <p><a href="#rest">rest</a></p>
1940 <hr />
1941 <h2 id="rshift">rshift</h2>
1942 <p>Basis Function</p>
1943 <p><a href="https://en.wikipedia.org/wiki/Logical_shift">Logical Right-Shift</a></p>
1944 <pre><code>   a n rshift
1945 ----------------
1946      (a&#x2215;2&#x207F;)</code></pre>
1947 <h3 id="crosslinks-90">Crosslinks</h3>
1948 <p><a href="#lshift">lshift</a></p>
1949 <hr />
1950 <h2 id="run">run</h2>
1951 <p>Function</p>
1952 <p>Run a quoted program in a list.</p>
1953 <h3 id="example-20">Example</h3>
1954 <pre><code>   [1 2 +] run
1955 -----------------
1956        [3]</code></pre>
1957 <h3 id="definition-74">Definition</h3>
1958 <blockquote>
1959 <p><a href="#section-16">&lt;{}</a> <a href="#infra">infra</a></p>
1960 </blockquote>
1961 <hr />
1962 <h2 id="second">second</h2>
1963 <p>Function</p>
1964 <pre><code>   [a b ...] second
1965 ----------------------
1966           b</code></pre>
1967 <h3 id="definition-75">Definition</h3>
1968 <blockquote>
1969 <p><a href="#rest">rest</a> <a href="#first">first</a></p>
1970 </blockquote>
1971 <h3 id="crosslinks-91">Crosslinks</h3>
1972 <p><a href="#first">first</a> <a href="#third">third</a> <a href="#fourth">fourth</a></p>
1973 <hr />
1974 <h2 id="select">select</h2>
1975 <p>Basis Function</p>
1976 <p>Use a Boolean value to select one of two items from a sequence. :</p>
1977 <pre><code>   [a b] false select
1978 ------------------------
1979            a
1980
1981    [a b] true select
1982 -----------------------
1983            b</code></pre>
1984 <h3 id="discussion-58">Discussion</h3>
1985 <p>The sequence can contain more than two items but not fewer.</p>
1986 <h3 id="crosslinks-92">Crosslinks</h3>
1987 <p><a href="#choice">choice</a></p>
1988 <hr />
1989 <h2 id="sharing">sharing</h2>
1990 <p>Function</p>
1991 <p>Print redistribution information.</p>
1992 <h3 id="discussion-59">Discussion</h3>
1993 <p>Mathematically this is a form of <a href="#id">id</a>, but it has the side-effect of printing out the GPL notice.</p>
1994 <h3 id="crosslinks-93">Crosslinks</h3>
1995 <p><a href="#warranty">warranty</a></p>
1996 <hr />
1997 <h2 id="shift">shift</h2>
1998 <p>Function</p>
1999 <p>Move the top item from one list to another.</p>
2000 <h3 id="example-21">Example</h3>
2001 <pre><code>   [x y z] [a b c] shift
2002 ---------------------------
2003       [a x y z] [b c]</code></pre>
2004 <h3 id="definition-76">Definition</h3>
2005 <blockquote>
2006 <p><a href="#uncons">uncons</a> [<a href="#swons">swons</a>] <a href="#dip">dip</a></p>
2007 </blockquote>
2008 <h3 id="crosslinks-94">Crosslinks</h3>
2009 <p><a href="#shunt">shunt</a></p>
2010 <hr />
2011 <h2 id="shunt">shunt</h2>
2012 <p>Function</p>
2013 <p>Like <a href="#concat">concat</a> but <a href="#reverse">reverse</a> the top list into the second.</p>
2014 <h3 id="example-22">Example</h3>
2015 <pre><code>   [a b c] [d e f] shunt
2016 ---------------------------
2017        [f e d a b c] </code></pre>
2018 <h3 id="definition-77">Definition</h3>
2019 <blockquote>
2020 <p>[<a href="#swons">swons</a>] <a href="#step">step</a></p>
2021 </blockquote>
2022 <h3 id="discussion-60">Discussion</h3>
2023 <p>This is more efficient than <a href="#concat">concat</a> so prefer it if you don&#x2019;t need to preserve order.</p>
2024 <h3 id="crosslinks-95">Crosslinks</h3>
2025 <p><a href="#concat">concat</a> <a href="#reverse">reverse</a> <a href="#shift">shift</a></p>
2026 <hr />
2027 <h2 id="size">size</h2>
2028 <p>Function</p>
2029 <p>Replace a list with its size.</p>
2030 <h3 id="example-23">Example</h3>
2031 <pre><code>   [23 [cats] 4] size
2032 ------------------------
2033            3</code></pre>
2034 <h3 id="definition-78">Definition</h3>
2035 <blockquote>
2036 <p>[<a href="#pop">pop</a> <a href="#section-21">++</a>] <a href="#step_zero">step_zero</a></p>
2037 </blockquote>
2038 <hr />
2039 <h2 id="sort">sort</h2>
2040 <p>Function</p>
2041 <p>Given a list return it sorted.</p>
2042 <h3 id="example-24">Example</h3>
2043 <pre><code>   [4 2 5 7 1] sort
2044 ----------------------
2045       [1 2 4 5 7]</code></pre>
2046 <hr />
2047 <h2 id="spiral_next">spiral_next</h2>
2048 <p>Function</p>
2049 <p>Example code.</p>
2050 <h3 id="discussion-61">Discussion</h3>
2051 <p>See the <a href="https://joypy.osdn.io/notebooks/Square_Spiral.html">&#x201C;Square Spiral Example Joy Code&#x201D; notebook</a>.</p>
2052 <hr />
2053 <h2 id="split_at">split_at</h2>
2054 <p>Function</p>
2055 <p>Split a list (second on the stack) at the position given by the number on the top of the stack.</p>
2056 <h3 id="example-25">Example</h3>
2057 <pre><code>   [1 2 3 4 5 6 7] 4 split_at
2058 --------------------------------
2059        [5 6 7] [4 3 2 1]</code></pre>
2060 <h3 id="definition-79">Definition</h3>
2061 <blockquote>
2062 <p>[<a href="#drop">drop</a>] [<a href="#take">take</a>] <a href="#clop">clop</a></p>
2063 </blockquote>
2064 <h3 id="discussion-62">Discussion</h3>
2065 <p>Take a list and a number <code>n</code> from the stack, take <code>n</code> items from the top of the list and <a href="#shunt">shunt</a> them onto a new list that replaces the number <code>n</code> on the top of the stack.</p>
2066 <h3 id="crosslinks-96">Crosslinks</h3>
2067 <p><a href="#split_list">split_list</a></p>
2068 <hr />
2069 <h2 id="split_list">split_list</h2>
2070 <p>Function</p>
2071 <p>Split a list (second on the stack) at the position given by the number on the top of the stack such that <a href="#concat">concat</a> would reconstruct the original list.</p>
2072 <pre><code>   [1 2 3 4 5 6 7] 4 split_list
2073 ----------------------------------
2074         [1 2 3 4] [5 6 7]</code></pre>
2075 <h3 id="definition-80">Definition</h3>
2076 <blockquote>
2077 <p>[<a href="#take">take</a> <a href="#reverse">reverse</a>] [<a href="#drop">drop</a>] <a href="#clop">clop</a></p>
2078 </blockquote>
2079 <h3 id="discussion-63">Discussion</h3>
2080 <p>Compare with <a href="#split_at">split_at</a>. This function does extra work to ensure that <a href="#concat">concat</a> would reconstruct the original list.</p>
2081 <h3 id="crosslinks-97">Crosslinks</h3>
2082 <p><a href="#split_at">split_at</a></p>
2083 <hr />
2084 <h2 id="sqr">sqr</h2>
2085 <p>Function</p>
2086 <p>Square the number on the top of the stack.</p>
2087 <pre><code>   n  sqr
2088 ------------
2089      n&#xB2;</code></pre>
2090 <h3 id="definition-81">Definition</h3>
2091 <blockquote>
2092 <p><a href="#dup">dup</a> <a href="#mul">mul</a></p>
2093 </blockquote>
2094 <hr />
2095 <h2 id="sqrt">sqrt</h2>
2096 <p>Basis Function Combinator</p>
2097 <p>Return the square root of the number a. Negative numbers return complex roots.</p>
2098 <h3 id="discussion-64">Discussion</h3>
2099 <p>Another &#x201C;numeric tower&#x201D; hatch&#x2026;</p>
2100 <hr />
2101 <h2 id="stack">stack</h2>
2102 <p>Function</p>
2103 <p>Put the stack onto the stack.</p>
2104 <pre><code>      ... c b a stack
2105 ---------------------------
2106    ... c b a [a b c ...]</code></pre>
2107 <h3 id="definition-82">Definition</h3>
2108 <blockquote>
2109 <p>[] <a href="#swaack">swaack</a> <a href="#dup">dup</a> <a href="#swaack">swaack</a> <a href="#first">first</a></p>
2110 </blockquote>
2111 <h3 id="discussion-65">Discussion</h3>
2112 <p>This function forms a pair with [unstack], and together they form the complement to the &#x201C;destructive&#x201D; pair <a href="#enstacken">enstacken</a> and <a href="#disenstacken">disenstacken</a>.</p>
2113 <h3 id="crosslinks-98">Crosslinks</h3>
2114 <p>[unstack] <a href="#enstacken">enstacken</a> <a href="#disenstacken">disenstacken</a></p>
2115 <hr />
2116 <h2 id="stackd">stackd</h2>
2117 <p>Function</p>
2118 <p>Grab the stack under the top item and put it onto the stack.</p>
2119 <h3 id="example-26">Example</h3>
2120 <pre><code>   ... 1 2 3 stackd
2121 ------------------------
2122   ... 1 2 [2 1 ...] 3</code></pre>
2123 <h3 id="definition-83">Definition</h3>
2124 <blockquote>
2125 <p>[<a href="#stack">stack</a>] <a href="#dip">dip</a></p>
2126 </blockquote>
2127 <hr />
2128 <h2 id="step">step</h2>
2129 <p>Combinator</p>
2130 <p>Run a quoted program on each item in a sequence.</p>
2131 <pre><code>   ... [] [Q] step
2132 ---------------------
2133          ...
2134
2135
2136    ... [a] [Q] step
2137 ----------------------
2138       ... a Q
2139
2140
2141    ... [a b c] [Q] . step
2142 ----------------------------------------
2143              ... a . Q [b c] [Q] step</code></pre>
2144 <h3 id="discussion-66">Discussion</h3>
2145 <p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
2146 <h3 id="crosslinks-99">Crosslinks</h3>
2147 <p><a href="#step_zero">step_zero</a></p>
2148 <hr />
2149 <h2 id="step_zero">step_zero</h2>
2150 <p>Combinator</p>
2151 <p>Like <a href="#step">step</a> but with 0 as the initial value.</p>
2152 <pre><code>   [...] [F] step_zero
2153 -------------------------
2154      0 [...] [F] step</code></pre>
2155 <h3 id="definition-84">Definition</h3>
2156 <blockquote>
2157 <p>0 <a href="#roll">roll&gt;</a> <a href="#step">step</a></p>
2158 </blockquote>
2159 <h3 id="discussion-67">Discussion</h3>
2160 <p><a href="#size">size</a> and <a href="#sum">sum</a> can both be defined in terms of this specialization of <a href="#step">step</a>.</p>
2161 <h3 id="crosslinks-100">Crosslinks</h3>
2162 <p><a href="#step">step</a></p>
2163 <hr />
2164 <h2 id="stuncons">stuncons</h2>
2165 <p>Function</p>
2166 <p>Take the <a href="#stack">stack</a> and <a href="#uncons">uncons</a> the top item.</p>
2167 <h3 id="example-27">Example</h3>
2168 <pre><code>   1 2 3 stuncons
2169 --------------------
2170    1 2 3 3 [2 1]</code></pre>
2171 <h3 id="definition-85">Definition</h3>
2172 <blockquote>
2173 <p><a href="#stack">stack</a> <a href="#uncons">uncons</a></p>
2174 </blockquote>
2175 <hr />
2176 <h2 id="stununcons">stununcons</h2>
2177 <p>Function</p>
2178 <p>Take the <a href="#stack">stack</a> and <a href="#uncons">uncons</a> the top two items.</p>
2179 <h3 id="example-28">Example</h3>
2180 <pre><code>   1 2 3 stununcons
2181 ----------------------
2182     1 2 3 3 2 [1]</code></pre>
2183 <h3 id="definition-86">Definition</h3>
2184 <blockquote>
2185 <p><a href="#stack">stack</a> <a href="#uncons">uncons</a> <a href="#uncons">uncons</a></p>
2186 </blockquote>
2187 <h3 id="crosslinks-101">Crosslinks</h3>
2188 <p><a href="#stuncons">stuncons</a></p>
2189 <hr />
2190 <h2 id="sub">sub</h2>
2191 <p>Basis Function</p>
2192 <p>Subtract the number on the top of the stack from the number below it.</p>
2193 <pre><code>   a b sub
2194 -------------
2195     (a-b)</code></pre>
2196 <h3 id="crosslinks-102">Crosslinks</h3>
2197 <p><a href="#add">add</a></p>
2198 <hr />
2199 <h2 id="succ">succ</h2>
2200 <p>Function</p>
2201 <p>Successor. Increment TOS.</p>
2202 <h3 id="definition-87">Definition</h3>
2203 <blockquote>
2204 <p>1 +</p>
2205 </blockquote>
2206 <h3 id="crosslinks-103">Crosslinks</h3>
2207 <p><a href="#pred">pred</a></p>
2208 <hr />
2209 <h2 id="sum">sum</h2>
2210 <p>Combinator</p>
2211 <p>Given a quoted sequence of numbers return the sum.</p>
2212 <h3 id="example-29">Example</h3>
2213 <pre><code>   [1 2 3 4 5] sum
2214 ---------------------
2215          15</code></pre>
2216 <h3 id="definition-88">Definition</h3>
2217 <blockquote>
2218 <p>[+] <a href="#step_zero">step_zero</a></p>
2219 </blockquote>
2220 <h3 id="crosslinks-104">Crosslinks</h3>
2221 <p><a href="#size">size</a></p>
2222 <hr />
2223 <h2 id="swaack">swaack</h2>
2224 <p>Basis Function</p>
2225 <p>Swap stack. Take a list from the top of the stack, replace the stack with the list, and put the old stack onto it.</p>
2226 <h3 id="example-30">Example</h3>
2227 <pre><code>   1 2 3 [4 5 6] swaack
2228 --------------------------
2229    6 5 4 [3 2 1]</code></pre>
2230 <h3 id="discussion-68">Discussion</h3>
2231 <p>This function works as a kind of &#x201C;context switch&#x201D;. It&#x2019;s used in the definition of <a href="#infra">infra</a>.</p>
2232 <h3 id="crosslinks-105">Crosslinks</h3>
2233 <p><a href="#infra">infra</a></p>
2234 <hr />
2235 <h2 id="swap">swap</h2>
2236 <p>Basis Function</p>
2237 <p>Swap the top two items on the stack.</p>
2238 <pre><code>   a b swap
2239 --------------
2240      b a</code></pre>
2241 <h3 id="crosslinks-106">Crosslinks</h3>
2242 <p><a href="#swapd">swapd</a></p>
2243 <hr />
2244 <h2 id="swapd">swapd</h2>
2245 <p>Function</p>
2246 <p>Swap the second and third items on the stack.</p>
2247 <pre><code>   a b c swapd
2248 -----------------
2249       b a c</code></pre>
2250 <h3 id="definition-89">Definition</h3>
2251 <blockquote>
2252 <p>[<a href="#swap">swap</a>] <a href="#dip">dip</a></p>
2253 </blockquote>
2254 <h3 id="crosslinks-107">Crosslinks</h3>
2255 <p><a href="#over">over</a> <a href="#tuck">tuck</a></p>
2256 <hr />
2257 <h2 id="swoncat">swoncat</h2>
2258 <p>Function</p>
2259 <p><a href="#concat">concat</a> two lists, but <a href="#swap">swap</a> the lists first.</p>
2260 <h3 id="definition-90">Definition</h3>
2261 <blockquote>
2262 <p><a href="#swap">swap</a> <a href="#concat">concat</a></p>
2263 </blockquote>
2264 <h3 id="crosslinks-108">Crosslinks</h3>
2265 <p><a href="#concat">concat</a></p>
2266 <hr />
2267 <h2 id="swons">swons</h2>
2268 <p>Function</p>
2269 <p>Like <a href="#cons">cons</a> but <a href="#swap">swap</a> the item and list.</p>
2270 <pre><code>   [...] a swons
2271 -------------------
2272       [a ...]</code></pre>
2273 <h3 id="definition-91">Definition</h3>
2274 <blockquote>
2275 <p><a href="#swap">swap</a> <a href="#cons">cons</a></p>
2276 </blockquote>
2277 <hr />
2278 <h2 id="tailrec">tailrec</h2>
2279 <p>Combinator</p>
2280 <p>A specialization of the <a href="#genrec">genrec</a> combinator.</p>
2281 <h3 id="definition-92">Definition</h3>
2282 <blockquote>
2283 <p>[<a href="#i">i</a>] <a href="#genrec">genrec</a></p>
2284 </blockquote>
2285 <h3 id="discussion-69">Discussion</h3>
2286 <p>Some recursive functions do not need to store additional data or pending actions per-call. These are called <a href="https://en.wikipedia.org/wiki/Tail_recursive">&#x201C;tail recursive&#x201D; functions</a>. In Joy, they appear as <a href="#genrec">genrec</a> definitions that have <a href="#i">i</a> for the second half of their recursive branch.</p>
2287 <p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
2288 <h3 id="crosslinks-109">Crosslinks</h3>
2289 <p><a href="#genrec">genrec</a></p>
2290 <hr />
2291 <h2 id="take">take</h2>
2292 <p>Function</p>
2293 <p>Expects an integer <code>n</code> and a list on the stack and replace them with a list with just the top <code>n</code> items in reverse order.</p>
2294 <pre><code>   [a b c d] 2 take
2295 ----------------------
2296         [b a]</code></pre>
2297 <h3 id="definition-93">Definition</h3>
2298 <blockquote>
2299 <p><a href="#section-18">&lt;&lt;{}</a> [<a href="#shift">shift</a>] <a href="#times">times</a> <a href="#pop">pop</a></p>
2300 </blockquote>
2301 <hr />
2302 <h2 id="ternary">ternary</h2>
2303 <p>Combinator</p>
2304 <p>Run a quoted program using exactly three stack values and leave the first item of the result on the stack.</p>
2305 <pre><code>   ... z y x [P] ternary
2306 -------------------------
2307          ... a</code></pre>
2308 <h3 id="definition-94">Definition</h3>
2309 <blockquote>
2310 <p><a href="#binary">binary</a> <a href="#popd">popd</a></p>
2311 </blockquote>
2312 <h3 id="discussion-70">Discussion</h3>
2313 <p>Runs any other quoted function and returns its first result while consuming exactly three items from the stack.</p>
2314 <h3 id="crosslinks-110">Crosslinks</h3>
2315 <p><a href="#binary">binary</a> <a href="#nullary">nullary</a> <a href="#unary">unary</a></p>
2316 <hr />
2317 <h2 id="third">third</h2>
2318 <p>Function</p>
2319 <pre><code>   [a b c ...] third
2320 -----------------------
2321            c</code></pre>
2322 <h3 id="definition-95">Definition</h3>
2323 <blockquote>
2324 <p><a href="#rest">rest</a> <a href="#second">second</a></p>
2325 </blockquote>
2326 <h3 id="crosslinks-111">Crosslinks</h3>
2327 <p><a href="#first">first</a> <a href="#second">second</a> <a href="#fourth">fourth</a> <a href="#rest">rest</a></p>
2328 <hr />
2329 <h2 id="times">times</h2>
2330 <p>Combinator</p>
2331 <p>Expect a quoted program and an integer <code>n</code> on the stack and do the program <code>n</code> times.</p>
2332 <pre><code>   ... n [Q] . times
2333 -----------------------  w/ n &lt;= 0
2334          ... .
2335
2336    ... 1 [Q] . times
2337 -----------------------
2338          ... . Q
2339
2340    ... n [Q] . times
2341 -------------------------------------  w/ n &gt; 1
2342          ... . Q (n-1) [Q] times</code></pre>
2343 <h3 id="definition-96">Definition</h3>
2344 <blockquote>
2345 <p>[-- dip] cons [swap] infra [0 &gt;] swap while pop :</p>
2346 </blockquote>
2347 <h3 id="discussion-71">Discussion</h3>
2348 <p>This works by building a little <a href="#while">while</a> program and running it:</p>
2349 <pre><code>                 1 3 [++] &#x2022; [-- dip] cons [swap] infra [0 &gt;] swap while pop                                                                                                                 
2350         1 3 [++] [-- dip] &#x2022; cons [swap] infra [0 &gt;] swap while pop                                                                                                                          
2351         1 3 [[++] -- dip] &#x2022; [swap] infra [0 &gt;] swap while pop                                                                                                                               
2352  1 3 [[++] -- dip] [swap] &#x2022; infra [0 &gt;] swap while pop                                                                                                                                      
2353               dip -- [++] &#x2022; swap [3 1] swaack [0 &gt;] swap while pop                                                                                                                          
2354               dip [++] -- &#x2022; [3 1] swaack [0 &gt;] swap while pop                                                                                                                               
2355         dip [++] -- [3 1] &#x2022; swaack [0 &gt;] swap while pop                                                                                                                                     
2356         1 3 [-- [++] dip] &#x2022; [0 &gt;] swap while pop                                                                                                                                            
2357   1 3 [-- [++] dip] [0 &gt;] &#x2022; swap while pop                                                                                                                                                  
2358   1 3 [0 &gt;] [-- [++] dip] &#x2022; while pop                                                                                                                                                       </code></pre>
2359 <p>This is a common pattern in Joy. You accept some parameters from the stack which typically include qouted programs and use them to build another program which does the actual work. This is kind of like macros in Lisp, or preprocessor directives in C.</p>
2360 <hr />
2361 <h2 id="truthy">truthy</h2>
2362 <p>See <a href="#bool">bool</a>.</p>
2363 <hr />
2364 <h2 id="tuck">tuck</h2>
2365 <p>Function</p>
2366 <p><a href="#dup">dup</a> the item on the top of the stack under the second item on the stack.</p>
2367 <pre><code>   a b tuck
2368 --------------
2369     b a b</code></pre>
2370 <h3 id="definition-97">Definition</h3>
2371 <blockquote>
2372 <p><a href="#dup">dup</a> [<a href="#swap">swap</a>] <a href="#dip">dip</a></p>
2373 </blockquote>
2374 <h3 id="crosslinks-112">Crosslinks</h3>
2375 <p><a href="#over">over</a></p>
2376 <hr />
2377 <h2 id="unary">unary</h2>
2378 <p>(Combinator)</p>
2379 <p>Run a quoted program using exactly one stack value and leave the first item of the result on the stack.</p>
2380 <pre><code>   ... x [P] unary
2381 ---------------------
2382        ... a</code></pre>
2383 <h3 id="definition-98">Definition</h3>
2384 <blockquote>
2385 <p><a href="#nullary">nullary</a> <a href="#popd">popd</a></p>
2386 </blockquote>
2387 <h3 id="discussion-72">Discussion</h3>
2388 <p>Runs any other quoted function and returns its first result while consuming exactly one item from the stack.</p>
2389 <h3 id="crosslinks-113">Crosslinks</h3>
2390 <p><a href="#binary">binary</a> <a href="#nullary">nullary</a> <a href="#ternary">ternary</a></p>
2391 <hr />
2392 <h2 id="uncons">uncons</h2>
2393 <p>Basis Function</p>
2394 <p>Removes an item from a list and leaves it on the stack under the rest of the list. You cannot <code>uncons</code> an item from an empty list.</p>
2395 <pre><code>   [a ...] uncons
2396 --------------------
2397       a [...]</code></pre>
2398 <h3 id="discussion-73">Discussion</h3>
2399 <p>This is the inverse of <a href="#cons">cons</a>.</p>
2400 <h3 id="crosslinks-114">Crosslinks</h3>
2401 <p><a href="#cons">cons</a></p>
2402 <hr />
2403 <h2 id="unique">unique</h2>
2404 <p>Function</p>
2405 <p>Given a list remove duplicate items.</p>
2406 <hr />
2407 <h2 id="unit">unit</h2>
2408 <p>Function</p>
2409 <pre><code>   a unit
2410 ------------
2411     [a]</code></pre>
2412 <h3 id="definition-99">Definition</h3>
2413 <blockquote>
2414 <p>[] <a href="#cons">cons</a></p>
2415 </blockquote>
2416 <hr />
2417 <h2 id="unquoted">unquoted</h2>
2418 <p>Combinator</p>
2419 <p>Unquote (using <a href="#i">i</a>) the list that is second on the stack.</p>
2420 <h3 id="example-31">Example</h3>
2421 <pre><code>   1 2 [3 4] 5 unquoted
2422 --------------------------
2423          1 2 3 4 5</code></pre>
2424 <h3 id="definition-100">Definition</h3>
2425 <blockquote>
2426 <p>[<a href="#i">i</a>] <a href="#dip">dip</a></p>
2427 </blockquote>
2428 <h3 id="crosslinks-115">Crosslinks</h3>
2429 <p><a href="#unit">unit</a></p>
2430 <hr />
2431 <h2 id="unswons">unswons</h2>
2432 <p>Function</p>
2433 <pre><code>   [a ...] unswons
2434 ---------------------
2435        [...] a</code></pre>
2436 <h3 id="definition-101">Definition</h3>
2437 <blockquote>
2438 <p><a href="#uncons">uncons</a> <a href="#swap">swap</a></p>
2439 </blockquote>
2440 <hr />
2441 <h2 id="void">void</h2>
2442 <p>Basis Function</p>
2443 <p>True if the form on TOS is void otherwise False.</p>
2444 <h3 id="discussion-74">Discussion</h3>
2445 <p>A form is any Joy expression composed solely of lists. This represents a binary Boolean logical formula in the arithmetic of the &#x201C;Laws of Form&#x201D;, see <a href="http://www.markability.net/">The Markable Mark</a></p>
2446 <hr />
2447 <h2 id="warranty">warranty</h2>
2448 <p>Basis Function</p>
2449 <p>Print warranty information.</p>
2450 <hr />
2451 <h2 id="while">while</h2>
2452 <p>Combinator</p>
2453 <p>A specialization of <a href="#loop">loop</a> that accepts a quoted predicate program <code>P</code> and runs it <a href="#nullary">nullary</a>.</p>
2454 <pre><code>   [P] [F] while
2455 ------------------- P -&gt; false
2456
2457     [P] [F] while
2458 --------------------- P -&gt; true
2459    F [P] [F] while</code></pre>
2460 <h3 id="definition-102">Definition</h3>
2461 <blockquote>
2462 <p><a href="#swap">swap</a> <a href="#nulco">nulco</a> <a href="#dupdipd">dupdipd</a> <a href="#concat">concat</a> <a href="#loop">loop</a></p>
2463 </blockquote>
2464 <h3 id="crosslinks-116">Crosslinks</h3>
2465 <p><a href="#loop">loop</a></p>
2466 <hr />
2467 <h2 id="words">words</h2>
2468 <p>Basis Function</p>
2469 <p>Print all the words in alphabetical order.</p>
2470 <h3 id="discussion-75">Discussion</h3>
2471 <p>Mathematically this is a form of <a href="#id">id</a>.</p>
2472 <h3 id="crosslinks-117">Crosslinks</h3>
2473 <p><a href="#help">help</a></p>
2474 <hr />
2475 <h2 id="x">x</h2>
2476 <p>Combinator</p>
2477 <p>Take a quoted function <code>F</code> and run it with itself as the first item on the stack.</p>
2478 <pre><code>   [F] x
2479 -----------
2480    [F] F</code></pre>
2481 <h3 id="definition-103">Definition</h3>
2482 <pre><code>dup i</code></pre>
2483 <h3 id="discussion-76">Discussion</h3>
2484 <p>The simplest recursive pattern.</p>
2485 <p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>. as well as <a href="https://www.kevinalbrecht.com/code/joy-mirror/j05cmp.html">Recursion Theory and Joy</a> by Manfred von</p>
2486 <hr />
2487 <h2 id="xor">xor</h2>
2488 <p>Basis Function</p>
2489 <p>Logical bit-wise eXclusive OR.</p>
2490 <h3 id="crosslinks-118">Crosslinks</h3>
2491 <p><a href="#and">and</a> <a href="#or">or</a></p>
2492 <hr />
2493 <h2 id="zip">zip</h2>
2494 <p>Function</p>
2495 <p>Replace the two lists on the top of the stack with a list of the pairs from each list. The smallest list sets the length of the result list.</p>
2496 <h3 id="example-32">Example</h3>
2497 <pre><code>   [1 2 3] [4 5 6] zip
2498 -------------------------
2499    [[4 1] [5 2] [6 3]]</code></pre>
2500 </body>
2501 </html>