1840 lines
79 KiB
HTML
1840 lines
79 KiB
HTML
<!DOCTYPE html>
|
|
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
|
|
<head>
|
|
<meta charset="utf-8" />
|
|
<meta name="generator" content="pandoc" />
|
|
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
|
|
<title>Thun Function Reference</title>
|
|
<style>
|
|
code{white-space: pre-wrap;}
|
|
span.smallcaps{font-variant: small-caps;}
|
|
span.underline{text-decoration: underline;}
|
|
div.column{display: inline-block; vertical-align: top; width: 50%;}
|
|
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
|
|
ul.task-list{list-style: none;}
|
|
</style>
|
|
<link rel="stylesheet" href="/css/fonts.css" />
|
|
<link rel="stylesheet" href="/css/func_ref.css" />
|
|
</head>
|
|
<body>
|
|
<header id="title-block-header">
|
|
<h1 class="title">Thun Function Reference</h1>
|
|
</header>
|
|
<nav id="TOC" role="doc-toc">
|
|
<ul>
|
|
<li><a href="#section">/<br />
|
|
</a></li>
|
|
<li><a href="#section-1">/</a></li>
|
|
<li><a href="#abs">abs</a></li>
|
|
<li><a href="#add">add</a></li>
|
|
<li><a href="#and">and</a></li>
|
|
<li><a href="#anamorphism">anamorphism</a></li>
|
|
<li><a href="#app1">app1</a></li>
|
|
<li><a href="#app2">app2</a></li>
|
|
<li><a href="#app3">app3</a></li>
|
|
<li><a href="#appn">appN</a></li>
|
|
<li><a href="#section-2">*</a></li>
|
|
<li><a href="#at">at</a></li>
|
|
<li><a href="#average">average</a></li>
|
|
<li><a href="#binary">binary</a></li>
|
|
<li><a href="#b">b</a></li>
|
|
<li><a href="#bool">bool</a></li>
|
|
<li><a href="#branch">branch</a></li>
|
|
<li><a href="#ccccons">ccccons</a></li>
|
|
<li><a href="#ccons">ccons</a></li>
|
|
<li><a href="#choice">choice</a></li>
|
|
<li><a href="#clear">clear</a></li>
|
|
<li><a href="#cleave">cleave</a></li>
|
|
<li><a href="#clop">clop</a></li>
|
|
<li><a href="#cmp">cmp</a></li>
|
|
<li><a href="#codi">codi</a></li>
|
|
<li><a href="#codireco">codireco</a></li>
|
|
<li><a href="#concat">concat</a></li>
|
|
<li><a href="#cond">cond</a></li>
|
|
<li><a href="#cons">cons</a></li>
|
|
<li><a href="#dinfrirst">dinfrirst</a></li>
|
|
<li><a href="#dipddd">dipddd</a></li>
|
|
<li><a href="#dipdd">dipdd</a></li>
|
|
<li><a href="#dipd">dipd</a></li>
|
|
<li><a href="#dip">dip</a></li>
|
|
<li><a href="#disenstacken">disenstacken</a></li>
|
|
<li><a href="#div">div</a></li>
|
|
<li><a href="#divmod">divmod</a></li>
|
|
<li><a href="#down_to_zero">down_to_zero</a></li>
|
|
<li><a href="#drop">drop</a></li>
|
|
<li><a href="#dupdd">dupdd</a></li>
|
|
<li><a href="#dupdipd">dupdipd</a></li>
|
|
<li><a href="#dupdip">dupdip</a></li>
|
|
<li><a href="#dupd">dupd</a></li>
|
|
<li><a href="#dup">dup</a></li>
|
|
<li><a href="#enstacken">enstacken</a></li>
|
|
<li><a href="#eq">eq</a></li>
|
|
<li><a href="#section-3">=</a></li>
|
|
<li><a href="#section-4">!=</a></li>
|
|
<li><a href="#section-5">!-</a></li>
|
|
<li><a href="#first">first</a></li>
|
|
<li><a href="#first_two">first_two</a></li>
|
|
<li><a href="#flatten">flatten</a></li>
|
|
<li><a href="#fork">fork</a></li>
|
|
<li><a href="#fourth">fourth</a></li>
|
|
<li><a href="#gcd">gcd</a></li>
|
|
<li><a href="#ge">ge</a></li>
|
|
<li><a href="#genrec">genrec</a></li>
|
|
<li><a href="#getitem">getitem</a></li>
|
|
<li><a href="#grabn">grabN</a></li>
|
|
<li><a href="#grba">grba</a></li>
|
|
<li><a href="#section-6">>=</a></li>
|
|
<li><a href="#section-7">>></a></li>
|
|
<li><a href="#section-8">></a></li>
|
|
<li><a href="#gt">gt</a></li>
|
|
<li><a href="#help">help</a></li>
|
|
<li><a href="#section-9">–</a></li>
|
|
<li><a href="#section-10">-</a></li>
|
|
<li><a href="#id">id</a></li>
|
|
<li><a href="#ifte">ifte</a></li>
|
|
<li><a href="#ii">ii</a></li>
|
|
<li><a href="#i">i</a></li>
|
|
<li><a href="#infra">infra</a></li>
|
|
<li><a href="#infrst">infrst</a></li>
|
|
<li><a href="#inscribe">inscribe</a></li>
|
|
<li><a href="#le">le</a></li>
|
|
<li><a href="#section-11"><=</a></li>
|
|
<li><a href="#section-12"><></a></li>
|
|
<li><a href="#section-13"><</a></li>
|
|
<li><a href="#section-14"><<</a></li>
|
|
<li><a href="#section-15"><<</a></li>
|
|
<li><a href="#section-16"><</a></li>
|
|
<li><a href="#loop">loop</a></li>
|
|
<li><a href="#lshift">lshift</a></li>
|
|
<li><a href="#lt">lt</a></li>
|
|
<li><a href="#make_generator">make_generator</a></li>
|
|
<li><a href="#map">map</a></li>
|
|
<li><a href="#max">max</a></li>
|
|
<li><a href="#min">min</a></li>
|
|
<li><a href="#mod">mod</a></li>
|
|
<li><a href="#modulus">modulus</a></li>
|
|
<li><a href="#mul">mul</a></li>
|
|
<li><a href="#neg">neg</a></li>
|
|
<li><a href="#neq">neq</a></li>
|
|
<li><a href="#not">not</a></li>
|
|
<li><a href="#nulco">nulco</a></li>
|
|
<li><a href="#null">null</a></li>
|
|
<li><a href="#nullary">nullary</a></li>
|
|
<li><a href="#of">of</a></li>
|
|
<li><a href="#or">or</a></li>
|
|
<li><a href="#over">over</a></li>
|
|
<li><a href="#pam">pam</a></li>
|
|
<li><a href="#section-17">%</a></li>
|
|
<li><a href="#pick">pick</a></li>
|
|
<li><a href="#section-18">+</a></li>
|
|
<li><a href="#section-19">++</a></li>
|
|
<li><a href="#pm">pm</a></li>
|
|
<li><a href="#popdd">popdd</a></li>
|
|
<li><a href="#popd">popd</a></li>
|
|
<li><a href="#pop">pop</a></li>
|
|
<li><a href="#popopdd">popopdd</a></li>
|
|
<li><a href="#popopd">popopd</a></li>
|
|
<li><a href="#popop">popop</a></li>
|
|
<li><a href="#popopop">popopop</a></li>
|
|
<li><a href="#pow">pow</a></li>
|
|
<li><a href="#pred">pred</a></li>
|
|
<li><a href="#primrec">primrec</a></li>
|
|
<li><a href="#product">product</a></li>
|
|
<li><a href="#section-20">?</a></li>
|
|
<li><a href="#quoted">quoted</a></li>
|
|
<li><a href="#range">range</a></li>
|
|
<li><a href="#range_to_zero">range_to_zero</a></li>
|
|
<li><a href="#reco">reco</a></li>
|
|
<li><a href="#remainder">remainder</a></li>
|
|
<li><a href="#rem">rem</a></li>
|
|
<li><a href="#remove">remove</a></li>
|
|
<li><a href="#rest">rest</a></li>
|
|
<li><a href="#reverse">reverse</a></li>
|
|
<li><a href="#rolldown">rolldown</a></li>
|
|
<li><a href="#roll">roll></a></li>
|
|
<li><a href="#roll-1">roll<</a></li>
|
|
<li><a href="#rollup">rollup</a></li>
|
|
<li><a href="#rrest">rrest</a></li>
|
|
<li><a href="#rshift">rshift</a></li>
|
|
<li><a href="#run">run</a></li>
|
|
<li><a href="#second">second</a></li>
|
|
<li><a href="#select">select</a></li>
|
|
<li><a href="#sharing">sharing</a></li>
|
|
<li><a href="#shift">shift</a></li>
|
|
<li><a href="#shunt">shunt</a></li>
|
|
<li><a href="#size">size</a></li>
|
|
<li><a href="#section-21">/</a></li>
|
|
<li><a href="#small">small</a></li>
|
|
<li><a href="#sort">sort</a></li>
|
|
<li><a href="#spiral_next">spiral_next</a></li>
|
|
<li><a href="#split_at">split_at</a></li>
|
|
<li><a href="#split_list">split_list</a></li>
|
|
<li><a href="#sqr">sqr</a></li>
|
|
<li><a href="#stackd">stackd</a></li>
|
|
<li><a href="#stack">stack</a></li>
|
|
<li><a href="#step">step</a></li>
|
|
<li><a href="#step_zero">step_zero</a></li>
|
|
<li><a href="#stuncons">stuncons</a></li>
|
|
<li><a href="#stununcons">stununcons</a></li>
|
|
<li><a href="#sub">sub</a></li>
|
|
<li><a href="#succ">succ</a></li>
|
|
<li><a href="#sum">sum</a></li>
|
|
<li><a href="#swaack">swaack</a></li>
|
|
<li><a href="#swapd">swapd</a></li>
|
|
<li><a href="#swap">swap</a></li>
|
|
<li><a href="#swoncat">swoncat</a></li>
|
|
<li><a href="#swons">swons</a></li>
|
|
<li><a href="#tailrec">tailrec</a></li>
|
|
<li><a href="#take">take</a></li>
|
|
<li><a href="#ternary">ternary</a></li>
|
|
<li><a href="#third">third</a></li>
|
|
<li><a href="#times">times</a></li>
|
|
<li><a href="#tuck">tuck</a></li>
|
|
<li><a href="#unary">unary</a></li>
|
|
<li><a href="#uncons">uncons</a></li>
|
|
<li><a href="#unique">unique</a></li>
|
|
<li><a href="#unit">unit</a></li>
|
|
<li><a href="#unstack">unstack</a></li>
|
|
<li><a href="#unquoted">unquoted</a></li>
|
|
<li><a href="#unswons">unswons</a></li>
|
|
<li><a href="#section-22">||</a></li>
|
|
<li><a href="#void">void</a></li>
|
|
<li><a href="#warranty">warranty</a></li>
|
|
<li><a href="#while">while</a></li>
|
|
<li><a href="#words">words</a></li>
|
|
<li><a href="#x">x</a></li>
|
|
<li><a href="#zip">zip</a></li>
|
|
</ul>
|
|
</nav>
|
|
<p><a href="/">Home</a></p>
|
|
<p>Version -10.0.0</p>
|
|
<p>Each function, combinator, or definition should be documented here.</p>
|
|
<hr />
|
|
<h2 id="section">/<br />
|
|
</h2>
|
|
<p>Binary Boolean <em>and</em>.</p>
|
|
<h3 id="crosslinks">Crosslinks</h3>
|
|
<p><a href="#bool">bool</a> <a href="#not">not</a> <a href="#section-1">/</a></p>
|
|
<hr />
|
|
<h2 id="section-1">/</h2>
|
|
<p>Binary Boolean <em>or</em>.</p>
|
|
<h3 id="crosslinks-1">Crosslinks</h3>
|
|
<p><a href="#bool">bool</a> <a href="#not">not</a> [/]</p>
|
|
<hr />
|
|
<h2 id="abs">abs</h2>
|
|
<p>Take an integer from the stack and replace it with its absolute value.</p>
|
|
<hr />
|
|
<h2 id="add">add</h2>
|
|
<p>Take two integers from the stack and replace them with their sum.</p>
|
|
<hr />
|
|
<h2 id="and">and</h2>
|
|
<p>Combinator</p>
|
|
<p>Short-circuiting Boolean AND</p>
|
|
<p>Accept two quoted programs, run the first and expect a Boolean value, if it’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.) The quoted programs are run with <a href="#nullary">nullary</a>.</p>
|
|
<pre><code> [A] [B] and
|
|
----------------- A -> true
|
|
B
|
|
|
|
|
|
[A] [B] and
|
|
----------------- A -> false
|
|
false</code></pre>
|
|
<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>
|
|
<h3 id="crosslinks-2">Crosslinks</h3>
|
|
<p><a href="#or">or</a></p>
|
|
<hr />
|
|
<h2 id="anamorphism">anamorphism</h2>
|
|
<p>Combinator</p>
|
|
<p>Build a list of values from a generator program <code>G</code> and a stopping predicate <code>P</code>.</p>
|
|
<pre><code> [P] [G] anamorphism
|
|
-----------------------------------------
|
|
[P] [pop []] [G] [dip swons] genrec</code></pre>
|
|
<h3 id="example">Example</h3>
|
|
<p>The <code>range</code> function generates a list of the integers from 0 to n - 1:</p>
|
|
<pre><code>[0 <=] [-- dup] anamorphism</code></pre>
|
|
<blockquote>
|
|
<p>joy? 5</p>
|
|
<p>5</p>
|
|
<p>joy? [0 <=] [– dup]</p>
|
|
<p>5 [0 <=] [– dup]</p>
|
|
<p>joy? anamorphism</p>
|
|
<p>[4 3 2 1 0]</p>
|
|
</blockquote>
|
|
<p>Note that the last value generated (0) is at the bottom of the list. See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
|
|
<hr />
|
|
<h2 id="app1">app1</h2>
|
|
<p>“apply one”</p>
|
|
<p>Combinator</p>
|
|
<p>Given a quoted program on TOS and anything as the second stack item run the program without disturbing the rest of the stack and replace the two args with the first result of the program.</p>
|
|
<pre><code> ... x [Q] app1
|
|
---------------------------------
|
|
... [x ...] [Q] infra first</code></pre>
|
|
<p>This is the same effect as the <a href="#unary">unary</a> combinator.</p>
|
|
<h3 id="discussion">Discussion</h3>
|
|
<p>Just a specialization of <code>nullary</code> really. Its parallelizable cousins are more useful.</p>
|
|
<h3 id="crosslinks-3">Crosslinks</h3>
|
|
<p><a href="#app2">app2</a> <a href="#app3">app3</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="app2">app2</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#app1">app1</a> with two items.</p>
|
|
<pre><code> ... y x [Q] . app2
|
|
-----------------------------------
|
|
... [y ...] [Q] . infra first
|
|
[x ...] [Q] infra first</code></pre>
|
|
<h3 id="discussion-1">Discussion</h3>
|
|
<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>
|
|
<p>This is not currently implemented as parallel processes but it can (and should) be done.</p>
|
|
<h3 id="crosslinks-4">Crosslinks</h3>
|
|
<p><a href="#app1">app1</a> <a href="#app3">app3</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="app3">app3</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#app1">app1</a> with three items.</p>
|
|
<pre><code> ... z y x [Q] . app3
|
|
-----------------------------------
|
|
... [z ...] [Q] . infra first
|
|
[y ...] [Q] infra first
|
|
[x ...] [Q] infra first</code></pre>
|
|
<h3 id="discussion-2">Discussion</h3>
|
|
<p>See <a href="#app2">app2</a>.</p>
|
|
<h3 id="crosslinks-5">Crosslinks</h3>
|
|
<p><a href="#app1">app1</a> <a href="#app2">app2</a> <a href="#appN">appN</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="appn">appN</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#app1">app1</a> with any number of items.</p>
|
|
<pre><code> ... xN ... x2 x1 x0 [Q] n . appN
|
|
--------------------------------------
|
|
... [xN ...] [Q] . infra first
|
|
...
|
|
[x2 ...] [Q] infra first
|
|
[x1 ...] [Q] infra first
|
|
[x0 ...] [Q] infra first</code></pre>
|
|
<h3 id="discussion-3">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-6">Crosslinks</h3>
|
|
<p><a href="#app1">app1</a> <a href="#app2">app2</a> <a href="#app3">app3</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="section-2">*</h2>
|
|
<p>See <a href="#mul">mul</a>.</p>
|
|
<hr />
|
|
<h2 id="at">at</h2>
|
|
<p>See <a href="#getitem">getitem</a>.</p>
|
|
<hr />
|
|
<h2 id="average">average</h2>
|
|
<p>Compute the average of a list of numbers. (Currently broken until I can figure out what to do about “numeric tower” in Thun.)</p>
|
|
<h3 id="discussion-4">Discussion</h3>
|
|
<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>
|
|
<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>
|
|
<hr />
|
|
<h2 id="binary">binary</h2>
|
|
<p>Combinator</p>
|
|
<p>Run a quoted program using exactly two stack values and leave the first item of the result on the stack.</p>
|
|
<pre><code> ... y x [P] binary
|
|
-----------------------
|
|
... a</code></pre>
|
|
<h3 id="discussion-5">Discussion</h3>
|
|
<p>Runs any other quoted function and returns its first result while consuming exactly two items from the stack.</p>
|
|
<h3 id="crosslinks-7">Crosslinks</h3>
|
|
<p><a href="#nullary">nullary</a> <a href="#ternary">ternary</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="b">b</h2>
|
|
<p>Combinator</p>
|
|
<p>Run two quoted programs</p>
|
|
<pre><code> [P] [Q] b
|
|
---------------
|
|
P Q</code></pre>
|
|
<h3 id="discussion-6">Discussion</h3>
|
|
<p>This combinator may seem trivial but it comes in handy.</p>
|
|
<h3 id="crosslinks-8">Crosslinks</h3>
|
|
<p><a href="#dupdip">dupdip</a> <a href="#ii">ii</a></p>
|
|
<hr />
|
|
<h2 id="bool">bool</h2>
|
|
<p>Convert the item on the top of the stack to a Boolean value.</p>
|
|
<h3 id="discussion-7">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-9">Crosslinks</h3>
|
|
<p><a href="#not">not</a></p>
|
|
<hr />
|
|
<h2 id="branch">branch</h2>
|
|
<p>Combinator</p>
|
|
<p>Use a Boolean value to select and run one of two quoted programs.</p>
|
|
<pre><code> false [F] [T] branch
|
|
--------------------------
|
|
F
|
|
|
|
true [F] [T] branch
|
|
-------------------------
|
|
T</code></pre>
|
|
<h3 id="discussion-8">Discussion</h3>
|
|
<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 “if..then..else” construct <a href="#ifte">ifte</a> adds a predicate function that is evaluated <a href="#nullary">nullary</a>.</p>
|
|
<h3 id="crosslinks-10">Crosslinks</h3>
|
|
<p><a href="#choice">choice</a> <a href="#ifte">ifte</a> <a href="#select">select</a></p>
|
|
<hr />
|
|
<h2 id="ccccons">ccccons</h2>
|
|
<pre><code> a b c d [...] ccccons
|
|
---------------------------
|
|
[a b c d ...]</code></pre>
|
|
<p>Do <a href="#cons">cons</a> four times.</p>
|
|
<h3 id="crosslinks-11">Crosslinks</h3>
|
|
<p><a href="#ccons">ccons</a> <a href="#cons">cons</a> <a href="#times">times</a></p>
|
|
<hr />
|
|
<h2 id="ccons">ccons</h2>
|
|
<pre><code> a b [...] ccons
|
|
---------------------
|
|
[a b ...]</code></pre>
|
|
<p>Do <a href="#cons">cons</a> two times.</p>
|
|
<h3 id="crosslinks-12">Crosslinks</h3>
|
|
<p><a href="#cons">cons</a> <a href="#ccons">ccons</a></p>
|
|
<hr />
|
|
<h2 id="choice">choice</h2>
|
|
<p>Use a Boolean value to select one of two items.</p>
|
|
<pre><code> a b false choice
|
|
----------------------
|
|
a
|
|
|
|
a b true choice
|
|
---------------------
|
|
b</code></pre>
|
|
<h3 id="discussion-9">Discussion</h3>
|
|
<p>It’s a matter of taste whether you implement this in terms of <a href="#branch">branch</a> or the other way around.</p>
|
|
<h3 id="crosslinks-13">Crosslinks</h3>
|
|
<p><a href="#branch">branch</a> <a href="#select">select</a></p>
|
|
<hr />
|
|
<h2 id="clear">clear</h2>
|
|
<p>Clear everything from the stack.</p>
|
|
<h3 id="crosslinks-14">Crosslinks</h3>
|
|
<p><a href="#stack">stack</a> <a href="#swaack">swaack</a></p>
|
|
<hr />
|
|
<h2 id="cleave">cleave</h2>
|
|
<p>Combinator</p>
|
|
<p>Run two programs in parallel, consuming one additional item, and put their results on the stack.</p>
|
|
<pre><code> ... x [A] [B] cleave
|
|
------------------------
|
|
... a b</code></pre>
|
|
<h3 id="derivation">Derivation</h3>
|
|
<blockquote>
|
|
<p><a href="#fork">fork</a> <a href="#popdd">popdd</a></p>
|
|
</blockquote>
|
|
<h3 id="example-1">Example</h3>
|
|
<pre><code> 1 2 3 [+] [-] cleave
|
|
--------------------------
|
|
1 2 5 -1</code></pre>
|
|
<h3 id="discussion-10">Discussion</h3>
|
|
<p>One of a handful of useful parallel combinators.</p>
|
|
<h3 id="crosslinks-15">Crosslinks</h3>
|
|
<p><a href="#clop">clop</a> <a href="#fork">fork</a> <a href="#map">map</a></p>
|
|
<hr />
|
|
<h2 id="clop">clop</h2>
|
|
<p>Combinator</p>
|
|
<p>Run two programs in parallel, consuming two additional items, and put their results on the stack.</p>
|
|
<pre><code> ... x y [A] [B] clop
|
|
--------------------------
|
|
... a b</code></pre>
|
|
<h3 id="discussion-11">Discussion</h3>
|
|
<p>Like <a href="#cleave">cleave</a> but consumes an additional item from the stack.</p>
|
|
<pre><code> 1 2 3 4 [+] [-] clop
|
|
--------------------------
|
|
1 2 7 -1</code></pre>
|
|
<h3 id="crosslinks-16">Crosslinks</h3>
|
|
<p><a href="#cleave">cleave</a> <a href="#fork">fork</a> <a href="#map">map</a></p>
|
|
<hr />
|
|
<h2 id="cmp">cmp</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> a b [G] [E] [L] cmp
|
|
------------------------- a > b
|
|
G
|
|
|
|
a b [G] [E] [L] cmp
|
|
------------------------- a = b
|
|
E
|
|
|
|
a b [G] [E] [L] cmp
|
|
------------------------- a < b
|
|
L</code></pre>
|
|
<h3 id="discussion-12">Discussion</h3>
|
|
<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-11"><=</a> or <a href="#section-6">>=</a> and not all three possibilities, e.g.:</p>
|
|
<pre><code>[G] [EL] dup cmp
|
|
|
|
[GE] [L] dupd cmp</code></pre>
|
|
<p>Or even:</p>
|
|
<pre><code>[GL] [E] over cmp</code></pre>
|
|
<hr />
|
|
<h2 id="codi">codi</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> a b [F] . codi
|
|
--------------------
|
|
b . F a</code></pre>
|
|
<h3 id="discussion-13">Discussion</h3>
|
|
<p>This is one of those weirdly specific functions that turns out to be useful in a few places.</p>
|
|
<h3 id="crosslinks-17">Crosslinks</h3>
|
|
<p><a href="#appn">appN</a> <a href="#codireco">codireco</a></p>
|
|
<hr />
|
|
<h2 id="codireco">codireco</h2>
|
|
<p>Combinator</p>
|
|
<p>This is part of the <a href="#make_generator">make_generator</a> function. You would not use this combinator directly.</p>
|
|
<h3 id="discussion-14">Discussion</h3>
|
|
<p>See <a href="#make_generator">make_generator</a> and the <a href="https://joypy.osdn.io/notebooks/Generator_Programs.html#an-interesting-variation">“Using <code>x</code> to Generate Values” 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>
|
|
<h3 id="crosslinks-18">Crosslinks</h3>
|
|
<p><a href="#make_generator">make_generator</a></p>
|
|
<hr />
|
|
<h2 id="concat">concat</h2>
|
|
<p>Concatinate two lists.</p>
|
|
<pre><code> [a b c] [d e f] concat
|
|
----------------------------
|
|
[a b c d e f]</code></pre>
|
|
<h3 id="crosslinks-19">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="cond">cond</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code>[
|
|
[ [Predicate0] Function0 ]
|
|
[ [Predicate1] Function1 ]
|
|
...
|
|
[ [PredicateN] FunctionN ]
|
|
[Default]
|
|
]
|
|
cond</code></pre>
|
|
<h3 id="discussion-15">Discussion</h3>
|
|
<p>It works by rewriting into a chain of nested <span class="title-ref">ifte</span> expressions, e.g.:</p>
|
|
<pre><code> [[[B0] T0] [[B1] T1] [D]] cond
|
|
-----------------------------------------
|
|
[B0] [T0] [[B1] [T1] [D] ifte] ifte</code></pre>
|
|
<h3 id="crosslinks-20">Crosslinks</h3>
|
|
<p><a href="#ifte">ifte</a></p>
|
|
<hr />
|
|
<h2 id="cons">cons</h2>
|
|
<p>Given an item and a list, append the item to the list to make a new list.</p>
|
|
<pre><code> a [...] cons
|
|
------------------
|
|
[a ...]</code></pre>
|
|
<h3 id="discussion-16">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-21">Crosslinks</h3>
|
|
<p><a href="#uncons">uncons</a></p>
|
|
<hr />
|
|
<h2 id="dinfrirst">dinfrirst</h2>
|
|
<p>Combinator</p>
|
|
<p>Specialist function (that means I forgot what it does and why.)</p>
|
|
<hr />
|
|
<h2 id="dipddd">dipddd</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#dip">dip</a> but expects four items. :</p>
|
|
<pre><code> ... z y x w [Q] . dipddd
|
|
-------------------------------
|
|
... . Q z y x w</code></pre>
|
|
<h3 id="discussion-17">Discussion</h3>
|
|
<p>See <a href="#dip">dip</a>.</p>
|
|
<h3 id="crosslinks-22">Crosslinks</h3>
|
|
<p><a href="#dip">dip</a> <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>
|
|
<hr />
|
|
<h2 id="dipdd">dipdd</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#dip">dip</a> but expects three items. :</p>
|
|
<pre><code> ... z y x [Q] . dipdd
|
|
-----------------------------
|
|
... . Q z y x</code></pre>
|
|
<h3 id="discussion-18">Discussion</h3>
|
|
<p>See <a href="#dip">dip</a>.</p>
|
|
<h3 id="crosslinks-23">Crosslinks</h3>
|
|
<p><a href="#dip">dip</a> <a href="#dipd">dipd</a> <a href="#dipddd">dipddd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a> <a href="#infra">infra</a></p>
|
|
<hr />
|
|
<h2 id="dipd">dipd</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#dip">dip</a> but expects two items.</p>
|
|
<pre><code> ... y x [Q] . dipd
|
|
-------------------------
|
|
... . Q y x</code></pre>
|
|
<h3 id="discussion-19">Discussion</h3>
|
|
<p>See <a href="#dip">dip</a>.</p>
|
|
<h3 id="crosslinks-24">Crosslinks</h3>
|
|
<p><a href="#dip">dip</a> <a href="#dipdd">dipdd</a> <a href="#dipddd">dipddd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a> <a href="#infra">infra</a></p>
|
|
<hr />
|
|
<h2 id="dip">dip</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> ... x [Q] . dip
|
|
---------------------
|
|
... . Q x</code></pre>
|
|
<h3 id="discussion-20">Discussion</h3>
|
|
<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">“Traversing Datastructures with Zippers” notebook</a>.</p>
|
|
<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’t use symbols as “atoms” 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 “set aside” the item and replace it after running <code>Q</code> but that means that there is an “extra space” 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>
|
|
<h3 id="crosslinks-25">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="disenstacken">disenstacken</h2>
|
|
<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>
|
|
<pre><code> 1 2 3 [4 5 6] disenstacken
|
|
--------------------------------
|
|
6 5 4</code></pre>
|
|
<h3 id="discussion-21">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-26">Crosslinks</h3>
|
|
<p><a href="#enstacken">enstacken</a> <a href="#stack">stack</a> <a href="#unstack">unstack</a></p>
|
|
<hr />
|
|
<h2 id="div">div</h2>
|
|
<p>Divide.</p>
|
|
<hr />
|
|
<h2 id="divmod">divmod</h2>
|
|
<pre><code> x y divmod
|
|
------------------
|
|
q r
|
|
(x/y) (x%y)</code></pre>
|
|
<p>Invariant: <code>qy + r = x</code>.</p>
|
|
<hr />
|
|
<h2 id="down_to_zero">down_to_zero</h2>
|
|
<p>Given a number greater than zero put all the Natural numbers (including zero) less than that onto the stack.</p>
|
|
<h3 id="example-2">Example</h3>
|
|
<pre><code> 3 down_to_zero
|
|
--------------------
|
|
3 2 1 0</code></pre>
|
|
<h3 id="crosslinks-27">Crosslinks</h3>
|
|
<p><a href="#range">range</a></p>
|
|
<hr />
|
|
<h2 id="drop">drop</h2>
|
|
<p>Expects an integer and a quote on the stack and returns the quote with n items removed off the top.</p>
|
|
<h3 id="example-3">Example</h3>
|
|
<pre><code> [a b c d] 2 drop
|
|
----------------------
|
|
[c d]</code></pre>
|
|
<h3 id="crosslinks-28">Crosslinks</h3>
|
|
<p><a href="#take">take</a></p>
|
|
<hr />
|
|
<h2 id="dupdd">dupdd</h2>
|
|
<p><a href="#dup">dup</a> the third item down on the stack.</p>
|
|
<pre><code> a b c dupdd
|
|
-----------------
|
|
a a b c</code></pre>
|
|
<h3 id="crosslinks-29">Crosslinks</h3>
|
|
<p><a href="#dup">dup</a> <a href="#dupd">dupd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
|
|
<hr />
|
|
<h2 id="dupdipd">dupdipd</h2>
|
|
<p>Combinator</p>
|
|
<p>Run a copy of program <code>F</code> under the next item down on the stack.</p>
|
|
<pre><code> a [F] dupdipd
|
|
-------------------
|
|
F a [F]</code></pre>
|
|
<h3 id="crosslinks-30">Crosslinks</h3>
|
|
<p><a href="#dupdip">dupdip</a></p>
|
|
<hr />
|
|
<h2 id="dupdip">dupdip</h2>
|
|
<p>Combinator</p>
|
|
<p>Apply a function <code>F</code> and <a href="#dup">dup</a> the item under it on the stack.</p>
|
|
<pre><code> a [F] dupdip
|
|
------------------
|
|
a F a</code></pre>
|
|
<h3 id="derivation-1">Derivation</h3>
|
|
<pre><code>a [F] dupdip
|
|
a [F] dupd dip
|
|
a [F] [dup] dip dip
|
|
a dup [F] dip
|
|
a a [F] dip
|
|
a F a</code></pre>
|
|
<h3 id="discussion-22">Discussion</h3>
|
|
<p>A very common and useful combinator.</p>
|
|
<h3 id="crosslinks-31">Crosslinks</h3>
|
|
<p><a href="#dupdipd">dupdipd</a></p>
|
|
<hr />
|
|
<h2 id="dupd">dupd</h2>
|
|
<p><a href="#dup">dup</a> the second item down on the stack.</p>
|
|
<pre><code> a b dupd
|
|
--------------
|
|
a a b</code></pre>
|
|
<h3 id="crosslinks-32">Crosslinks</h3>
|
|
<p><a href="#dup">dup</a> <a href="#dupdd">dupdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
|
|
<hr />
|
|
<h2 id="dup">dup</h2>
|
|
<p>“Dup”licate the top item on the stack.</p>
|
|
<pre><code> a dup
|
|
-----------
|
|
a a</code></pre>
|
|
<h3 id="crosslinks-33">Crosslinks</h3>
|
|
<p><a href="#dupd">dupd</a> <a href="#dupdd">dupdd</a> <a href="#dupdip">dupdip</a> <a href="#dupdipd">dupdipd</a></p>
|
|
<hr />
|
|
<h2 id="enstacken">enstacken</h2>
|
|
<p>Put the stack onto the stack replacing the contents of the stack.</p>
|
|
<pre><code> ... a b c enstacken
|
|
-------------------------
|
|
[c b a ...]</code></pre>
|
|
<h3 id="discussion-23">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-34">Crosslinks</h3>
|
|
<p><a href="#stack">stack</a> <a href="#disenstacken">disenstacken</a> <a href="#unstack">unstack</a></p>
|
|
<hr />
|
|
<h2 id="eq">eq</h2>
|
|
<p>Compare the two items on the top of the stack for equality and replace them with a Boolean value.</p>
|
|
<pre><code> a b eq
|
|
-------------
|
|
Boolean
|
|
(a = b)</code></pre>
|
|
<h3 id="crosslinks-35">Crosslinks</h3>
|
|
<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="#neq">neq</a></p>
|
|
<hr />
|
|
<h2 id="section-3">=</h2>
|
|
<p>See <a href="#eq">eq</a>.</p>
|
|
<hr />
|
|
<h2 id="section-4">!=</h2>
|
|
<p>See <a href="#neq">neq</a>.</p>
|
|
<hr />
|
|
<h2 id="section-5">!-</h2>
|
|
<p>Not negative.</p>
|
|
<pre><code> n !-
|
|
----------- n < 0
|
|
false
|
|
|
|
|
|
n !-
|
|
---------- n >= 0
|
|
true</code></pre>
|
|
<h3 id="discussion-24">Discussion</h3>
|
|
<p>Return a Boolean value indicating if a number is greater than or equal to zero.</p>
|
|
<hr />
|
|
<h2 id="first">first</h2>
|
|
<p>Replace a list with its first item.</p>
|
|
<pre><code> [a ...]
|
|
--------------
|
|
a</code></pre>
|
|
<h3 id="crosslinks-36">Crosslinks</h3>
|
|
<p><a href="#second">second</a> <a href="#third">third</a> <a href="#fourth">fourth</a> <a href="#rest">rest</a></p>
|
|
<hr />
|
|
<h2 id="first_two">first_two</h2>
|
|
<p>Replace a list with its first two items.</p>
|
|
<pre><code> [a b ...] first_two
|
|
-------------------------
|
|
a b</code></pre>
|
|
<h3 id="crosslinks-37">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="flatten">flatten</h2>
|
|
<p>Given a list of lists, concatinate them.</p>
|
|
<h3 id="example-4">Example</h3>
|
|
<pre><code> [[1 2] [3 [4] 5] [6 7]] flatten
|
|
-------------------------------------
|
|
[1 2 3 [4] 5 6 7]</code></pre>
|
|
<h3 id="discussion-25">Discussion</h3>
|
|
<p>Note that only one “level” of lists is flattened. In the example above <code>[4]</code> is not unquoted.</p>
|
|
<h3 id="crosslinks-38">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="fork">fork</h2>
|
|
<p>Combinator</p>
|
|
<p>Run two quoted programs in parallel and replace them with their results.</p>
|
|
<pre><code> ... [F] [G] fork
|
|
----------------------
|
|
... f g</code></pre>
|
|
<h3 id="discussion-26">Discussion</h3>
|
|
<p>The basic parallelism combinator, the two programs are run independently.</p>
|
|
<h3 id="crosslinks-39">Crosslinks</h3>
|
|
<p><a href="#cleave">cleave</a> <a href="#clop">clop</a> <a href="#map">map</a></p>
|
|
<hr />
|
|
<h2 id="fourth">fourth</h2>
|
|
<p>Replace a list with its fourth item.</p>
|
|
<pre><code> [a b c d ...] fourth
|
|
--------------------------
|
|
d</code></pre>
|
|
<h3 id="crosslinks-40">Crosslinks</h3>
|
|
<p><a href="#first">first</a> <a href="#second">second</a> <a href="#third">third</a> <a href="#rest">rest</a></p>
|
|
<hr />
|
|
<h2 id="gcd">gcd</h2>
|
|
<p>Take two integers from the stack and replace them with their Greatest Common Denominator.</p>
|
|
<h3 id="discussion-27">Discussion</h3>
|
|
<p>Euclid’s Algorithm</p>
|
|
<hr />
|
|
<h2 id="ge">ge</h2>
|
|
<p>Greater-than-or-equal-to comparison of two numbers.</p>
|
|
<pre><code> a b ge
|
|
--------------
|
|
Boolean
|
|
(a >= b)</code></pre>
|
|
<h3 id="crosslinks-41">Crosslinks</h3>
|
|
<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="#neq">neq</a></p>
|
|
<hr />
|
|
<h2 id="genrec">genrec</h2>
|
|
<p>Combinator</p>
|
|
<p><strong>Gen</strong>eral <strong>Rec</strong>ursion Combinator.</p>
|
|
<pre><code> [if] [then] [rec1] [rec2] genrec
|
|
---------------------------------------------------------------------
|
|
[if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte</code></pre>
|
|
<h3 id="discussion-28">Discussion</h3>
|
|
<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>
|
|
<p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
|
|
<p>From <a href="https://www.kevinalbrecht.com/code/joy-mirror/j05cmp.html">“Recursion Theory and Joy”</a> by Manfred von Thun:</p>
|
|
<blockquote>
|
|
<p>“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.”</p>
|
|
</blockquote>
|
|
<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 “sandwiching” a quotation of the whole function.</p>
|
|
<p>For example, given a (general recursive) function <code>F</code>:</p>
|
|
<pre><code>F == [I] [T] [R1] [R2] genrec</code></pre>
|
|
<p>If the <code>[I]</code> if-part fails you must derive <code>R1</code> and <code>R2</code> from: :</p>
|
|
<pre><code>... R1 [F] R2</code></pre>
|
|
<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>
|
|
<pre><code>F == [I] [T] [R1] [R2] genrec
|
|
== [I] [T] [R1 [F] R2] ifte</code></pre>
|
|
<p>Tail recursive functions are those where <code>R2</code> is the <code>i</code> combinator:</p>
|
|
<pre><code>P == [I] [T] [R] tailrec
|
|
== [I] [T] [R [P] i] ifte
|
|
== [I] [T] [R P] ifte</code></pre>
|
|
<h3 id="crosslinks-42">Crosslinks</h3>
|
|
<p><a href="#anamorphism">anamorphism</a> <a href="#tailrec">tailrec</a> <a href="#x">x</a></p>
|
|
<hr />
|
|
<h2 id="getitem">getitem</h2>
|
|
<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>
|
|
<h3 id="example-5">Example</h3>
|
|
<pre><code> [a b c d] 2 getitem
|
|
-------------------------
|
|
c</code></pre>
|
|
<h3 id="discussion-29">Discussion</h3>
|
|
<p>If the number isn’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>
|
|
<h3 id="crosslinks-43">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="grabn">grabN</h2>
|
|
<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>
|
|
<h3 id="example-6">Example</h3>
|
|
<pre><code> a b c d e 3 grabN
|
|
-----------------------
|
|
a b [c d e]</code></pre>
|
|
<hr />
|
|
<h2 id="grba">grba</h2>
|
|
<p>A weird function used in <a href="#app2">app2</a> that does this:</p>
|
|
<pre><code> ... 1 2 3 4 5 grba
|
|
-------------------------------
|
|
... 1 2 3 [4 3 2 1 ...] 5</code></pre>
|
|
<p>It grabs the stack under the top item, and substitutes it for the second item down on the stack.</p>
|
|
<h3 id="discussion-30">Discussion</h3>
|
|
<p>This function “grabs” an item from the stack along with a copy of the stack. It’s part of the <a href="#app2">app2</a> definition.</p>
|
|
<h3 id="crosslinks-44">Crosslinks</h3>
|
|
<p><a href="#app2">app2</a></p>
|
|
<hr />
|
|
<h2 id="section-6">>=</h2>
|
|
<p>See <a href="#ge">ge</a>.</p>
|
|
<hr />
|
|
<h2 id="section-7">>></h2>
|
|
<p>See <a href="#rshift">rshift</a>.</p>
|
|
<hr />
|
|
<h2 id="section-8">></h2>
|
|
<p>See <a href="#gt">gt</a>.</p>
|
|
<hr />
|
|
<h2 id="gt">gt</h2>
|
|
<p>Greater-than comparison of two numbers.</p>
|
|
<pre><code> a b gt
|
|
--------------
|
|
Boolean
|
|
(a > b)</code></pre>
|
|
<h3 id="crosslinks-45">Crosslinks</h3>
|
|
<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="#neq">neq</a></p>
|
|
<hr />
|
|
<h2 id="help">help</h2>
|
|
<p>Accepts a quoted symbol on the top of the stack and prints its documentation.</p>
|
|
<pre><code> [foo] help
|
|
----------------</code></pre>
|
|
<h3 id="discussion-31">Discussion</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="section-9">–</h2>
|
|
<p>See <a href="#pred">pred</a>.</p>
|
|
<hr />
|
|
<h2 id="section-10">-</h2>
|
|
<p>See <a href="#sub">sub</a>.</p>
|
|
<hr />
|
|
<h2 id="id">id</h2>
|
|
<p>The identity function.</p>
|
|
<h3 id="discussion-32">Discussion</h3>
|
|
<p>Does nothing. It’s kind of a mathematical thing, but it occasionally comes in handy.</p>
|
|
<hr />
|
|
<h2 id="ifte">ifte</h2>
|
|
<p>Combinator</p>
|
|
<p>If-Then-Else combinator, a common and convenient specialization of <a href="#branch">branch</a>.</p>
|
|
<pre><code> [if] [then] [else] ifte
|
|
---------------------------------------
|
|
[if] nullary [else] [then] branch</code></pre>
|
|
<h3 id="crosslinks-46">Crosslinks</h3>
|
|
<p><a href="#branch">branch</a> <a href="#loop">loop</a> <a href="#while">while</a></p>
|
|
<hr />
|
|
<h2 id="ii">ii</h2>
|
|
<p>Combinator</p>
|
|
<p>Take a quoted program from the stack and run it twice, first under the top item, then again with the top item.</p>
|
|
<pre><code>... a [Q] ii
|
|
------------------
|
|
... Q a Q</code></pre>
|
|
<h3 id="example-7">Example</h3>
|
|
<p>It’s a little tricky to understand how this works so here’s an example trace:</p>
|
|
<pre><code> 1 2 3 4 [++] • [dip] dupdip i
|
|
1 2 3 4 [++] [dip] • dupdip i
|
|
1 2 3 4 [++] • dip [++] i
|
|
1 2 3 • ++ 4 [++] i
|
|
1 2 4 • 4 [++] i
|
|
1 2 4 4 • [++] i
|
|
1 2 4 4 [++] • i
|
|
1 2 4 4 • ++
|
|
1 2 4 5 •</code></pre>
|
|
<h3 id="discussion-33">Discussion</h3>
|
|
<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’s not:</p>
|
|
<pre><code> 1 2 3 4 [+] ii
|
|
--------------------
|
|
1 9
|
|
|
|
1 2 3 4 [+] app2
|
|
----------------------
|
|
1 2 5 6</code></pre>
|
|
<h3 id="crosslinks-47">Crosslinks</h3>
|
|
<p><a href="#app2">app2</a> <a href="#b">b</a></p>
|
|
<hr />
|
|
<h2 id="i">i</h2>
|
|
<p>Combinator</p>
|
|
<p>Append a quoted expression onto the pending expression.</p>
|
|
<pre><code> [Q] . i
|
|
-------------
|
|
. Q</code></pre>
|
|
<h3 id="discussion-34">Discussion</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="infra">infra</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> ... x y z [a b c] [Q] infra
|
|
---------------------------------
|
|
c b a Q [z y x ...] swaack
|
|
|
|
|
|
... [a b c] [F] swons swaack [i] dip swaack
|
|
... [[F] a b c] swaack [i] dip swaack
|
|
|
|
c b a [F] [...] [i] dip swaack
|
|
c b a [F] i [...] swaack
|
|
c b a F [...] swaack
|
|
d e [...] swaack
|
|
... [e d]</code></pre>
|
|
<h3 id="discussion-35">Discussion</h3>
|
|
<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 “pocket universe”. If the list represents a datastructure then <code>infra</code> lets you work on its internal structure.</p>
|
|
<h3 id="crosslinks-48">Crosslinks</h3>
|
|
<p><a href="#swaack">swaack</a></p>
|
|
<hr />
|
|
<h2 id="infrst">infrst</h2>
|
|
<p>Combinator</p>
|
|
<p>Does <a href="#infra">infra</a> and then extracts the <a href="#first">first</a> item from the resulting list.</p>
|
|
<hr />
|
|
<h2 id="inscribe">inscribe</h2>
|
|
<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>
|
|
<h3 id="example-8">Example</h3>
|
|
<pre><code>[sqr dup mul] inscribe</code></pre>
|
|
<h3 id="discussion-36">Discussion</h3>
|
|
<p>This is the only function that modifies the dictionary. It’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’re doing.</p>
|
|
<hr />
|
|
<h2 id="le">le</h2>
|
|
<p>Less-Than-or-Equal-to comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
|
|
<pre><code> a b le
|
|
-------------
|
|
Boolean
|
|
(a <= b)</code></pre>
|
|
<h3 id="crosslinks-49">Crosslinks</h3>
|
|
<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="#neq">neq</a></p>
|
|
<hr />
|
|
<h2 id="section-11"><=</h2>
|
|
<p>See <a href="#le">le</a>.</p>
|
|
<hr />
|
|
<h2 id="section-12"><></h2>
|
|
<p>See <a href="#neq">neq</a>.</p>
|
|
<hr />
|
|
<h2 id="section-13"><</h2>
|
|
<pre><code> ... a <{}
|
|
----------------
|
|
... [] a</code></pre>
|
|
<h3 id="discussion-37">Discussion</h3>
|
|
<p>Tuck an empty list just under the first item on the stack.</p>
|
|
<h3 id="crosslinks-50">Crosslinks</h3>
|
|
<p><a href="#section-18"><<{}</a></p>
|
|
<hr />
|
|
<h2 id="section-14"><<</h2>
|
|
<pre><code> ... b a <{}
|
|
-----------------
|
|
... [] b a</code></pre>
|
|
<h3 id="discussion-38">Discussion</h3>
|
|
<p>Tuck an empty list just under the first two items on the stack.</p>
|
|
<h3 id="crosslinks-51">Crosslinks</h3>
|
|
<p><a href="#section-16"><{}</a></p>
|
|
<hr />
|
|
<h2 id="section-15"><<</h2>
|
|
<p>See <a href="#lshift">lshift</a>.</p>
|
|
<hr />
|
|
<h2 id="section-16"><</h2>
|
|
<p>See <a href="#lt">lt</a>.</p>
|
|
<hr />
|
|
<h2 id="loop">loop</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> false [Q] loop
|
|
--------------------
|
|
|
|
|
|
true [Q] . loop
|
|
--------------------------
|
|
. Q [Q] loop</code></pre>
|
|
<h3 id="discussion-39">Discussion</h3>
|
|
<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>
|
|
<blockquote>
|
|
<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>
|
|
</blockquote>
|
|
<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’s more common and convenient form <a href="#ifte">ifte</a>, <a href="#loop">loop</a> has <a href="#while">while</a>.</p>
|
|
<h3 id="crosslinks-52">Crosslinks</h3>
|
|
<p><a href="#branch">branch</a> <a href="#fork">fork</a> <a href="#while">while</a></p>
|
|
<hr />
|
|
<h2 id="lshift">lshift</h2>
|
|
<p><a href="https://en.wikipedia.org/wiki/Logical_shift">Logical Left-Shift</a></p>
|
|
<pre><code> a n lshift
|
|
----------------
|
|
(a×2ⁿ)</code></pre>
|
|
<h3 id="crosslinks-53">Crosslinks</h3>
|
|
<p><a href="#rshift">rshift</a></p>
|
|
<hr />
|
|
<h2 id="lt">lt</h2>
|
|
<p>Less-Than comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
|
|
<pre><code> a b lt
|
|
-------------
|
|
Boolean
|
|
(a < b)</code></pre>
|
|
<h3 id="crosslinks-54">Crosslinks</h3>
|
|
<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="#neq">neq</a></p>
|
|
<hr />
|
|
<h2 id="make_generator">make_generator</h2>
|
|
<p>Given an initial state value and a quoted generator function build a generator quote.</p>
|
|
<pre><code> state [generator function] make_generator
|
|
-----------------------------------------------
|
|
[state [generator function] codireco]</code></pre>
|
|
<h3 id="example-9">Example</h3>
|
|
<pre><code> 230 [dup ++] make_generator
|
|
---------------------------------
|
|
[230 [dup ++] codireco]</code></pre>
|
|
<p>And then:</p>
|
|
<pre><code> [230 [dup ++] codireco] 5 [x] times pop
|
|
---------------------------------------------
|
|
230 231 232 233 234</code></pre>
|
|
<h3 id="discussion-40">Discussion</h3>
|
|
<p>See the <a href="https://joypy.osdn.io/notebooks/Generator_Programs.html#an-interesting-variation">“Using <code>x</code> to Generate Values” notebook</a>.</p>
|
|
<h3 id="crosslinks-55">Crosslinks</h3>
|
|
<p><a href="#codireco">codireco</a></p>
|
|
<hr />
|
|
<h2 id="map">map</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<h3 id="example-10">Example</h3>
|
|
<pre><code> 5 [1 2 3] [++ *] map
|
|
--------------------------
|
|
5 [10 15 20]</code></pre>
|
|
<h3 id="discussion-41">Discussion</h3>
|
|
<p>This is a common operation in many languages. In Joy it can be a parallelism combinator due to the “pure” nature of the language.</p>
|
|
<h3 id="crosslinks-56">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="max">max</h2>
|
|
<p>Given a list find the maximum.</p>
|
|
<h3 id="example-11">Example</h3>
|
|
<pre><code> [1 2 3 4] max
|
|
-------------------
|
|
4</code></pre>
|
|
<h3 id="crosslinks-57">Crosslinks</h3>
|
|
<p><a href="#min">min</a> <a href="#size">size</a> <a href="#sum">sum</a></p>
|
|
<hr />
|
|
<h2 id="min">min</h2>
|
|
<p>Given a list find the minimum.</p>
|
|
<h3 id="example-12">Example</h3>
|
|
<pre><code> [1 2 3 4] min
|
|
-------------------
|
|
1 </code></pre>
|
|
<h3 id="crosslinks-58">Crosslinks</h3>
|
|
<p><a href="#max">max</a> <a href="#size">size</a> <a href="#sum">sum</a></p>
|
|
<hr />
|
|
<h2 id="mod">mod</h2>
|
|
<p>Return the remainder of <code>a</code> divided by <code>b</code>.</p>
|
|
<pre><code> a b mod
|
|
-------------
|
|
(a%b)</code></pre>
|
|
<h3 id="crosslinks-59">Crosslinks</h3>
|
|
<p><a href="#divmod">divmod</a> <a href="#mul">mul</a></p>
|
|
<hr />
|
|
<h2 id="modulus">modulus</h2>
|
|
<p>See <a href="#mod">mod</a>.</p>
|
|
<hr />
|
|
<h2 id="mul">mul</h2>
|
|
<p>Multiply two numbers.</p>
|
|
<pre><code> a b mul
|
|
-------------
|
|
(a×b)</code></pre>
|
|
<h3 id="crosslinks-60">Crosslinks</h3>
|
|
<p><a href="#div">div</a> <a href="#product">product</a></p>
|
|
<hr />
|
|
<h2 id="neg">neg</h2>
|
|
<p>Invert the sign of a number.</p>
|
|
<pre><code> a neg
|
|
-----------
|
|
-a</code></pre>
|
|
<hr />
|
|
<h2 id="neq">neq</h2>
|
|
<p>Not-Equal comparison of the two items on the top of the stack, replacing them with a Boolean value.</p>
|
|
<pre><code> a b neq
|
|
-------------
|
|
Boolean
|
|
(a = b)</code></pre>
|
|
<h3 id="crosslinks-61">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="not">not</h2>
|
|
<p>Invert the Boolean value on the top of the stack.</p>
|
|
<pre><code> true not
|
|
--------------
|
|
false
|
|
|
|
false not
|
|
---------------
|
|
true</code></pre>
|
|
<h3 id="crosslinks-62">Crosslinks</h3>
|
|
<p><a href="#bool">bool</a></p>
|
|
<hr />
|
|
<h2 id="nulco">nulco</h2>
|
|
<p>Take the item on the top of the stack and <a href="#cons">cons</a> it onto <code>[nullary]</code>.</p>
|
|
<pre><code> [F] nulco
|
|
-------------------
|
|
[[F] nullary]</code></pre>
|
|
<h3 id="discussion-42">Discussion</h3>
|
|
<p>Helper function for <a href="#or">or</a> and <a href="#and">and</a>.</p>
|
|
<h3 id="crosslinks-63">Crosslinks</h3>
|
|
<p><a href="#and">and</a> <a href="#or">or</a></p>
|
|
<hr />
|
|
<h2 id="null">null</h2>
|
|
<p>True if the item on the top of the stack is an empty list, false if it’s a list but not empty, and an error if it’s not a list.</p>
|
|
<hr />
|
|
<h2 id="nullary">nullary</h2>
|
|
<p>Combinator</p>
|
|
<p>Run a quoted program without using any stack values and leave the first item of the result on the stack.</p>
|
|
<pre><code> ... [P] nullary
|
|
---------------------
|
|
... a</code></pre>
|
|
<h3 id="example-13">Example</h3>
|
|
<pre><code>... [P] nullary
|
|
... [P] [stack] dip infra first
|
|
... stack [P] infra first
|
|
... [...] [P] infra first
|
|
... [a ...] first
|
|
... a</code></pre>
|
|
<h3 id="discussion-43">Discussion</h3>
|
|
<p>A very useful function that runs any other quoted function and returns it’s first result without disturbing the stack (under the quoted program.)</p>
|
|
<h3 id="crosslinks-64">Crosslinks</h3>
|
|
<p><a href="#unary">unary</a> <a href="#binary">binary</a> <a href="#ternary">ternary</a></p>
|
|
<hr />
|
|
<h2 id="of">of</h2>
|
|
<p>Like <a href="#getitem">getitem</a> but <a href="#swap">swap</a>s the order of arguments.</p>
|
|
<h3 id="example-14">Example</h3>
|
|
<pre><code> 2 [a b c d] of
|
|
--------------------
|
|
c</code></pre>
|
|
<h3 id="crosslinks-65">Crosslinks</h3>
|
|
<p><a href="#getitem">getitem</a></p>
|
|
<hr />
|
|
<h2 id="or">or</h2>
|
|
<p>Logical bit-wise OR.</p>
|
|
<h3 id="crosslinks-66">Crosslinks</h3>
|
|
<p><a href="#and">and</a></p>
|
|
<hr />
|
|
<h2 id="over">over</h2>
|
|
<p><a href="#dup">dup</a> the second item on the stack <code>over</code> the first.</p>
|
|
<pre><code> a b over
|
|
--------------
|
|
a b a</code></pre>
|
|
<h3 id="definition">Definition</h3>
|
|
<p>There are many many ways to define this function.</p>
|
|
<blockquote>
|
|
<p><a href="#swap">swap</a> <a href="#tuck">tuck</a></p>
|
|
</blockquote>
|
|
<blockquote>
|
|
<p>[<a href="#pop">pop</a>] <a href="#nullary">nullary</a></p>
|
|
</blockquote>
|
|
<blockquote>
|
|
<p>[<a href="#dup">dup</a>] <a href="#dip">dip</a> <a href="#swap">swap</a></p>
|
|
</blockquote>
|
|
<blockquote>
|
|
<p><a href="#unit">unit</a> <a href="#dupdip">dupdip</a></p>
|
|
</blockquote>
|
|
<blockquote>
|
|
<p><a href="#unit">unit</a> <a href="#dupdipd">dupdipd</a> <a href="#first">first</a></p>
|
|
</blockquote>
|
|
<p>And so on…</p>
|
|
<h3 id="discussion-44">Discussion</h3>
|
|
<p>A fine old word from Forth.</p>
|
|
<h3 id="crosslinks-67">Crosslinks</h3>
|
|
<p><a href="#tuck">tuck</a></p>
|
|
<hr />
|
|
<h2 id="pam">pam</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<h3 id="example-15">Example</h3>
|
|
<pre><code> 5 7 [[+][-][*][/][%]] pam
|
|
-------------------------------
|
|
5 7 [12 -2 35 0 5]</code></pre>
|
|
<h3 id="discussion-45">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-68">Crosslinks</h3>
|
|
<p><a href="#map">map</a></p>
|
|
<hr />
|
|
<h2 id="section-17">%</h2>
|
|
<p>See <a href="#mod">mod</a>.</p>
|
|
<hr />
|
|
<h2 id="pick">pick</h2>
|
|
<p>See <a href="#getitem">getitem</a>.</p>
|
|
<hr />
|
|
<h2 id="section-18">+</h2>
|
|
<p>See <a href="#add">add</a>.</p>
|
|
<hr />
|
|
<h2 id="section-19">++</h2>
|
|
<p>See <a href="#succ">succ</a>.</p>
|
|
<hr />
|
|
<h2 id="pm">pm</h2>
|
|
<p>Plus or minus. Replace two numbers with their sum and difference.</p>
|
|
<pre><code> a b pm
|
|
-----------------
|
|
(a+b) (a-b)</code></pre>
|
|
<hr />
|
|
<h2 id="popdd">popdd</h2>
|
|
<p><a href="#pop">pop</a> the third item on the stack.</p>
|
|
<pre><code> a b c popdd
|
|
-----------------
|
|
b c</code></pre>
|
|
<h3 id="crosslinks-69">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="popd">popd</h2>
|
|
<p><a href="#pop">pop</a> the second item down on the stack.</p>
|
|
<pre><code> a b popd
|
|
--------------
|
|
b</code></pre>
|
|
<h3 id="crosslinks-70">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="pop">pop</h2>
|
|
<p>Pop the top item from the stack and discard it.</p>
|
|
<pre><code> a pop
|
|
-----------</code></pre>
|
|
<h3 id="crosslinks-71">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="popopdd">popopdd</h2>
|
|
<pre><code> a b c d popopdd
|
|
---------------------
|
|
c d</code></pre>
|
|
<h3 id="crosslinks-72">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="popopd">popopd</h2>
|
|
<p><a href="#pop">pop</a> the second and third items from the stack.</p>
|
|
<pre><code> a b c popopd
|
|
------------------
|
|
c</code></pre>
|
|
<h3 id="crosslinks-73">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="popop">popop</h2>
|
|
<p><a href="#pop">pop</a> two items from the stack.</p>
|
|
<pre><code> a b popop
|
|
---------------</code></pre>
|
|
<h3 id="crosslinks-74">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="popopop">popopop</h2>
|
|
<p><a href="#pop">pop</a> three items from the stack.</p>
|
|
<pre><code> a b c popopop
|
|
-------------------</code></pre>
|
|
<h3 id="crosslinks-75">Crosslinks</h3>
|
|
<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>
|
|
<hr />
|
|
<h2 id="pow">pow</h2>
|
|
<p>Take two numbers <code>a</code> and <code>n</code> from the stack and raise <code>a</code> to the <code>n</code>th power. (<code>n</code> is on the top of the stack.)</p>
|
|
<pre><code> a n pow
|
|
-------------
|
|
(aⁿ)</code></pre>
|
|
<h3 id="example-16">Example</h3>
|
|
<pre><code> 2 [2 3 4 5 6 7 8 9] [pow] map
|
|
-----------------------------------
|
|
2 [4 8 16 32 64 128 256 512]</code></pre>
|
|
<hr />
|
|
<h2 id="pred">pred</h2>
|
|
<p>Predecessor. Decrement TOS.</p>
|
|
<h3 id="crosslinks-76">Crosslinks</h3>
|
|
<p><a href="#succ">succ</a></p>
|
|
<hr />
|
|
<h2 id="primrec">primrec</h2>
|
|
<p>Combinator</p>
|
|
<p>From the <a href="https://www.kevinalbrecht.com/code/joy-mirror/j00ovr.html">“Overview of the language JOY”</a></p>
|
|
<blockquote>
|
|
<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>
|
|
</blockquote>
|
|
<blockquote>
|
|
<p>5 [1] [*] primrec</p>
|
|
</blockquote>
|
|
<blockquote>
|
|
<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>
|
|
</blockquote>
|
|
<pre><code> 0 [Base] [Recur] primrec
|
|
------------------------------
|
|
Base
|
|
|
|
n [Base] [Recur] primrec
|
|
------------------------------------------ n > 0
|
|
n (n-1) [Base] [Recur] primrec Recur</code></pre>
|
|
<h3 id="discussion-46">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-77">Crosslinks</h3>
|
|
<p><a href="#genrec">genrec</a> <a href="#tailrec">tailrec</a></p>
|
|
<hr />
|
|
<h2 id="product">product</h2>
|
|
<p>Just as <a href="#sum">sum</a> sums a list of numbers, this function multiplies them together.</p>
|
|
<h3 id="definition-1">Definition</h3>
|
|
<blockquote>
|
|
<p>1 <a href="#swap">swap</a> [<a href="#mul">mul</a>] <a href="#step">step</a></p>
|
|
</blockquote>
|
|
<p>Or,</p>
|
|
<blockquote>
|
|
<p>[1] [<a href="#mul">mul</a>] <a href="#primrec">primrec</a></p>
|
|
</blockquote>
|
|
<hr />
|
|
<h2 id="section-20">?</h2>
|
|
<p>Is the item on the top of the stack “truthy”?</p>
|
|
<h3 id="discussion-47">Discussion</h3>
|
|
<p>You often want to test the truth value of an item on the stack without consuming the item.</p>
|
|
<h3 id="crosslinks-78">Crosslinks</h3>
|
|
<p><a href="#bool">bool</a></p>
|
|
<hr />
|
|
<h2 id="quoted">quoted</h2>
|
|
<p>“Quote D” Wrap the second item on the stack in a list.</p>
|
|
<pre><code> a b quoted
|
|
----------------
|
|
[a] b</code></pre>
|
|
<h3 id="discussion-48">Discussion</h3>
|
|
<p>This comes from the original Joy stuff.</p>
|
|
<h3 id="crosslinks-79">Crosslinks</h3>
|
|
<p><a href="#unit">unit</a></p>
|
|
<hr />
|
|
<h2 id="range">range</h2>
|
|
<p>Expect a number <code>n</code> on the stack and replace it with a list: <code>[(n-1)...0]</code>.</p>
|
|
<h3 id="example-17">Example</h3>
|
|
<pre><code> 5 range
|
|
-----------------
|
|
[4 3 2 1 0]
|
|
|
|
-5 range
|
|
--------------
|
|
[]</code></pre>
|
|
<h3 id="discussion-49">Discussion</h3>
|
|
<p>If <code>n</code> is less than 1 the resulting list is empty.</p>
|
|
<h3 id="crosslinks-80">Crosslinks</h3>
|
|
<p><a href="#range_to_zero">range_to_zero</a></p>
|
|
<hr />
|
|
<h2 id="range_to_zero">range_to_zero</h2>
|
|
<p>Take a number <code>n</code> from the stack and replace it with a list <code>[0...n]</code>.</p>
|
|
<h3 id="example-18">Example</h3>
|
|
<pre><code> 5 range_to_zero
|
|
---------------------
|
|
[0 1 2 3 4 5]</code></pre>
|
|
<h3 id="discussion-50">Discussion</h3>
|
|
<p>Note that the order is reversed compared to <a href="#range">range</a>.</p>
|
|
<h3 id="crosslinks-81">Crosslinks</h3>
|
|
<p><a href="#down_to_zero">down_to_zero</a> <a href="#range">range</a></p>
|
|
<hr />
|
|
<h2 id="reco">reco</h2>
|
|
<p>Replace the first item in a list with the item under it.</p>
|
|
<pre><code> a [b ...] reco
|
|
--------------------
|
|
[a ...]</code></pre>
|
|
<h3 id="crosslinks-82">Crosslinks</h3>
|
|
<p><a href="#codireco">codireco</a> <a href="#make_generator">make_generator</a></p>
|
|
<hr />
|
|
<h2 id="remainder">remainder</h2>
|
|
<p>See <a href="#mod">mod</a>.</p>
|
|
<hr />
|
|
<h2 id="rem">rem</h2>
|
|
<p>See <a href="#mod">mod</a>.</p>
|
|
<hr />
|
|
<h2 id="remove">remove</h2>
|
|
<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’t in the list then the list is unchanged.</p>
|
|
<pre><code> [1 2 3 1] 1 remove
|
|
------------------------
|
|
[2 3 1]</code></pre>
|
|
<h3 id="definition-2">Definition</h3>
|
|
<p>See the <a href="https://osdn.net/projects/joypy/scm/git/Thun/blobs/master/docs/notebooks/Remove-Function.ipynb">“Remove Function” notebook</a>.</p>
|
|
<hr />
|
|
<h2 id="rest">rest</h2>
|
|
<pre><code> [a ...] rest
|
|
------------------
|
|
[...]</code></pre>
|
|
<h3 id="crosslinks-83">Crosslinks</h3>
|
|
<p><a href="#first">first</a> <a href="#uncons">uncons</a></p>
|
|
<hr />
|
|
<h2 id="reverse">reverse</h2>
|
|
<p>Reverse the list on the top of the stack.</p>
|
|
<h3 id="example-19">Example</h3>
|
|
<pre><code> [1 2 3] reverse
|
|
---------------------
|
|
[3 2 1]</code></pre>
|
|
<hr />
|
|
<h2 id="rolldown">rolldown</h2>
|
|
<pre><code> a b c rolldown
|
|
--------------------
|
|
b c a</code></pre>
|
|
<h3 id="crosslinks-84">Crosslinks</h3>
|
|
<p><a href="#rollup">rollup</a></p>
|
|
<hr />
|
|
<h2 id="roll">roll></h2>
|
|
<p>See <a href="#rollup">rollup</a>.</p>
|
|
<hr />
|
|
<h2 id="roll-1">roll<</h2>
|
|
<p>See <a href="#rolldown">rolldown</a>.</p>
|
|
<hr />
|
|
<h2 id="rollup">rollup</h2>
|
|
<pre><code> a b c rollup
|
|
------------------
|
|
c a b</code></pre>
|
|
<h3 id="crosslinks-85">Crosslinks</h3>
|
|
<p><a href="#rolldown">rolldown</a></p>
|
|
<hr />
|
|
<h2 id="rrest">rrest</h2>
|
|
<pre><code> [a b ...] rrest
|
|
---------------------
|
|
[...]</code></pre>
|
|
<h3 id="crosslinks-86">Crosslinks</h3>
|
|
<p><a href="#rest">rest</a></p>
|
|
<hr />
|
|
<h2 id="rshift">rshift</h2>
|
|
<p><a href="https://en.wikipedia.org/wiki/Logical_shift">Logical Right-Shift</a></p>
|
|
<pre><code> a n rshift
|
|
----------------
|
|
(a∕2ⁿ)</code></pre>
|
|
<h3 id="crosslinks-87">Crosslinks</h3>
|
|
<p><a href="#lshift">lshift</a></p>
|
|
<hr />
|
|
<h2 id="run">run</h2>
|
|
<p>Run a quoted program in a list.</p>
|
|
<h3 id="example-20">Example</h3>
|
|
<pre><code> [1 2 +] run
|
|
-----------------
|
|
[3]</code></pre>
|
|
<hr />
|
|
<h2 id="second">second</h2>
|
|
<pre><code> [a b ...] second
|
|
----------------------
|
|
b</code></pre>
|
|
<h3 id="crosslinks-88">Crosslinks</h3>
|
|
<p><a href="#first">first</a> <a href="#third">third</a> <a href="#fourth">fourth</a></p>
|
|
<hr />
|
|
<h2 id="select">select</h2>
|
|
<p>Use a Boolean value to select one of two items from a sequence. :</p>
|
|
<pre><code> [a b] false select
|
|
------------------------
|
|
a
|
|
|
|
[a b] true select
|
|
-----------------------
|
|
b</code></pre>
|
|
<h3 id="discussion-51">Discussion</h3>
|
|
<p>The sequence can contain more than two items but not fewer.</p>
|
|
<h3 id="crosslinks-89">Crosslinks</h3>
|
|
<p><a href="#choice">choice</a></p>
|
|
<hr />
|
|
<h2 id="sharing">sharing</h2>
|
|
<p>Print redistribution information.</p>
|
|
<h3 id="discussion-52">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-90">Crosslinks</h3>
|
|
<p><a href="#warranty">warranty</a></p>
|
|
<hr />
|
|
<h2 id="shift">shift</h2>
|
|
<p>Move the top item from one list to another.</p>
|
|
<h3 id="example-21">Example</h3>
|
|
<pre><code> [x y z] [a b c] shift
|
|
---------------------------
|
|
[a x y z] [b c]</code></pre>
|
|
<h3 id="crosslinks-91">Crosslinks</h3>
|
|
<p><a href="#shunt">shunt</a></p>
|
|
<hr />
|
|
<h2 id="shunt">shunt</h2>
|
|
<p>Like <a href="#concat">concat</a> but <a href="#reverse">reverse</a> the top list into the second.</p>
|
|
<h3 id="example-22">Example</h3>
|
|
<pre><code> [a b c] [d e f] shunt
|
|
---------------------------
|
|
[f e d a b c] </code></pre>
|
|
<h3 id="discussion-53">Discussion</h3>
|
|
<p>This is more efficient than <a href="#concat">concat</a> so prefer it if you don’t need to preserve order.</p>
|
|
<h3 id="crosslinks-92">Crosslinks</h3>
|
|
<p><a href="#concat">concat</a> <a href="#reverse">reverse</a> <a href="#shift">shift</a></p>
|
|
<hr />
|
|
<h2 id="size">size</h2>
|
|
<p>Replace a list with its size.</p>
|
|
<h3 id="example-23">Example</h3>
|
|
<pre><code> [23 [cats] 4] size
|
|
------------------------
|
|
3</code></pre>
|
|
<hr />
|
|
<h2 id="section-21">/</h2>
|
|
<p>See <a href="#div">div</a>.</p>
|
|
<hr />
|
|
<h2 id="small">small</h2>
|
|
<p>Return <code>true</code> if the item on the top of the stack is a list with zero or one item in it, <code>false</code> if it is a list with more than one item in it, and an error if it is not a list.</p>
|
|
<h3 id="crosslinks-93">Crosslinks</h3>
|
|
<p><a href="#null">null</a></p>
|
|
<hr />
|
|
<h2 id="sort">sort</h2>
|
|
<p>Given a list return it sorted.</p>
|
|
<h3 id="example-24">Example</h3>
|
|
<pre><code> [4 2 5 7 1] sort
|
|
----------------------
|
|
[1 2 4 5 7]</code></pre>
|
|
<hr />
|
|
<h2 id="spiral_next">spiral_next</h2>
|
|
<p>Example code.</p>
|
|
<h3 id="discussion-54">Discussion</h3>
|
|
<p>See the <a href="https://joypy.osdn.io/notebooks/Square_Spiral.html">“Square Spiral Example Joy Code” notebook</a>.</p>
|
|
<hr />
|
|
<h2 id="split_at">split_at</h2>
|
|
<p>Split a list (second on the stack) at the position given by the number on the top of the stack.</p>
|
|
<h3 id="example-25">Example</h3>
|
|
<pre><code> [1 2 3 4 5 6 7] 4 split_at
|
|
--------------------------------
|
|
[5 6 7] [4 3 2 1]</code></pre>
|
|
<h3 id="discussion-55">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-94">Crosslinks</h3>
|
|
<p><a href="#split_list">split_list</a></p>
|
|
<hr />
|
|
<h2 id="split_list">split_list</h2>
|
|
<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>
|
|
<pre><code> [1 2 3 4 5 6 7] 4 split_list
|
|
----------------------------------
|
|
[1 2 3 4] [5 6 7]</code></pre>
|
|
<h3 id="discussion-56">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-95">Crosslinks</h3>
|
|
<p><a href="#split_at">split_at</a></p>
|
|
<hr />
|
|
<h2 id="sqr">sqr</h2>
|
|
<p>Square the number on the top of the stack.</p>
|
|
<pre><code> n sqr
|
|
------------
|
|
n²</code></pre>
|
|
<hr />
|
|
<h2 id="stackd">stackd</h2>
|
|
<p>Grab the stack under the top item and put it onto the stack.</p>
|
|
<h3 id="example-26">Example</h3>
|
|
<pre><code> ... 1 2 3 stackd
|
|
------------------------
|
|
... 1 2 [2 1 ...] 3</code></pre>
|
|
<h3 id="crosslinks-96">Crosslinks</h3>
|
|
<p><a href="#stack">stack</a></p>
|
|
<hr />
|
|
<h2 id="stack">stack</h2>
|
|
<p>Put the stack onto the stack.</p>
|
|
<pre><code> ... c b a stack
|
|
---------------------------
|
|
... c b a [a b c ...]</code></pre>
|
|
<h3 id="discussion-57">Discussion</h3>
|
|
<p>This function forms a pair with <a href="#unstack">unstack</a>, and together they form the complement to the “destructive” pair <a href="#enstacken">enstacken</a> and <a href="#disenstacken">disenstacken</a>.</p>
|
|
<h3 id="crosslinks-97">Crosslinks</h3>
|
|
<p><a href="#enstacken">enstacken</a> <a href="#disenstacken">disenstacken</a> <a href="#stackd">stackd</a> <a href="#unstack">unstack</a></p>
|
|
<hr />
|
|
<h2 id="step">step</h2>
|
|
<p>Combinator</p>
|
|
<p>Run a quoted program on each item in a sequence.</p>
|
|
<pre><code> ... [] [Q] step
|
|
---------------------
|
|
...
|
|
|
|
|
|
... [a] [Q] step
|
|
----------------------
|
|
... a Q
|
|
|
|
|
|
... [a b c] [Q] . step
|
|
----------------------------------------
|
|
... a . Q [b c] [Q] step</code></pre>
|
|
<h3 id="discussion-58">Discussion</h3>
|
|
<p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
|
|
<h3 id="crosslinks-98">Crosslinks</h3>
|
|
<p><a href="#step_zero">step_zero</a></p>
|
|
<hr />
|
|
<h2 id="step_zero">step_zero</h2>
|
|
<p>Combinator</p>
|
|
<p>Like <a href="#step">step</a> but with 0 as the initial value.</p>
|
|
<pre><code> [...] [F] step_zero
|
|
-------------------------
|
|
0 [...] [F] step</code></pre>
|
|
<h3 id="discussion-59">Discussion</h3>
|
|
<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>
|
|
<h3 id="crosslinks-99">Crosslinks</h3>
|
|
<p><a href="#step">step</a></p>
|
|
<hr />
|
|
<h2 id="stuncons">stuncons</h2>
|
|
<p>Take the <a href="#stack">stack</a> and <a href="#uncons">uncons</a> the top item.</p>
|
|
<h3 id="example-27">Example</h3>
|
|
<pre><code> 1 2 3 stuncons
|
|
--------------------
|
|
1 2 3 3 [2 1]</code></pre>
|
|
<hr />
|
|
<h2 id="stununcons">stununcons</h2>
|
|
<p>Take the <a href="#stack">stack</a> and <a href="#uncons">uncons</a> the top two items.</p>
|
|
<h3 id="example-28">Example</h3>
|
|
<pre><code> 1 2 3 stununcons
|
|
----------------------
|
|
1 2 3 3 2 [1]</code></pre>
|
|
<h3 id="crosslinks-100">Crosslinks</h3>
|
|
<p><a href="#stuncons">stuncons</a></p>
|
|
<hr />
|
|
<h2 id="sub">sub</h2>
|
|
<p>Subtract the number on the top of the stack from the number below it.</p>
|
|
<pre><code> a b sub
|
|
-------------
|
|
(a-b)</code></pre>
|
|
<h3 id="crosslinks-101">Crosslinks</h3>
|
|
<p><a href="#add">add</a></p>
|
|
<hr />
|
|
<h2 id="succ">succ</h2>
|
|
<p>Successor. Increment TOS.</p>
|
|
<h3 id="crosslinks-102">Crosslinks</h3>
|
|
<p><a href="#pred">pred</a></p>
|
|
<hr />
|
|
<h2 id="sum">sum</h2>
|
|
<p>Combinator</p>
|
|
<p>Given a quoted sequence of numbers return the sum.</p>
|
|
<h3 id="example-29">Example</h3>
|
|
<pre><code> [1 2 3 4 5] sum
|
|
---------------------
|
|
15</code></pre>
|
|
<h3 id="crosslinks-103">Crosslinks</h3>
|
|
<p><a href="#size">size</a></p>
|
|
<hr />
|
|
<h2 id="swaack">swaack</h2>
|
|
<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>
|
|
<h3 id="example-30">Example</h3>
|
|
<pre><code> 1 2 3 [4 5 6] swaack
|
|
--------------------------
|
|
6 5 4 [3 2 1]</code></pre>
|
|
<h3 id="discussion-60">Discussion</h3>
|
|
<p>This function works as a kind of “context switch”. It’s used in the definition of <a href="#infra">infra</a>.</p>
|
|
<h3 id="crosslinks-104">Crosslinks</h3>
|
|
<p><a href="#infra">infra</a></p>
|
|
<hr />
|
|
<h2 id="swapd">swapd</h2>
|
|
<p>Swap the second and third items on the stack.</p>
|
|
<pre><code> a b c swapd
|
|
-----------------
|
|
b a c</code></pre>
|
|
<h3 id="crosslinks-105">Crosslinks</h3>
|
|
<p><a href="#over">over</a> <a href="#tuck">tuck</a></p>
|
|
<hr />
|
|
<h2 id="swap">swap</h2>
|
|
<p>Swap the top two items on the stack.</p>
|
|
<pre><code> a b swap
|
|
--------------
|
|
b a</code></pre>
|
|
<h3 id="crosslinks-106">Crosslinks</h3>
|
|
<p><a href="#swapd">swapd</a></p>
|
|
<hr />
|
|
<h2 id="swoncat">swoncat</h2>
|
|
<p><a href="#concat">concat</a> two lists, but <a href="#swap">swap</a> the lists first.</p>
|
|
<h3 id="crosslinks-107">Crosslinks</h3>
|
|
<p><a href="#concat">concat</a></p>
|
|
<hr />
|
|
<h2 id="swons">swons</h2>
|
|
<p>Like <a href="#cons">cons</a> but <a href="#swap">swap</a> the item and list.</p>
|
|
<pre><code> [...] a swons
|
|
-------------------
|
|
[a ...]</code></pre>
|
|
<hr />
|
|
<h2 id="tailrec">tailrec</h2>
|
|
<p>Combinator</p>
|
|
<p>A specialization of the <a href="#genrec">genrec</a> combinator.</p>
|
|
<h3 id="discussion-61">Discussion</h3>
|
|
<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">“tail recursive” 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>
|
|
<p>See the <a href="https://joypy.osdn.io/notebooks/Recursion_Combinators.html">Recursion Combinators notebook</a>.</p>
|
|
<h3 id="crosslinks-108">Crosslinks</h3>
|
|
<p><a href="#genrec">genrec</a></p>
|
|
<hr />
|
|
<h2 id="take">take</h2>
|
|
<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>
|
|
<pre><code> [a b c d] 2 take
|
|
----------------------
|
|
[b a]</code></pre>
|
|
<hr />
|
|
<h2 id="ternary">ternary</h2>
|
|
<p>Combinator</p>
|
|
<p>Run a quoted program using exactly three stack values and leave the first item of the result on the stack.</p>
|
|
<pre><code> ... z y x [P] ternary
|
|
-------------------------
|
|
... a</code></pre>
|
|
<h3 id="discussion-62">Discussion</h3>
|
|
<p>Runs any other quoted function and returns its first result while consuming exactly three items from the stack.</p>
|
|
<h3 id="crosslinks-109">Crosslinks</h3>
|
|
<p><a href="#binary">binary</a> <a href="#nullary">nullary</a> <a href="#unary">unary</a></p>
|
|
<hr />
|
|
<h2 id="third">third</h2>
|
|
<pre><code> [a b c ...] third
|
|
-----------------------
|
|
c</code></pre>
|
|
<h3 id="crosslinks-110">Crosslinks</h3>
|
|
<p><a href="#first">first</a> <a href="#second">second</a> <a href="#fourth">fourth</a> <a href="#rest">rest</a></p>
|
|
<hr />
|
|
<h2 id="times">times</h2>
|
|
<p>Combinator</p>
|
|
<p>Expect a quoted program and an integer <code>n</code> on the stack and do the program <code>n</code> times.</p>
|
|
<pre><code> ... n [Q] . times
|
|
----------------------- w/ n <= 0
|
|
... .
|
|
|
|
... 1 [Q] . times
|
|
-----------------------
|
|
... . Q
|
|
|
|
... n [Q] . times
|
|
------------------------------------- w/ n > 1
|
|
... . Q (n-1) [Q] times</code></pre>
|
|
<h3 id="discussion-63">Discussion</h3>
|
|
<p>This works by building a little <a href="#while">while</a> program and running it:</p>
|
|
<pre><code> 1 3 [++] • [-- dip] cons [swap] infra [0 >] swap while pop
|
|
1 3 [++] [-- dip] • cons [swap] infra [0 >] swap while pop
|
|
1 3 [[++] -- dip] • [swap] infra [0 >] swap while pop
|
|
1 3 [[++] -- dip] [swap] • infra [0 >] swap while pop
|
|
dip -- [++] • swap [3 1] swaack [0 >] swap while pop
|
|
dip [++] -- • [3 1] swaack [0 >] swap while pop
|
|
dip [++] -- [3 1] • swaack [0 >] swap while pop
|
|
1 3 [-- [++] dip] • [0 >] swap while pop
|
|
1 3 [-- [++] dip] [0 >] • swap while pop
|
|
1 3 [0 >] [-- [++] dip] • while pop</code></pre>
|
|
<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>
|
|
<hr />
|
|
<h2 id="tuck">tuck</h2>
|
|
<p><a href="#dup">dup</a> the item on the top of the stack under the second item on the stack.</p>
|
|
<pre><code> a b tuck
|
|
--------------
|
|
b a b</code></pre>
|
|
<h3 id="crosslinks-111">Crosslinks</h3>
|
|
<p><a href="#over">over</a></p>
|
|
<hr />
|
|
<h2 id="unary">unary</h2>
|
|
<p>(Combinator)</p>
|
|
<p>Run a quoted program using exactly one stack value and leave the first item of the result on the stack.</p>
|
|
<pre><code> ... x [P] unary
|
|
---------------------
|
|
... a</code></pre>
|
|
<h3 id="discussion-64">Discussion</h3>
|
|
<p>Runs any other quoted function and returns its first result while consuming exactly one item from the stack.</p>
|
|
<h3 id="crosslinks-112">Crosslinks</h3>
|
|
<p><a href="#binary">binary</a> <a href="#nullary">nullary</a> <a href="#ternary">ternary</a></p>
|
|
<hr />
|
|
<h2 id="uncons">uncons</h2>
|
|
<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>
|
|
<pre><code> [a ...] uncons
|
|
--------------------
|
|
a [...]</code></pre>
|
|
<h3 id="discussion-65">Discussion</h3>
|
|
<p>This is the inverse of <a href="#cons">cons</a>.</p>
|
|
<h3 id="crosslinks-113">Crosslinks</h3>
|
|
<p><a href="#cons">cons</a></p>
|
|
<hr />
|
|
<h2 id="unique">unique</h2>
|
|
<p>Given a list remove duplicate items.</p>
|
|
<hr />
|
|
<h2 id="unit">unit</h2>
|
|
<pre><code> a unit
|
|
------------
|
|
[a]</code></pre>
|
|
<hr />
|
|
<h2 id="unstack">unstack</h2>
|
|
<p>Take a list from the top of the stack and <code>concat</code> it to the stack.</p>
|
|
<pre><code>joy? 1 2 3 [4 5 6]
|
|
|
|
1 2 3 [4 5 6]
|
|
|
|
joy? unstack
|
|
|
|
1 2 3 6 5 4</code></pre>
|
|
<h3 id="crosslinks-114">Crosslinks</h3>
|
|
<p><a href="#stack">stack</a> <a href="#disenstacken">disenstacken</a> <a href="#enstacken">enstacken</a></p>
|
|
<hr />
|
|
<h2 id="unquoted">unquoted</h2>
|
|
<p>Combinator</p>
|
|
<p>Unquote (using <a href="#i">i</a>) the list that is second on the stack.</p>
|
|
<h3 id="example-31">Example</h3>
|
|
<pre><code> 1 2 [3 4] 5 unquoted
|
|
--------------------------
|
|
1 2 3 4 5</code></pre>
|
|
<h3 id="crosslinks-115">Crosslinks</h3>
|
|
<p><a href="#unit">unit</a></p>
|
|
<hr />
|
|
<h2 id="unswons">unswons</h2>
|
|
<pre><code> [a ...] unswons
|
|
---------------------
|
|
[...] a</code></pre>
|
|
<hr />
|
|
<h2 id="section-22">||</h2>
|
|
<p>Combinator</p>
|
|
<p>Short-circuiting Boolean OR</p>
|
|
<h3 id="discussion-66">Discussion</h3>
|
|
<p>Accept two quoted programs, run the first and expect a Boolean value, if it’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>
|
|
<pre><code> [A] [B] ||
|
|
---------------- A -> false
|
|
B
|
|
|
|
|
|
[A] [B] ||
|
|
---------------- A -> true
|
|
true</code></pre>
|
|
<h3 id="crosslinks-116">Crosslinks</h3>
|
|
<p><a href="#and">and</a></p>
|
|
<hr />
|
|
<h2 id="void">void</h2>
|
|
<p>True if the form on TOS is void otherwise False.</p>
|
|
<h3 id="discussion-67">Discussion</h3>
|
|
<p>A form is any Joy expression composed solely of lists. This represents a binary Boolean logical formula in the arithmetic of the “Laws of Form”, see <a href="http://www.markability.net/">The Markable Mark</a></p>
|
|
<hr />
|
|
<h2 id="warranty">warranty</h2>
|
|
<p>Print warranty information.</p>
|
|
<hr />
|
|
<h2 id="while">while</h2>
|
|
<p>Combinator</p>
|
|
<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>
|
|
<pre><code> [P] [F] while
|
|
------------------- P -> false
|
|
|
|
[P] [F] while
|
|
--------------------- P -> true
|
|
F [P] [F] while</code></pre>
|
|
<h3 id="crosslinks-117">Crosslinks</h3>
|
|
<p><a href="#loop">loop</a></p>
|
|
<hr />
|
|
<h2 id="words">words</h2>
|
|
<p>Print all the words in alphabetical order.</p>
|
|
<h3 id="discussion-68">Discussion</h3>
|
|
<p>Mathematically this is a form of <a href="#id">id</a>.</p>
|
|
<h3 id="crosslinks-118">Crosslinks</h3>
|
|
<p><a href="#help">help</a></p>
|
|
<hr />
|
|
<h2 id="x">x</h2>
|
|
<p>Combinator</p>
|
|
<p>Take a quoted function <code>F</code> and run it with itself as the first item on the stack.</p>
|
|
<pre><code> [F] x
|
|
-----------
|
|
[F] F</code></pre>
|
|
<h3 id="discussion-69">Discussion</h3>
|
|
<p>The simplest recursive pattern.</p>
|
|
<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>
|
|
<hr />
|
|
<h2 id="zip">zip</h2>
|
|
<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>
|
|
<h3 id="example-32">Example</h3>
|
|
<pre><code> [1 2 3] [4 5 6] zip
|
|
-------------------------
|
|
[[1 4] [2 5] [3 6]]</code></pre>
|
|
</body>
|
|
</html>
|