From 9de71c84bfc5d6876afdce4b4c4e01bb1bde7b6b Mon Sep 17 00:00:00 2001
From: Simon Forman Version -10.0.0 Each function, combinator, or definition should be documented
-here. See and. Combinator Short-circuiting Boolean AND Accept two quoted programs, run the first and expect a Boolean
-value, if it's TODO: this is derived in one of the notebooks I think, look it
-up and link to it, or copy the content here. This is seldom useful, I suspect, but this way you have it. See mul. See id. See xor. See eq. See ne. Function Not negative. Return a Boolean value indicating if a number is greater than or
-equal to zero. See gt. See ge. See rshift. See sub. See pred. See lt. See le. See ne. Function Tuck an empty list just under the first item on the stack. See lshift. Function Tuck an empty list just under the first two items on the
-stack. See mod. See add. See succ. Function Is the item on the top of the stack "truthy"? You often want to test the truth value of an item on the stack
-without consuming the item. See floordiv. See floordiv. See floordiv. Combinator Short-circuiting Boolean OR Accept two quoted programs, run the first and expect a Boolean
-value, if it’s Function Return the absolute value of the argument. Basis Function Add two numbers together: a + b. Combinator Build a list of values from a generator program The [0 <=] [-- dup] anamorphism See the Recursion
-Combinators notebook. Basis Function Logical bit-wise AND. "apply one" Combinator 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. This is the same effect as the unary
-combinator. Just a specialization of Combinator Like app1 with two items. [[grba] [swap] [grba] [swap]] [dip] [[infrst]] [cons] [ii] Unlike app1, which is essentially an alias
-for unary, this function is not the same as
-binary. Instead of running one program using
-exactly two items from the stack and pushing one result (as
-binary 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. This is not currently implemented as parallel processes but it
-can (and should) be done. Combinator Like [app1] with three items. 3 [appN] See [app2]. Combinator Like [app1] with any number of items. [[grabN]] [codi] [map] [disenstacken] This function takes a quoted function See getitem. Function Compute the average of a list of numbers. (Currently broken
-until I can figure out what to do about "numeric tower" in
-Thun.) [[sum]] [[size]] [cleave] [/] 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? As an exercise in Functional Programming in Joy it would be fun
-to convert this into a catamorphism. See the Recursion
-Combinators notebook. Combinator Run two quoted programs [[i]] [dip] [i] This combinator may seem trivial but it comes in handy. Combinator Run a quoted program using exactly two stack values and leave
-the first item of the result on the stack. [unary] [popd] Runs any other quoted function and returns its first result
-while consuming exactly two items from the stack. Basis Function Convert the item on the top of the stack to a Boolean value. For integers 0 is [not] Basis Combinator Use a Boolean value to select and run one of two quoted
-programs. [rolldown] [choice] [i] This is one of the fundamental operations (although it can be
-defined in terms of [choice] as above). The more common
-"if..then..else" construct [ifte] adds a predicate function that is
-evaluated [nullary]. [choice] [ifte] [select] Function Do [cons] four times. [ccons] [ccons] [ccons] [cons] [times] Function Do [cons] two times. [cons] [cons] [cons] [ccons] Basis Function Use a Boolean value to select one of two items. [[pop]] [[popd]] [branch] It's a matter of taste whether you implement this in terms of
-[branch] or the other way around. [branch] [select] Basis Function Clear everything from the stack. [stack] [bool] [[pop] [stack] [bool]] [loop] [stack] [swaack] Combinator Run two programs in parallel, consuming one additional item, and
-put their results on the stack. [fork] [popdd] One of a handful of useful parallel combinators. [clop] [fork] [map] Combinator Run two programs in parallel, consuming two additional items,
-and put their results on the stack. [cleave] [popdd] Like [cleave] but consumes an additional item from the
-stack. [cleave] [fork] [map] Combinator 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. This is useful sometimes, and you can [dup] or [dupd] with two
-quoted programs to handle the cases when you just want to deal with
-[<=] or [>=] and not all three possibilities, e.g.: Or even: TODO: link to tree notebooks where this was used. Combinator Take a quoted program from the stack, [cons] the next item onto
-it, then [dip] the whole thing under what was the third item on the
-stack. [cons] [dip] This is one of those weirdly specific functions that turns out
-to be useful in a few places. [appN] [codireco] Combinator This is part of the [make_generator] function. You would not use
-this combinator directly. [codi] [reco] See [make_generator] and the
-"Using [make_generator] Function Concatinate two lists. [first] [firsttwo] [flatten] [fourth] [getitem] [remove]
-[rest] [reverse] [rrest] [second] [shift] [shunt] [size] [sort]
-[splitat] [split_list] [swaack] [third] [zip] Combinator 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 It works by rewriting into a chain of nested [ifte]{.title-ref}
-expressions, e.g.: [ifte] Basis Function Given an item and a list, append the item to the list to make a
-new list. Cons is a venerable old function
-from Lisp. Its inverse operation is [uncons]. [uncons] Combinator Specialist function (that means I forgot what it does and
-why.) [dip] [infrst] Basis Combinator The This along with [infra] are enough to update any datastructure.
-See the "Traversing
-Datastructures with Zippers" notebook. Note that the item that was on the top of the stack
-( [dipd] [dipdd] [dupdip] [dupdipd] [infra] Combinator Like [dip] but expects two items. See [dip]. [dip] [dipdd] [dupdip] [dupdipd] [infra] Combinator Like [dip] but expects three items. : See [dip]. [dip] [dipd] [dupdip] [dupdipd] [infra] Function The [[clear]] [dip] [reverse] unstack 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. [enstacken] [stack] unstack See floordiv. Function Invariant: [[floordiv]] [[mod]] [clop] Function Given a number greater than zero put all the Natural numbers
-(including zero) less than that onto the stack. [0 >] [[dup] [--]] [while] [range] Function Expects an integer and a quote on the stack and returns the
-quote with n items removed off the top. [[rest]] [times] [take] Basis Function "Dup"licate the top item on the stack. [dupd] [dupdd] [dupdip] [dupdipd] Function [dup] the second item down on the stack. [[dup]] [dip] [dup] [dupdd] [dupdip] [dupdipd] Function [dup] the third item down on the stack. [[dup]] [dipd] [dup] [dupd] [dupdip] [dupdipd] Combinator Apply a function [dupd] [dip] A very common and useful combinator. [dupdipd] Combinator Run a copy of program [dup] [dipd] [dupdip] Function Put the stack onto the stack replacing the contents of the
-stack. [stack] [[clear]] [dip] This is a destructive version of [stack]. See the note under
-[disenstacken] about the apparent but illusory reversal of the
-stack. [stack] [unstack] [disenstacken] Basis Function Compare the two items on the top of the stack for equality and
-replace them with a Boolean value. [cmp] [ge] [gt] [le] [lt] [ne] Function Replace a list with its first item. [uncons] [pop] [second] [third] [fourth] [rest] Function Replace a list with its first two items. [uncons] [first] [first] [second] [third] [fourth] [rest] Function Given a list of lists, concatinate them. [\<{}] [[concat]] [step] Note that only one "level" of lists is flattened. In the example
-above [concat] [first] [firsttwo] [fourth] [getitem] [remove]
-[rest] [reverse] [rrest] [second] [shift] [shunt] [size] [sort]
-[splitat] [split_list] [swaack] [third] [zip] Basis Function Return the largest integer \<= x. This function doesn't make sense (yet) to have because there are
-(as yet) only integers in the system. Basis Function I don't know why this is called "floor" div, I think it rounds
-its result down (not towards zero or up.) All the division commands need to be revisited when the "numeric
-tower" for Thun gets nailed down. [divmod] Combinator Run two quoted programs in parallel and replace them with their
-results. [[i]] [app2] The basic parallelism combinator, the two programs are run
-independently. [cleave] [clop] [map] Function Replace a list with its fourth item. [rest] [third] [first] [second] [third] [rest] Function Take two integers from the stack and replace them with their
-Greatest Common Denominator. true [[tuck] [mod] [dup] 0 [>]] [loop] [pop] Euclid's Algorithm Function Compiled GCD function. See [gcd]. [gcd] Basis Function Greater-than-or-equal-to comparison of two numbers. [cmp] [eq] [gt] [le] [lt] [ne] Combinator General Recursion
-Combinator. [[[genrec]] [ccccons]] [nullary] [swons] [concat] [ifte] (Note that this definition includes the See the Recursion
-Combinators notebook. From "Recursion
-Theory and Joy" by Manfred von Thun: "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." The way to design one of these is to fix your base case
- For example, given a (general recursive) function
- If the Just set the stack arguments in front, and figure out what
- Tail recursive functions are those where [anamorphism] [tailrec] [x] Function Expects an integer and a quote on the stack and returns the item
-at the nth position in the quote counting from 0. [drop] [first] If the number isn't a valid index into the quote
- [concat] [first] [firsttwo] [flatten] [fourth] [remove]
-[rest] [reverse] [rrest] [second] [shift] [shunt] [size] [sort]
-[splitat] [split_list] [swaack] [third] [zip] Function Expect a number on the top of the satck and put that many items
-from uner it onto a new list. [\<{}] [[cons]] [times] Function A weird function used in [app2] that does this: It grabs the stack under the top item, and substitutes it for
-the second item down on the stack. [[stack] [popd]] [dip] This function "grabs" an item from the stack along with a copy
-of the stack. It's part of the [app2] definition. [app2] Basis Function Greater-than comparison of two numbers. [cmp] [eq] [ge] [le] [lt] [ne] Function Accepts a quoted symbol on the top of the stack and prints its
-documentation. Technically this is equivalent to Function [[sqr]] [ii] [+] [sqrt] This is another function that has to wait on the numeric
-tower. [sqrt] Basis Combinator Append a quoted expression onto the pending expression. This is a fundamental combinator. It is used in all kinds of
-places. For example, the [x] combinator can be defined as Basis Function The identity function. Does nothing. It's kind of a mathematical thing, but it
-occasionally comes in handy. Combinator If-Then-Else combinator, a common and convenient specialization
-of [branch]. [[nullary]] [dipd] [swap] [branch] [branch] [loop] [while] Combinator Take a quoted program from the stack and run it twice, first
-under the top item, then again with the top item. [[dip]] [dupdip] [i] It's a little tricky to understand how this works so here's an
-example trace: In some cases (like the example above) this is the same effect
-as using [app2] but most of the time it's not: [app2] [b] Combinator 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.) [swons] [swaack] [[i]] [dip] [swaack] 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 Combinator Does [infra] and then extracts the [first] item from the
-resulting list. [infra] [first] 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. 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 Basis Function Less-Than-or-Equal-to comparison of the two items on the top of
-the stack, replacing them with a Boolean value. [cmp] [eq] [ge] [gt] [lt] [ne] Basis Combinator Expect a quoted program This, along with [branch] and [fork], is one of the four main
-combinators of all programming. The fourth, sequence, is implied by
-juxtaposition. That is to say, in Joy 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. Anyway, [branch], [fork], amd [loop] are the fundamental
-combinators in Joy. Just as [branch] has it's more common and
-convenient form [ifte], [loop] has [while]. [branch] [fork] [while] Basis Function [rshift] Basis Function Less-Than comparison of the two items on the top of the stack,
-replacing them with a Boolean value. [cmp] [eq] [ge] [gt] [le] [ne] Function Given an initial state value and a quoted generator function
-build a generator quote. And then: [[codireco]] [ccons] See the
-"Using [codireco] Combinator 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. This is a common operation in many languages. In Joy it can be a
-parallelism combinator due to the "pure" nature of the
-language. [app1] [app2] [app3] appN [fork] Basis Function Given a list find the maximum. [min] [size] [sum] Basis Function Given a list find the minimum. [max] [size] [sum] Basis Function Return the remainder of [divmod] [mul] See mod. Basis Function Multiply two numbers. [div] [product] Basis Function Not-Equal comparison of the two items on the top of the stack,
-replacing them with a Boolean value. [cmp] [eq] [ge] [gt] [le] [lt] Function Invert the sign of a number. 0 [swap] [-] Function Like [bool] but convert the item on the top of the stack to the
-inverse Boolean value. [bool] [true] [false] [branch] [bool] Function Take the item on the top of the stack and [cons] it onto
- [[nullary]] [cons] Helper function for [\|\|] and [&&]. [&&] [\|\|] Combinator Run a quoted program without using any stack values and leave
-the first item of the result on the stack. [[stack]] [dip] [infra] [first] A very useful function that runs any other quoted function and
-returns it's first result without disturbing the stack (under the
-quoted program.) Function Like [getitem] but [swap]s the order of arguments. [swap] [getitem] [getitem] Basis Function Logical bit-wise OR. [and] [xor] Function [dup] the second item on the stack There are many many ways to define this function. [swap] [tuck] [[pop]] [nullary] [[dup]] [dip] [swap] [unit] [dupdip] [unit] [dupdipd] [first] And so on... A fine old word from Forth. [tuck] Combinator Take a list of quoted functions from the stack and replace it
-with a list of the [first] results from running those functions (on
-copies of the rest of the stack.) [[i]] [map] A specialization of [map] that runs a list of functions in
-parallel (if the underlying [map] function is so implemented, of
-course.) [map] See getitem. Function Plus or minus. Replace two numbers with their sum and
-difference. [+] [-] [clop] Basis Function Pop the top item from the stack and discard it. [popd] [popdd] [popop] [popopd] [popopdd] [popopop] Function [pop] the second item down on the stack. [swap] [pop] [pop] [popdd] [popop] [popopd] [popopdd] [popopop] Function [pop] the third item on the stack. [rolldown] [pop] [pop] [popd] [popop] [popopd] [popopdd] [popopop] Function [pop] two items from the stack. [pop] [pop] [pop] [popd] [popdd] [popopd] [popopdd] [popopop] Function [pop] the second and third items from the stack. [rollup] [popop] [pop] [popd] [popdd] [popop] [popopdd] [popopop] Function [[popop]] [dipd] [pop] [popd] [popdd] [popop] [popopd] [popopop] Function [pop] three items from the stack. [pop] [popop] [pop] [popd] [popdd] [popop] [popopd] [popopdd] Basis Function Take two numbers Function Predecessor. Decrement TOS. 1 - [succ] Combinator From the "Overview
-of the language JOY" 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. 5 [1] [*] primrec 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. Simple and useful specialization of the [genrec] combinator from
-the original
-Joy system. [genrec] [tailrec] Function Just as [sum] sums a list of numbers, this function multiplies
-them together. 1 [swap] [[mul]] [step] Or, [1] [[mul]] [primrec] Function "Quote D" Wrap the second item on the stack in a list. [[unit]] [dip] This comes from the original Joy stuff. [unit] Function Expect a number [0 \<=] [1 - [dup]] [anamorphism] If [rangetozero] Function Take a number [unit] [[downtozero]] [infra] Note that the order is reversed compared to [range]. [downtozero] [range] Function Replace the first item in a list with the item under it. [rest] [cons] [codireco] [make_generator] See mod. See mod. Function 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. See the
-"Remove Function" notebook. Basis Function [first] [uncons] Function Reverse the list on the top of the stack. [\<{}] [shunt] Function [swapd] [swap] [rollup] Function [swap] [swapd] [rolldown] See rollup. See rolldown. Function Round a number to a given precision in decimal digits. Another one that won't make sense until the "numeric tower" is
-nailed down. Function [rest] [rest] [rest] Basis Function [lshift] Function Run a quoted program in a list. [\<{}] [infra] Function [rest] [first] [first] [third] [fourth] Basis Function Use a Boolean value to select one of two items from a sequence.
-: The sequence can contain more than two items but not fewer. [choice] Function Print redistribution information. Mathematically this is a form of [id], but it has the
-side-effect of printing out the GPL notice. [warranty] Function Move the top item from one list to another. [uncons] [[swons]] [dip] [shunt] Function Like [concat] but [reverse] the top list into the second. [[swons]] [step] This is more efficient than [concat] so prefer it if you don't
-need to preserve order. [concat] [reverse] [shift] Function Replace a list with its size. [[pop] [++]] [step_zero] Function Given a list return it sorted. Function Example code. See the "Square Spiral
-Example Joy Code" notebook. Function Split a list (second on the stack) at the position given by the
-number on the top of the stack. [[drop]] [[take]] [clop] Take a list and a number [split_list] Function Split a list (second on the stack) at the position given by the
-number on the top of the stack such that [concat] would reconstruct
-the original list. [[take] [reverse]] [[drop]] [clop] Compare with [split_at]. This function does extra work to ensure
-that [concat] would reconstruct the original list. [split_at] Function Square the number on the top of the stack. [dup] [mul] Basis Function Combinator Return the square root of the number a. Negative numbers return
-complex roots. Another "numeric tower" hatch... Function Put the stack onto the stack. [] [swaack] [dup] [swaack] [first] This function forms a pair with [unstack], and together they
-form the complement to the "destructive" pair [enstacken] and
-[disenstacken]. [unstack] [enstacken] [disenstacken] Function Grab the stack under the top item and put it onto the stack. [[stack]] [dip] Combinator Run a quoted program on each item in a sequence. See the Recursion
-Combinators notebook. [step_zero] Combinator Like [step] but with 0 as the initial value. 0 [roll>] [step] [size] and [sum] can both be defined in terms of this
-specialization of [step]. [step] Function Take the [stack] and [uncons] the top item. [stack] [uncons] Function Take the [stack] and [uncons] the top two items. [stack] [uncons] [uncons] [stuncons] Basis Function Subtract the number on the top of the stack from the number
-below it. [add] Function Successor. Increment TOS. 1 + [pred] Combinator Given a quoted sequence of numbers return the sum. [+] [step_zero] [size] Basis Function Swap stack. Take a list from the top of the stack, replace the
-stack with the list, and put the old stack onto it. This function works as a kind of "context switch". It's used in
-the definition of [infra]. [infra] Basis Function Swap the top two items on the stack. [swapd] Function Swap the second and third items on the stack. [[swap]] [dip] [over] [tuck] Function [concat] two lists, but [swap] the lists first. [swap] [concat] [concat] Function Like [cons] but [swap] the item and list. [swap] [cons] Combinator A specialization of the [genrec] combinator. [[i]] [genrec] Some recursive functions do not need to store additional data or
-pending actions per-call. These are called "tail recursive"
-functions. In Joy, they appear as [genrec] definitions that
-have [i] for the second half of their recursive branch. See the Recursion
-Combinators notebook. [genrec] Function Expects an integer [\<\<{}] [[shift]] [times] [pop] Combinator Run a quoted program using exactly three stack values and leave
-the first item of the result on the stack. [binary] [popd] Runs any other quoted function and returns its first result
-while consuming exactly three items from the stack. Function [rest] [second] [first] [second] [fourth] [rest] Combinator Expect a quoted program and an integer [-- dip] cons [swap] infra [0 >] swap while pop : This works by building a little [while] program and running
-it: 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. See bool. Function [dup] the item on the top of the stack under the second item on
-the stack. [dup] [[swap]] [dip] [over] (Combinator) Run a quoted program using exactly one stack value and leave the
-first item of the result on the stack. [nullary] [popd] Runs any other quoted function and returns its first result
-while consuming exactly one item from the stack. Basis Function Removes an item from a list and leaves it on the stack under the
-rest of the list. You cannot This is the inverse of [cons]. [cons] Function Given a list remove duplicate items. Function [] [cons] Combinator Unquote (using [i]) the list that is second on the stack. [[i]] [dip] [unit] Function [uncons] [swap] Basis Function True if the form on TOS is void otherwise False. 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 The
-Markable Mark Basis Function Print warranty information. Combinator A specialization of [loop] that accepts a quoted predicate
-program [swap] [nulco] [dupdipd] [concat] [loop] [loop] Basis Function Print all the words in alphabetical order. Mathematically this is a form of [id]. [help] Combinator Take a quoted function The simplest recursive pattern. See the Recursion
-Combinators notebook. as well as Recursion
-Theory and Joy by Manfred von Basis Function Logical bit-wise eXclusive OR. [and] [or] Function 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. Version -10.0.0 Each function, combinator, or definition should be documented here. See and. Combinator Short-circuiting Boolean AND Accept two quoted programs, run the first and expect a Boolean value, if it’s TODO: this is derived in one of the notebooks I think, look it up and link to it, or copy the content here. This is seldom useful, I suspect, but this way you have it. See mul. See id. See xor. See eq. See ne. Function Not negative. Return a Boolean value indicating if a number is greater than or equal to zero. See gt. See ge. See rshift. See sub. See pred. See lt. See le. See ne. Function Tuck an empty list just under the first item on the stack. See lshift. Function Tuck an empty list just under the first two items on the stack. See mod. See add. See succ. Function Is the item on the top of the stack “truthy”? You often want to test the truth value of an item on the stack without consuming the item. See floordiv. See floordiv. See floordiv. Combinator Short-circuiting Boolean OR Accept two quoted programs, run the first and expect a Boolean value, if it’s Function Return the absolute value of the argument. Basis Function Add two numbers together: a + b. Combinator Build a list of values from a generator program The [0 <=] [-- dup] anamorphism See the Recursion Combinators notebook. Basis Function Logical bit-wise AND. “apply one” Combinator 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. This is the same effect as the unary combinator. Just a specialization of Combinator Like app1 with two items. Unlike app1, which is essentially an alias for unary, this function is not the same as binary. Instead of running one program using exactly two items from the stack and pushing one result (as binary 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. This is not currently implemented as parallel processes but it can (and should) be done. Combinator Like app1 with three items. 3 appN See app2. Combinator Like app1 with any number of items. This function takes a quoted function See getitem. Function Compute the average of a list of numbers. (Currently broken until I can figure out what to do about “numeric tower” in Thun.) 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? As an exercise in Functional Programming in Joy it would be fun to convert this into a catamorphism. See the Recursion Combinators notebook. Combinator Run two quoted programs This combinator may seem trivial but it comes in handy. Combinator Run a quoted program using exactly two stack values and leave the first item of the result on the stack. Runs any other quoted function and returns its first result while consuming exactly two items from the stack. Basis Function Convert the item on the top of the stack to a Boolean value. For integers 0 is Basis Combinator Use a Boolean value to select and run one of two quoted programs. This is one of the fundamental operations (although it can be defined in terms of choice as above). The more common “if..then..else” construct ifte adds a predicate function that is evaluated nullary. Function Do cons four times. Function Do cons two times. Basis Function Use a Boolean value to select one of two items. It’s a matter of taste whether you implement this in terms of branch or the other way around. Basis Function Clear everything from the stack. Combinator Run two programs in parallel, consuming one additional item, and put their results on the stack. One of a handful of useful parallel combinators. Combinator Run two programs in parallel, consuming two additional items, and put their results on the stack. Like cleave but consumes an additional item from the stack. Combinator 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. This is useful sometimes, and you can dup or dupd with two quoted programs to handle the cases when you just want to deal with <= or >= and not all three possibilities, e.g.: Or even: TODO: link to tree notebooks where this was used. Combinator Take a quoted program from the stack, cons the next item onto it, then dip the whole thing under what was the third item on the stack. This is one of those weirdly specific functions that turns out to be useful in a few places. Combinator This is part of the make_generator function. You would not use this combinator directly. See make_generator and the “Using Function Concatinate two lists. first first_two flatten fourth getitem remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip Combinator 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 It works by rewriting into a chain of nested ifte expressions, e.g.: Basis Function Given an item and a list, append the item to the list to make a new list. Cons is a venerable old function from Lisp. Its inverse operation is uncons. Combinator Specialist function (that means I forgot what it does and why.) Basis Combinator The This along with infra are enough to update any datastructure. See the “Traversing Datastructures with Zippers” notebook. Note that the item that was on the top of the stack ( dipd dipdd dupdip dupdipd infra Combinator Like dip but expects two items. See dip. dip dipdd dupdip dupdipd infra Combinator Like dip but expects three items. : See dip. Function The 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. See floordiv. Function Invariant: Function Given a number greater than zero put all the Natural numbers (including zero) less than that onto the stack. Function Expects an integer and a quote on the stack and returns the quote with n items removed off the top. Basis Function “Dup”licate the top item on the stack. Function dup the second item down on the stack. Function dup the third item down on the stack. Combinator Apply a function A very common and useful combinator. Combinator Run a copy of program Function Put the stack onto the stack replacing the contents of the stack. This is a destructive version of stack. See the note under disenstacken about the apparent but illusory reversal of the stack. stack [unstack] disenstacken Basis Function Compare the two items on the top of the stack for equality and replace them with a Boolean value. Function Replace a list with its first item. Function Replace a list with its first two items. first second third fourth rest Function Given a list of lists, concatinate them. Note that only one “level” of lists is flattened. In the example above concat first first_two fourth getitem remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip Basis Function Return the largest integer <= x. This function doesn’t make sense (yet) to have because there are (as yet) only integers in the system. Basis Function I don’t know why this is called “floor” div, I think it rounds its result down (not towards zero or up.) All the division commands need to be revisited when the “numeric tower” for Thun gets nailed down. Combinator Run two quoted programs in parallel and replace them with their results. The basic parallelism combinator, the two programs are run independently. Function Replace a list with its fourth item. Function Take two integers from the stack and replace them with their Greatest Common Denominator. Euclid’s Algorithm Function Compiled GCD function. See gcd. Basis Function Greater-than-or-equal-to comparison of two numbers. Combinator General Recursion Combinator. (Note that this definition includes the See the Recursion Combinators notebook. From “Recursion Theory and Joy” by Manfred von Thun: “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.” The way to design one of these is to fix your base case For example, given a (general recursive) function If the Just set the stack arguments in front, and figure out what Tail recursive functions are those where Function Expects an integer and a quote on the stack and returns the item at the nth position in the quote counting from 0. If the number isn’t a valid index into the quote concat first first_two flatten fourth remove rest reverse rrest second shift shunt size sort split_at split_list swaack third zip Function Expect a number on the top of the stack and cons that many items from under it onto a new list. Function A weird function used in app2 that does this: It grabs the stack under the top item, and substitutes it for the second item down on the stack. This function “grabs” an item from the stack along with a copy of the stack. It’s part of the app2 definition. Basis Function Greater-than comparison of two numbers. Function Accepts a quoted symbol on the top of the stack and prints its documentation. Technically this is equivalent to Function This is another function that has to wait on the numeric tower. Basis Combinator Append a quoted expression onto the pending expression. This is a fundamental combinator. It is used in all kinds of places. For example, the x combinator can be defined as Basis Function The identity function. Does nothing. It’s kind of a mathematical thing, but it occasionally comes in handy. Combinator If-Then-Else combinator, a common and convenient specialization of branch. Combinator Take a quoted program from the stack and run it twice, first under the top item, then again with the top item. It’s a little tricky to understand how this works so here’s an example trace: In some cases (like the example above) this is the same effect as using app2 but most of the time it’s not: Combinator 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.) 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 Combinator Does infra and then extracts the first item from the resulting list. 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. 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 Basis Function Less-Than-or-Equal-to comparison of the two items on the top of the stack, replacing them with a Boolean value. Basis Combinator Expect a quoted program This, along with branch and fork, is one of the four main combinators of all programming. The fourth, sequence, is implied by juxtaposition. That is to say, in Joy 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. Anyway, branch, fork, amd loop are the fundamental combinators in Joy. Just as branch has it’s more common and convenient form ifte, loop has while. Basis Function Basis Function Less-Than comparison of the two items on the top of the stack, replacing them with a Boolean value. Function Given an initial state value and a quoted generator function build a generator quote. And then: See the “Using Combinator 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. This is a common operation in many languages. In Joy it can be a parallelism combinator due to the “pure” nature of the language. Basis Function Given a list find the maximum. Basis Function Given a list find the minimum. Basis Function Return the remainder of See mod. Basis Function Multiply two numbers. Basis Function Not-Equal comparison of the two items on the top of the stack, replacing them with a Boolean value. Function Invert the sign of a number. Function Like bool but convert the item on the top of the stack to the inverse Boolean value. Function Take the item on the top of the stack and cons it onto Helper function for || and &&. Combinator Run a quoted program without using any stack values and leave the first item of the result on the stack. A very useful function that runs any other quoted function and returns it’s first result without disturbing the stack (under the quoted program.) Function Like getitem but swaps the order of arguments. Basis Function Logical bit-wise OR. Function dup the second item on the stack There are many many ways to define this function. And so on… A fine old word from Forth. Combinator Take a list of quoted functions from the stack and replace it with a list of the first results from running those functions (on copies of the rest of the stack.) A specialization of map that runs a list of functions in parallel (if the underlying map function is so implemented, of course.) See getitem. Function Plus or minus. Replace two numbers with their sum and difference. [+] [-] clop Basis Function Pop the top item from the stack and discard it. popd popdd popop popopd popopdd popopop Function pop the second item down on the stack. pop popdd popop popopd popopdd popopop Function pop the third item on the stack. pop popd popop popopd popopdd popopop Function pop two items from the stack. pop popd popdd popopd popopdd popopop Function pop the second and third items from the stack. pop popd popdd popop popopdd popopop Function pop popd popdd popop popopd popopop Function pop three items from the stack. pop popd popdd popop popopd popopdd Basis Function Take two numbers Function Predecessor. Decrement TOS. 1 - Combinator From the “Overview of the language JOY” 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. 5 [1] [*] primrec 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. Simple and useful specialization of the genrec combinator from the original Joy system. Function Just as sum sums a list of numbers, this function multiplies them together. Or, Function “Quote D” Wrap the second item on the stack in a list. This comes from the original Joy stuff. Function Expect a number [0 <=] [1 - dup] anamorphism If Function Take a number Note that the order is reversed compared to range. Function Replace the first item in a list with the item under it. See mod. See mod. Function 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. See the “Remove Function” notebook. Basis Function Function Reverse the list on the top of the stack. Function Function See rollup. See rolldown. Function Round a number to a given precision in decimal digits. Another one that won’t make sense until the “numeric tower” is nailed down. Function Basis Function Function Run a quoted program in a list. Function Basis Function Use a Boolean value to select one of two items from a sequence. : The sequence can contain more than two items but not fewer. Function Print redistribution information. Mathematically this is a form of id, but it has the side-effect of printing out the GPL notice. Function Move the top item from one list to another. Function Like concat but reverse the top list into the second. This is more efficient than concat so prefer it if you don’t need to preserve order. Function Replace a list with its size. Function Given a list return it sorted. Function Example code. See the “Square Spiral Example Joy Code” notebook. Function Split a list (second on the stack) at the position given by the number on the top of the stack. Take a list and a number Function Split a list (second on the stack) at the position given by the number on the top of the stack such that concat would reconstruct the original list. Compare with split_at. This function does extra work to ensure that concat would reconstruct the original list. Function Square the number on the top of the stack. Basis Function Combinator Return the square root of the number a. Negative numbers return complex roots. Another “numeric tower” hatch… Function Put the stack onto the stack. This function forms a pair with [unstack], and together they form the complement to the “destructive” pair enstacken and disenstacken. [unstack] enstacken disenstacken Function Grab the stack under the top item and put it onto the stack. Combinator Run a quoted program on each item in a sequence. See the Recursion Combinators notebook. Combinator Like step but with 0 as the initial value. size and sum can both be defined in terms of this specialization of step. Function Take the stack and uncons the top item. Function Take the stack and uncons the top two items. Basis Function Subtract the number on the top of the stack from the number below it. Function Successor. Increment TOS. 1 + Combinator Given a quoted sequence of numbers return the sum. [+] step_zero Basis Function Swap stack. Take a list from the top of the stack, replace the stack with the list, and put the old stack onto it. This function works as a kind of “context switch”. It’s used in the definition of infra. Basis Function Swap the top two items on the stack. Function Swap the second and third items on the stack. Function concat two lists, but swap the lists first. Function Like cons but swap the item and list. Combinator A specialization of the genrec combinator. Some recursive functions do not need to store additional data or pending actions per-call. These are called “tail recursive” functions. In Joy, they appear as genrec definitions that have i for the second half of their recursive branch. See the Recursion Combinators notebook. Function Expects an integer Combinator Run a quoted program using exactly three stack values and leave the first item of the result on the stack. Runs any other quoted function and returns its first result while consuming exactly three items from the stack. Function Combinator Expect a quoted program and an integer [-- dip] cons [swap] infra [0 >] swap while pop : This works by building a little while program and running it: 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. See bool. Function dup the item on the top of the stack under the second item on the stack. (Combinator) Run a quoted program using exactly one stack value and leave the first item of the result on the stack. Runs any other quoted function and returns its first result while consuming exactly one item from the stack. Basis Function Removes an item from a list and leaves it on the stack under the rest of the list. You cannot This is the inverse of cons. Function Given a list remove duplicate items. Function [] cons Combinator Unquote (using i) the list that is second on the stack. Function Basis Function True if the form on TOS is void otherwise False. 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 The Markable Mark Basis Function Print warranty information. Combinator A specialization of loop that accepts a quoted predicate program Basis Function Print all the words in alphabetical order. Mathematically this is a form of id. Combinator Take a quoted function The simplest recursive pattern. See the Recursion Combinators notebook. as well as Recursion Theory and Joy by Manfred von Basis Function Logical bit-wise eXclusive OR. Function 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.
-&
-
-&&
-true pop it and run the second program
-(which should also return a Boolean value) otherwise pop the second
-program (leaving false on the stack.)
- [A] [B] &&
----------------- true
- B
-
-
- [A] [B] &&
----------------- false
- false
-Definition
-
-nulco [nullary [false]] dip branch
-Derivation
-Discussion
-Crosslinks
-
-
-*
-
-•
-
-^
-
-=
-
-!=
-
-!-
-
- n !-
------------ n < 0
- false
-
-
- n !-
----------- n >= 0
- true
-Definition
-
-0 \>=
-Discussion
-
->
-
->=
-
->>
-
--
-
---
-
-<
-
-<=
-
-<>
-
-\<{}
-
- ... a <{}
-----------------
- ... [] a
-Definition
-
-[] swap
-Discussion
-Crosslinks
-
-
-<<
-
-\<\<{}
-
- ... b a <{}
------------------
- ... [] b a
-Definition
-
-[] rollup
-Discussion
-Crosslinks
-
-
-%
-
-+
-
-++
-
-?
-Definition
-
-
-
-Discussion
-Crosslinks
-
-
-/
-
-//
-
-/floor
-
-\|\|
-Definition
-
-
-
-Discussion
-false pop it and run the second program
-(which should also return a Boolean value) otherwise pop the second
-program (leaving true on the stack.)
- [A] [B] ||
----------------- A -> false
- B
-
-
- [A] [B] ||
----------------- A -> true
- true
-Crosslinks
-
-
-abs
-Definition
-
-
-
-
-add
-
-anamorphism
-G
-and a stopping predicate P.
- [P] [G] anamorphism
------------------------------------------
- [P] [pop []] [G] [dip swons] genrec
-Definition
-
-
-
-Example
-range function generates a list of the integers
-from 0 to n - 1:
-
-Discussion
-
-and
-Crosslinks
-
-
-app1
-
- ... x [Q] app1
----------------------------------
- ... [x ...] [Q] infra first
-Definition
-
-
-
-Discussion
-nullary really. Its
-parallelizable cousins are more useful.Crosslinks
-
-
-app2
-
- ... y x [Q] . app2
------------------------------------
- ... [y ...] [Q] . infra first
- [x ...] [Q] infra first
-Definition
-
-
-Discussion
-Crosslinks
-
-
-app3
-
- ... z y x [Q] . app3
------------------------------------
- ... [z ...] [Q] . infra first
- [y ...] [Q] infra first
- [x ...] [Q] infra first
-Definition
-
-
-Discussion
-Crosslinks
-
-
-appN
-
- ... xN ... x2 x1 x0 [Q] n . appN
---------------------------------------
- ... [xN ...] [Q] . infra first
- ...
- [x2 ...] [Q] infra first
- [x1 ...] [Q] infra first
- [x0 ...] [Q] infra first
-Definition
-
-
-Discussion
-Q and an
-integer and runs the function that many times on that many stack
-items. See also [app2].Crosslinks
-
-
-at
-
-average
-Definition
-
-
-Discussion
-
-b
-
- [P] [Q] b
----------------
- P Q
-Definition
-
-
-Discussion
-Crosslinks
-
-
-binary
-
- ... y x [P] binary
------------------------
- ... a
-Definition
-
-
-Discussion
-Crosslinks
-
-
-bool
-Discussion
-false and any other number is
-true; for lists the empty list is false
-and all other lists are true.Crosslinks
-
-branch
-
- false [F] [T] branch
---------------------------
- F
-
- true [F] [T] branch
--------------------------
- T
-Definition
-
-
-Discussion
-Crosslinks
-
-ccccons
-
- a b c d [...] ccccons
----------------------------
- [a b c d ...]
-Definition
-
-
-Crosslinks
-
-ccons
-
- a b [...] ccons
----------------------
- [a b ...]
-Definition
-
-
-Crosslinks
-
-choice
-
- a b false choice
-----------------------
- a
-
- a b true choice
----------------------
- b
-Definition
-
-
-Discussion
-Crosslinks
-
-clear
-Definition
-
-
-Crosslinks
-
-cleave
-
- ... x [A] [B] cleave
-------------------------
- ... a b
-Derivation
-
-
-Example
-
- 1 2 3 [+] [-] cleave
---------------------------
- 1 2 5 -1
-Discussion
-Crosslinks
-
-clop
-
- ... x y [A] [B] clop
---------------------------
- ... a b
-Definition
-
-
-Discussion
-
- 1 2 3 4 [+] [-] clop
---------------------------
- 1 2 7 -1
-Crosslinks
-
-cmp
-
- 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
-Discussion
-
-[G] [EL] dup cmp
-
-[GE] [L] dupd cmp
-
-[GL] [E] over cmp
-Crosslinks
-
-codi
-
- a b [F] . codi
---------------------
- b . F a
-Definition
-
-
-Discussion
-Crosslinks
-
-codireco
-Definition
-
-
-Discussion
-x to Generate Values" notebook as well as
-Recursion
-Theory and Joy by Manfred von Thun.Crosslinks
-
-concat
-
- [a b c] [d e f] concat
-----------------------------
- [a b c d e f]
-Crosslinks
-
-cond
-true. If no predicates return
-true the default function runs.
-[
- [ [Predicate0] Function0 ]
- [ [Predicate1] Function1 ]
- ...
- [ [PredicateN] FunctionN ]
- [Default]
-]
-cond
-Discussion
-
- [[[B0] T0] [[B1] T1] [D]] cond
------------------------------------------
- [B0] [T0] [[B1] [T1] [D] ifte] ifte
-Crosslinks
-
-cons
-
- a [...] cons
-------------------
- [a ...]
-Discussion
-Crosslinks
-
-dinfrirst
-Definition
-
-
-
-dip
-dip 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.
- ... x [Q] . dip
----------------------
- ... . Q x
-Discussion
-x 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
-dip and infra to operate on compound
-datastructures with atoms in them. This is a kind of side-effect of
-the Continuation-Passing Style. The dip combinator
-could "set aside" the item and replace it after running
-Q but that means that there is an "extra space" where
-the item resides while Q 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.)Crosslinks
-
-dipd
-
- ... y x [Q] . dipd
--------------------------
- ... . Q y x
-Discussion
-Crosslinks
-
-dipdd
-
- ... z y x [Q] . dip
------------------------------
- ... . Q z y x
-Discussion
-Crosslinks
-
-disenstacken
-disenstacken function expects a list on top of
-the stack and makes that the stack discarding the rest of the
-stack.
- 1 2 3 [4 5 6] disenstacken
---------------------------------
- 6 5 4
-Definition
-
-
-Discussion
-Crosslinks
-
-div
-
-divmod
-
- x y divmod
-------------------
- q r
- (x/y) (x%y)
-qy + r = x.Definition
-
-
-
-downtozero
-Example
-
- 3 down_to_zero
---------------------
- 3 2 1 0
-Definition
-
-
-Crosslinks
-
-drop
-Example
-
- [a b c d] 2 drop
-----------------------
- [c d]
-Definition
-
-
-Crosslinks
-
-dup
-
- a dup
------------
- a a
-Crosslinks
-
-dupd
-
- a b dupd
---------------
- a a b
-Definition
-
-
-Crosslinks
-
-dupdd
-
- a b c dupdd
------------------
- a a b c
-Definition
-
-
-Crosslinks
-
-dupdip
-F and [dup] the item under it on
-the stack.
- a [F] dupdip
-------------------
- a F a
-Definition
-
-
-Derivation
-
-a [F] dupdip
-a [F] dupd dip
-a [F] [dup] dip dip
-a dup [F] dip
-a a [F] dip
-a F a
-Discussion
-Crosslinks
-
-dupdipd
-F under the next item down on
-the stack.
- a [F] dupdipd
--------------------
- F a [F]
-Definition
-
-
-Crosslinks
-
-enstacken
-
- ... a b c enstacken
--------------------------
- [c b a ...]
-Definition
-
-
-Discussion
-Crosslinks
-
-eq
-
- a b eq
--------------
- Boolean
- (a = b)
-Crosslinks
-
-first
-
- [a ...]
---------------
- a
-Definition
-
-
-Crosslinks
-
-first_two
-
- [a b ...] first_two
--------------------------
- a b
-Definition
-
-
-Crosslinks
-
-flatten
-Example
-
- [[1 2] [3 [4] 5] [6 7]] flatten
--------------------------------------
- [1 2 3 [4] 5 6 7]
-Definition
-
-
-Discussion
-[4] is not unquoted.Crosslinks
-
-floor
-Discussion
-
-floordiv
-
- a b floordiv
-------------------
- (a/b)
-Discussion
-Crosslinks
-
-fork
-
- ... [F] [G] fork
-----------------------
- ... f g
-Definition
-
-
-Discussion
-Crosslinks
-
-fourth
-
- [a b c d ...] fourth
---------------------------
- d
-Definition
-
-
-Crosslinks
-
-gcd
-Definition
-
-
-Discussion
-
-gcd2
-Discussion
-Crosslinks
-
-ge
-
- a b ge
---------------
- Boolean
- (a >= b)
-Crosslinks
-
-genrec
-
- [if] [then] [rec1] [rec2] genrec
----------------------------------------------------------------------
- [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifte
-Definition
-
-
-genrec
-symbol itself, it is self-referential. This is possible because the
-definition machinery does not check that symbols in defs are in the
-dictionary. genrec is the only self-referential
-definition.)Discussion
-
-
-[then] and the test [if], and then treat
-rec1 and rec2 as an else-part
-"sandwiching" a quotation of the whole function.F:
-F == [I] [T] [R1] [R2] genrec
-[I] if-part fails you must derive
-R1 and R2 from: :
-... R1 [F] R2
-R1 and R2 have to do to apply the quoted
-[F] in the proper way. In effect, the
-genrec combinator turns into an [ifte] combinator with
-a quoted copy of the original definition in the else-part:
-F == [I] [T] [R1] [R2] genrec
- == [I] [T] [R1 [F] R2] ifte
-R2 is the
-i combinator:
-P == [I] [T] [R] tailrec
- == [I] [T] [R [P] i] ifte
- == [I] [T] [R P] ifte
-Crosslinks
-
-getitem
-Example
-
- [a b c d] 2 getitem
--------------------------
- c
-Definition
-
-
-Discussion
-getitem will cause some sort of problem (the exact
-nature of which is implementation-dependant.)Crosslinks
-
-grabN
-Example
-
- a b c d e 3 grabN
------------------------
- a b [c d e]
-Definition
-
-
-
-grba
-
- ... 1 2 3 4 5 grba
--------------------------------
- ... 1 2 3 [4 3 2 1 ...] 5
-Definition
-
-
-Discussion
-Crosslinks
-
-gt
-
- a b gt
---------------
- Boolean
- (a > b)
-Crosslinks
-
-help
-
- [foo] help
-----------------
-Discussion
-pop, but it will
-only work if the item on the top of the stack is a quoted
-symbol.
-hypot
-
- x y hypot
----------------------------
- sqrt(sqr(x) + sqr(y))
-Definition
-
-
-Discussion
-Crosslinks
-
-i
-
- [Q] . i
--------------
- . Q
-Discussion
-dup
-i.
-id
-Discussion
-
-ifte
-
- [if] [then] [else] ifte
----------------------------------------
- [if] nullary [else] [then] branch
-Definition
-
-
-Crosslinks
-
-ii
-
-... a [Q] ii
-------------------
- ... Q a Q
-Definition
-
-
-Example
-
- 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 •
-Discussion
-
- 1 2 3 4 [+] ii
---------------------
- 1 9
-
- 1 2 3 4 [+] app2
-----------------------
- 1 2 5 6
-Crosslinks
-
-infra
-
- ... x y z [a b c] [Q] infra
----------------------------------
- c b a Q [z y x ...] swaack
-Definition
-
-
-Discussion
-infra lets you work on its internal
-structure.Crosslinks
-
-
-infrst
-Definition
-
-
-
-inscribe
-Example
-
-[sqr dup mul] inscribe
-Discussion
-defs.txt file. It can be
-abused, which you should avoid unless you know what you're
-doing.
-le
-
- a b le
--------------
- Boolean
- (a <= b)
-Crosslinks
-
-loop
-Q and a Boolean value on
-the stack. If the value is false discard the quoted program,
-otherwise run a copy of Q and loop
-again.
- false [Q] loop
---------------------
-
-
- true [Q] . loop
---------------------------
- . Q [Q] loop
-Discussion
-F G is like
-G(F(...)) in a language bassed on function
-application. Or again, to quote the Joy
-Wikipedia entry#Mathematical_purity),
-
-Crosslinks
-
-lshift
-
- a n lshift
-----------------
- (a×2ⁿ)
-Crosslinks
-
-lt
-
- a b lt
--------------
- Boolean
- (a < b)
-Crosslinks
-
-make_generator
-
- state [generator function] make_generator
------------------------------------------------
- [state [generator function] codireco]
-Example
-
- 230 [dup ++] make_generator
----------------------------------
- [230 [dup ++] codireco]
-
- [230 [dup ++] codireco] 5 [x] times pop
----------------------------------------------
- 230 231 232 233 234
-Definition
-
-
-Discussion
-x to Generate Values" notebook.Crosslinks
-
-map
-Example
-
- 5 [1 2 3] [++ *] map
---------------------------
- 5 [10 15 20]
-Discussion
-Crosslinks
-
-max
-Example
-
- [1 2 3 4] max
--------------------
- 4
-Crosslinks
-
-min
-Example
-
- [1 2 3 4] min
--------------------
- 1
-Crosslinks
-
-mod
-a divided by
-b.
- a b mod
--------------
- (a%b)
-Crosslinks
-
-modulus
-
-mul
-
- a b mul
--------------
- (a×b)
-Crosslinks
-
-ne
-
- a b ne
--------------
- Boolean
- (a = b)
-Crosslinks
-
-neg
-
- a neg
------------
- -a
-Definition
-
-
-
-not
-
- true not
---------------
- false
-
- false not
----------------
- true
-Definition
-
-
-Crosslinks
-
-nulco
-[nullary].
- [F] nulco
--------------------
- [[F] nullary]
-Definition
-
-
-Discussion
-Crosslinks
-
-nullary
-
- ... [P] nullary
----------------------
- ... a
-Definition
-
-
-Example
-
-... [P] nullary
-... [P] [stack] dip infra first
-... stack [P] infra first
-... [...] [P] infra first
-... [a ...] first
-... a
-Discussion
-Crosslinks
-
-
-of
-Example
-
- 2 [a b c d] of
---------------------
- c
-Definition
-
-
-Crosslinks
-
-or
-Crosslinks
-
-over
-over the
-first.
- a b over
---------------
- a b a
-Definition
-
-
-Discussion
-Crosslinks
-
-pam
-Example
-
- 5 7 [[+][-][*][/][%]] pam
--------------------------------
- 5 7 [12 -2 35 0 5]
-Definition
-
-
-Discussion
-Crosslinks
-
-pick
-
-pm
-
- a b pm
------------------
- (a+b) (a-b)
-Definition
-
-
-
-pop
-
- a pop
------------
-Crosslinks
-
-popd
-
- a b popd
---------------
- b
-Definition
-
-
-Crosslinks
-
-popdd
-
- a b c popdd
------------------
- b c
-Definition
-
-
-Crosslinks
-
-popop
-
- a b popop
----------------
-Definition
-
-
-Crosslinks
-
-popopd
-
- a b c popopd
-------------------
- c
-Definition
-
-
-Crosslinks
-
-popopdd
-
- a b c d popopdd
----------------------
- c d
-Definition
-
-
-Crosslinks
-
-popopop
-
- a b c popopop
--------------------
-Definition
-
-
-Crosslinks
-
-pow
-a and b from the
-stack and raise a to the nth power.
-(b is on the top of the stack.)
- a n pow
--------------
- (aⁿ)
-Example
-
- 2 [2 3 4 5 6 7 8 9] [pow] map
------------------------------------
- 2 [4 8 16 32 64 128 256 512]
-
-pred
-Definition
-
-
-Crosslinks
-
-primrec
-
-
-
- 0 [Base] [Recur] primrec
-------------------------------
- Base
-
- n [Base] [Recur] primrec
------------------------------------------- n > 0
- n (n-1) [Base] [Recur] primrec Recur
-Discussion
-Crosslinks
-
-product
-Definition
-
-
-
-
-
-quoted
-
- a b quoted
-----------------
- [a] b
-Definition
-
-
-Discussion
-Crosslinks
-
-range
-n on the stack and replace it with
-a list: [(n-1)...0].Example
-
- 5 range
------------------
- [4 3 2 1 0]
-
- -5 range
---------------
- []
-Definition
-
-
-Discussion
-n is less than 1 the resulting list is
-empty.Crosslinks
-
-rangetozero
-n from the stack and replace it with
-a list [0...n].Example
-
- 5 range_to_zero
----------------------
- [0 1 2 3 4 5]
-Definition
-
-
-Discussion
-Crosslinks
-
-reco
-
- a [b ...] reco
---------------------
- [a ...]
-Definition
-
-
-Crosslinks
-
-rem
-
-remainder
-
-remove
-
- [1 2 3 1] 1 remove
-------------------------
- [2 3 1]
-Definition
-
-rest
-
- [a ...] rest
-------------------
- [...]
-Crosslinks
-
-reverse
-Example
-
- [1 2 3] reverse
----------------------
- [3 2 1]
-Definition
-
-
-
-rolldown
-
- a b c rolldown
---------------------
- b c a
-Definition
-
-
-Crosslinks
-
-rollup
-
- a b c rollup
-------------------
- c a b
-Definition
-
-
-Crosslinks
-
-roll>
-
-roll<
-
-round
-Discussion
-
-rrest
-
- [a b ...] rrest
----------------------
- [...]
-Definition
-
-
-Crosslinks
-
-rshift
-
- a n rshift
-----------------
- (a∕2ⁿ)
-Crosslinks
-
-run
-Example
-
- [1 2 +] run
------------------
- [3]
-Definition
-
-
-
-second
-
- [a b ...] second
-----------------------
- b
-Definition
-
-
-Crosslinks
-
-select
-
- [a b] false select
-------------------------
- a
-
- [a b] true select
------------------------
- b
-Discussion
-Crosslinks
-
-sharing
-Discussion
-Crosslinks
-
-shift
-Example
-
- [x y z] [a b c] shift
----------------------------
- [a x y z] [b c]
-Definition
-
-
-Crosslinks
-
-shunt
-Example
-
- [a b c] [d e f] shunt
----------------------------
- [f e d a b c]
-Definition
-
-
-Discussion
-Crosslinks
-
-size
-Example
-
- [23 [cats] 4] size
-------------------------
- 3
-Definition
-
-
-
-sort
-Example
-
- [4 2 5 7 1] sort
-----------------------
- [1 2 4 5 7]
-
-spiral_next
-Discussion
-
-split_at
-Example
-
- [1 2 3 4 5 6 7] 4 split_at
---------------------------------
- [5 6 7] [4 3 2 1]
-Definition
-
-
-Discussion
-n from the stack, take
-n items from the top of the list and [shunt] them onto
-a new list that replaces the number n on the top of
-the stack.Crosslinks
-
-split_list
-
- [1 2 3 4 5 6 7] 4 split_list
-----------------------------------
- [1 2 3 4] [5 6 7]
-Definition
-
-
-Discussion
-Crosslinks
-
-sqr
-
- n sqr
-------------
- n²
-Definition
-
-
-
-sqrt
-Discussion
-
-stack
-
- ... c b a stack
----------------------------
- ... c b a [a b c ...]
-Definition
-
-
-Discussion
-Crosslinks
-
-stackd
-Example
-
- ... 1 2 3 stackd
-------------------------
- ... 1 2 [2 1 ...] 3
-Definition
-
-
-
-step
-
- ... [] [Q] step
----------------------
- ...
-
-
- ... [a] [Q] step
-----------------------
- ... a Q
-
-
- ... [a b c] [Q] . step
-----------------------------------------
- ... a . Q [b c] [Q] step
-Discussion
-Crosslinks
-
-step_zero
-
- [...] [F] step_zero
--------------------------
- 0 [...] [F] step
-Definition
-
-
-Discussion
-Crosslinks
-
-stuncons
-Example
-
- 1 2 3 stuncons
---------------------
- 1 2 3 3 [2 1]
-Definition
-
-
-
-stununcons
-Example
-
- 1 2 3 stununcons
-----------------------
- 1 2 3 3 2 [1]
-Definition
-
-
-Crosslinks
-
-sub
-
- a b sub
--------------
- (a-b)
-Crosslinks
-
-succ
-Definition
-
-
-Crosslinks
-
-sum
-Example
-
- [1 2 3 4 5] sum
----------------------
- 15
-Definition
-
-
-Crosslinks
-
-swaack
-Example
-
- 1 2 3 [4 5 6] swaack
---------------------------
- 6 5 4 [3 2 1]
-Discussion
-Crosslinks
-
-swap
-
- a b swap
---------------
- b a
-Crosslinks
-
-swapd
-
- a b c swapd
------------------
- b a c
-Definition
-
-
-Crosslinks
-
-swoncat
-Definition
-
-
-Crosslinks
-
-swons
-
- [...] a swons
--------------------
- [a ...]
-Definition
-
-
-
-tailrec
-Definition
-
-
-Discussion
-Crosslinks
-
-take
-n and a list on the stack and
-replace them with a list with just the top n items in
-reverse order.
- [a b c d] 2 take
-----------------------
- [b a]
-Definition
-
-
-
-ternary
-
- ... z y x [P] ternary
--------------------------
- ... a
-Definition
-
-
-Discussion
-Crosslinks
-
-
-third
-
- [a b c ...] third
------------------------
- c
-Definition
-
-
-Crosslinks
-
-times
-n on the
-stack and do the program n times.
- ... n [Q] . times
------------------------ w/ n <= 0
- ... .
-
- ... 1 [Q] . times
------------------------
- ... . Q
-
- ... n [Q] . times
-------------------------------------- w/ n > 1
- ... . Q (n-1) [Q] times
-Definition
-
-
-Discussion
-
- 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
-
-truthy
-
-tuck
-
- a b tuck
---------------
- b a b
-Definition
-
-
-Crosslinks
-
-unary
-
- ... x [P] unary
----------------------
- ... a
-Definition
-
-
-Discussion
-Crosslinks
-
-
-uncons
-uncons an item from an
-empty list.
- [a ...] uncons
---------------------
- a [...]
-Discussion
-Crosslinks
-
-unique
-
-unit
-
- a unit
-------------
- [a]
-Definition
-
-
-
-unquoted
-Example
-
- 1 2 [3 4] 5 unquoted
---------------------------
- 1 2 3 4 5
-Definition
-
-
-Crosslinks
-
-unswons
-
- [a ...] unswons
----------------------
- [...] a
-Definition
-
-
-
-void
-Discussion
-
-warranty
-
-while
-P and runs it [nullary].
- [P] [F] while
-------------------- P -> false
-
- [P] [F] while
---------------------- P -> true
- F [P] [F] while
-Definition
-
-
-Crosslinks
-
-words
-Discussion
-Crosslinks
-
-x
-F and run it with itself as
-the first item on the stack.
- [F] x
------------
- [F] F
-Definition
-
-dup i
-Discussion
-
-xor
-Crosslinks
-
-zip
-Example
-
-
-
diff --git a/docs/html/FuncRef.html b/docs/html/FuncRef.html
new file mode 100644
index 0000000..9d7ed40
--- /dev/null
+++ b/docs/html/FuncRef.html
@@ -0,0 +1,2501 @@
+
+
+
+
+
+
+ [1 2 3] [4 5 6] zip
--------------------------
- [[4 1] [5 2] [6 3]]
-Function Reference
+
+&
+
+&&
+true pop it and run the second program (which should also return a Boolean value) otherwise pop the second program (leaving false on the stack.)
+ [A] [B] &&
+---------------- true
+ B
+
+
+ [A] [B] &&
+---------------- false
+ falseDefinition
+
+nulco [nullary [false]] dip branchDerivation
+Discussion
+Crosslinks
+
+
+*
+
+•
+
+^
+
+=
+
+!=
+
+!-
+
+ n !-
+----------- n < 0
+ false
+
+
+ n !-
+---------- n >= 0
+ trueDefinition
+
+0 \>=Discussion
+
+>
+
+>=
+
+>>
+
+-
+
+–
+
+<
+
+<=
+
+<>
+
+<{}
+
+ ... a <{}
+----------------
+ ... [] aDefinition
+
+[] swapDiscussion
+Crosslinks
+
+
+<<
+
+<<{}
+
+ ... b a <{}
+-----------------
+ ... [] b aDefinition
+
+[] rollupDiscussion
+Crosslinks
+
+
+%
+
++
+
+++
+
+?
+Definition
+
+
+
+Discussion
+Crosslinks
+
+
+/
+
+//
+
+/floor
+
+||
+Definition
+
+
+
+Discussion
+false pop it and run the second program (which should also return a Boolean value) otherwise pop the second program (leaving true on the stack.)
+ [A] [B] ||
+---------------- A -> false
+ B
+
+
+ [A] [B] ||
+---------------- A -> true
+ trueCrosslinks
+
+
+abs
+Definition
+
+
+
+
+add
+
+anamorphism
+G and a stopping predicate P.
+ [P] [G] anamorphism
+-----------------------------------------
+ [P] [pop []] [G] [dip swons] genrecDefinition
+
+
+
+Example
+range function generates a list of the integers from 0 to n - 1:
+
+Discussion
+
+and
+Crosslinks
+
+
+app1
+
+ ... x [Q] app1
+---------------------------------
+ ... [x ...] [Q] infra firstDefinition
+
+
+
+Discussion
+nullary really. Its parallelizable cousins are more useful.Crosslinks
+
+
+app2
+
+ ... y x [Q] . app2
+-----------------------------------
+ ... [y ...] [Q] . infra first
+ [x ...] [Q] infra firstDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+app3
+
+ ... z y x [Q] . app3
+-----------------------------------
+ ... [z ...] [Q] . infra first
+ [y ...] [Q] infra first
+ [x ...] [Q] infra firstDefinition
+
+
+Discussion
+Crosslinks
+
+
+appN
+
+ ... xN ... x2 x1 x0 [Q] n . appN
+--------------------------------------
+ ... [xN ...] [Q] . infra first
+ ...
+ [x2 ...] [Q] infra first
+ [x1 ...] [Q] infra first
+ [x0 ...] [Q] infra firstDefinition
+
+
+
+Discussion
+Q and an integer and runs the function that many times on that many stack items. See also app2.Crosslinks
+
+
+at
+
+average
+Definition
+
+
+
+Discussion
+
+b
+
+ [P] [Q] b
+---------------
+ P QDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+binary
+
+ ... y x [P] binary
+-----------------------
+ ... aDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+bool
+Discussion
+false and any other number is true; for lists the empty list is false and all other lists are true.Crosslinks
+
+
+branch
+
+ false [F] [T] branch
+--------------------------
+ F
+
+ true [F] [T] branch
+-------------------------
+ TDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+ccccons
+
+ a b c d [...] ccccons
+---------------------------
+ [a b c d ...]Definition
+
+
+
+Crosslinks
+
+
+ccons
+
+ a b [...] ccons
+---------------------
+ [a b ...]Definition
+
+
+
+Crosslinks
+
+
+choice
+
+ a b false choice
+----------------------
+ a
+
+ a b true choice
+---------------------
+ bDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+clear
+Definition
+
+
+
+Crosslinks
+
+
+cleave
+
+ ... x [A] [B] cleave
+------------------------
+ ... a bDerivation
+
+
+
+Example
+
+ 1 2 3 [+] [-] cleave
+--------------------------
+ 1 2 5 -1Discussion
+Crosslinks
+
+
+clop
+
+ ... x y [A] [B] clop
+--------------------------
+ ... a bDefinition
+
+
+
+Discussion
+
+ 1 2 3 4 [+] [-] clop
+--------------------------
+ 1 2 7 -1Crosslinks
+
+
+cmp
+
+ 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
+ LDiscussion
+
+[G] [EL] dup cmp
+
+[GE] [L] dupd cmp
+[GL] [E] over cmpCrosslinks
+
+codi
+
+ a b [F] . codi
+--------------------
+ b . F aDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+codireco
+Definition
+
+
+
+Discussion
+x to Generate Values” notebook as well as Recursion Theory and Joy by Manfred von Thun.Crosslinks
+
+
+concat
+
+ [a b c] [d e f] concat
+----------------------------
+ [a b c d e f]Crosslinks
+
+cond
+true. If no predicates return true the default function runs.
+[
+ [ [Predicate0] Function0 ]
+ [ [Predicate1] Function1 ]
+ ...
+ [ [PredicateN] FunctionN ]
+ [Default]
+]
+condDiscussion
+
+ [[[B0] T0] [[B1] T1] [D]] cond
+-----------------------------------------
+ [B0] [T0] [[B1] [T1] [D] ifte] ifteCrosslinks
+
+
+cons
+
+ a [...] cons
+------------------
+ [a ...]Discussion
+Crosslinks
+
+
+dinfrirst
+Definition
+
+
+
+
+dip
+dip 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.
+ ... x [Q] . dip
+---------------------
+ ... . Q xDiscussion
+x 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 dip and infra to operate on compound datastructures with atoms in them. This is a kind of side-effect of the Continuation-Passing Style. The dip combinator could “set aside” the item and replace it after running Q but that means that there is an “extra space” where the item resides while Q 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.)Crosslinks
+
+dipd
+
+ ... y x [Q] . dipd
+-------------------------
+ ... . Q y xDiscussion
+Crosslinks
+
+dipdd
+
+ ... z y x [Q] . dip
+-----------------------------
+ ... . Q z y xDiscussion
+Crosslinks
+
+
+disenstacken
+disenstacken function expects a list on top of the stack and makes that the stack discarding the rest of the stack.
+ 1 2 3 [4 5 6] disenstacken
+--------------------------------
+ 6 5 4Definition
+
+
+
+Discussion
+Crosslinks
+
+
+div
+
+divmod
+
+ x y divmod
+------------------
+ q r
+ (x/y) (x%y)qy + r = x.Definition
+
+
+
+
+down_to_zero
+Example
+
+ 3 down_to_zero
+--------------------
+ 3 2 1 0Definition
+
+
+
+Crosslinks
+
+
+drop
+Example
+
+ [a b c d] 2 drop
+----------------------
+ [c d]Definition
+
+
+
+Crosslinks
+
+
+dup
+
+ a dup
+-----------
+ a aCrosslinks
+
+
+dupd
+
+ a b dupd
+--------------
+ a a bDefinition
+
+
+
+Crosslinks
+
+
+dupdd
+
+ a b c dupdd
+-----------------
+ a a b cDefinition
+
+
+
+Crosslinks
+
+
+dupdip
+F and dup the item under it on the stack.
+ a [F] dupdip
+------------------
+ a F aDefinition
+
+
+
+Derivation
+
+a [F] dupdip
+a [F] dupd dip
+a [F] [dup] dip dip
+a dup [F] dip
+a a [F] dip
+a F aDiscussion
+Crosslinks
+
+
+dupdipd
+F under the next item down on the stack.
+ a [F] dupdipd
+-------------------
+ F a [F]Definition
+
+
+
+Crosslinks
+
+
+enstacken
+
+ ... a b c enstacken
+-------------------------
+ [c b a ...]Definition
+
+
+
+Discussion
+Crosslinks
+
+eq
+
+ a b eq
+-------------
+ Boolean
+ (a = b)Crosslinks
+
+
+first
+
+ [a ...]
+--------------
+ aDefinition
+
+
+
+Crosslinks
+
+
+first_two
+
+ [a b ...] first_two
+-------------------------
+ a bDefinition
+
+
+
+Crosslinks
+
+flatten
+Example
+
+ [[1 2] [3 [4] 5] [6 7]] flatten
+-------------------------------------
+ [1 2 3 [4] 5 6 7]Definition
+
+
+
+Discussion
+[4] is not unquoted.Crosslinks
+
+floor
+Discussion
+
+floordiv
+
+ a b floordiv
+------------------
+ (a/b)Discussion
+Crosslinks
+
+
+fork
+
+ ... [F] [G] fork
+----------------------
+ ... f gDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+fourth
+
+ [a b c d ...] fourth
+--------------------------
+ dDefinition
+
+
+
+Crosslinks
+
+
+gcd
+Definition
+
+
+
+Discussion
+
+gcd2
+Discussion
+Crosslinks
+
+
+ge
+
+ a b ge
+--------------
+ Boolean
+ (a >= b)Crosslinks
+
+
+genrec
+
+ [if] [then] [rec1] [rec2] genrec
+---------------------------------------------------------------------
+ [if] [then] [rec1 [[if] [then] [rec1] [rec2] genrec] rec2] ifteDefinition
+
+
+
+genrec symbol itself, it is self-referential. This is possible because the definition machinery does not check that symbols in defs are in the dictionary. genrec is the only self-referential definition.)Discussion
+
+
+[then] and the test [if], and then treat rec1 and rec2 as an else-part “sandwiching” a quotation of the whole function.F:
+F == [I] [T] [R1] [R2] genrec[I] if-part fails you must derive R1 and R2 from: :
+... R1 [F] R2R1 and R2 have to do to apply the quoted [F] in the proper way. In effect, the genrec combinator turns into an ifte combinator with a quoted copy of the original definition in the else-part:
+F == [I] [T] [R1] [R2] genrec
+ == [I] [T] [R1 [F] R2] ifteR2 is the i combinator:
+P == [I] [T] [R] tailrec
+ == [I] [T] [R [P] i] ifte
+ == [I] [T] [R P] ifteCrosslinks
+
+
+getitem
+Example
+
+ [a b c d] 2 getitem
+-------------------------
+ cDefinition
+
+
+
+Discussion
+getitem will cause some sort of problem (the exact nature of which is implementation-dependant.)Crosslinks
+
+grabN
+Example
+
+ a b c d e 3 grabN
+-----------------------
+ a b [c d e]Definition
+
+
+
+
+grba
+
+ ... 1 2 3 4 5 grba
+-------------------------------
+ ... 1 2 3 [4 3 2 1 ...] 5Definition
+
+
+
+Discussion
+Crosslinks
+
+
+gt
+
+ a b gt
+--------------
+ Boolean
+ (a > b)Crosslinks
+
+
+help
+
+ [foo] help
+----------------Discussion
+pop, but it will only work if the item on the top of the stack is a quoted symbol.
+hypot
+
+ x y hypot
+---------------------------
+ sqrt(sqr(x) + sqr(y))Definition
+
+
+
+Discussion
+Crosslinks
+
+
+i
+
+ [Q] . i
+-------------
+ . QDiscussion
+dup i.
+id
+Discussion
+
+ifte
+
+ [if] [then] [else] ifte
+---------------------------------------
+ [if] nullary [else] [then] branchDefinition
+
+
+
+Crosslinks
+
+
+ii
+
+... a [Q] ii
+------------------
+ ... Q a QDefinition
+
+
+
+Example
+
+ 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 •Discussion
+
+ 1 2 3 4 [+] ii
+--------------------
+ 1 9
+
+ 1 2 3 4 [+] app2
+----------------------
+ 1 2 5 6Crosslinks
+
+
+infra
+
+ ... x y z [a b c] [Q] infra
+---------------------------------
+ c b a Q [z y x ...] swaackDefinition
+
+
+
+Discussion
+infra lets you work on its internal structure.Crosslinks
+
+
+infrst
+Definition
+
+
+
+
+inscribe
+Example
+
+[sqr dup mul] inscribeDiscussion
+defs.txt file. It can be abused, which you should avoid unless you know what you’re doing.
+le
+
+ a b le
+-------------
+ Boolean
+ (a <= b)Crosslinks
+
+
+loop
+Q and a Boolean value on the stack. If the value is false discard the quoted program, otherwise run a copy of Q and loop again.
+ false [Q] loop
+--------------------
+
+
+ true [Q] . loop
+--------------------------
+ . Q [Q] loopDiscussion
+F G is like G(F(...)) in a language bassed on function application. Or again, to quote the Joy Wikipedia entry,
+
+Crosslinks
+
+
+lshift
+
+ a n lshift
+----------------
+ (a×2ⁿ)Crosslinks
+
+
+lt
+
+ a b lt
+-------------
+ Boolean
+ (a < b)Crosslinks
+
+
+make_generator
+
+ state [generator function] make_generator
+-----------------------------------------------
+ [state [generator function] codireco]Example
+
+ 230 [dup ++] make_generator
+---------------------------------
+ [230 [dup ++] codireco]
+ [230 [dup ++] codireco] 5 [x] times pop
+---------------------------------------------
+ 230 231 232 233 234Definition
+
+
+
+Discussion
+x to Generate Values” notebook.Crosslinks
+
+
+map
+Example
+
+ 5 [1 2 3] [++ *] map
+--------------------------
+ 5 [10 15 20]Discussion
+Crosslinks
+
+
+max
+Example
+
+ [1 2 3 4] max
+-------------------
+ 4Crosslinks
+
+
+min
+Example
+
+ [1 2 3 4] min
+-------------------
+ 1 Crosslinks
+
+
+mod
+a divided by b.
+ a b mod
+-------------
+ (a%b)Crosslinks
+
+
+modulus
+
+mul
+
+ a b mul
+-------------
+ (a×b)Crosslinks
+
+
+ne
+
+ a b ne
+-------------
+ Boolean
+ (a = b)Crosslinks
+
+
+neg
+
+ a neg
+-----------
+ -aDefinition
+
+
+
+
+not
+
+ true not
+--------------
+ false
+
+ false not
+---------------
+ trueDefinition
+
+
+
+Crosslinks
+
+
+nulco
+[nullary].
+ [F] nulco
+-------------------
+ [[F] nullary]Definition
+
+
+
+Discussion
+Crosslinks
+
+
+nullary
+
+ ... [P] nullary
+---------------------
+ ... aDefinition
+
+
+
+Example
+
+... [P] nullary
+... [P] [stack] dip infra first
+... stack [P] infra first
+... [...] [P] infra first
+... [a ...] first
+... aDiscussion
+Crosslinks
+
+
+of
+Example
+
+ 2 [a b c d] of
+--------------------
+ cDefinition
+
+
+
+Crosslinks
+
+
+or
+Crosslinks
+
+
+over
+over the first.
+ a b over
+--------------
+ a b aDefinition
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+Discussion
+Crosslinks
+
+
+pam
+Example
+
+ 5 7 [[+][-][*][/][%]] pam
+-------------------------------
+ 5 7 [12 -2 35 0 5]Definition
+
+
+
+Discussion
+Crosslinks
+
+
+pick
+
+pm
+
+ a b pm
+-----------------
+ (a+b) (a-b)Definition
+
+
+
+pop
+
+ a pop
+-----------Crosslinks
+
+popd
+
+ a b popd
+--------------
+ bDefinition
+
+
+
+Crosslinks
+
+popdd
+
+ a b c popdd
+-----------------
+ b cDefinition
+
+
+
+Crosslinks
+
+popop
+
+ a b popop
+---------------Definition
+
+
+
+Crosslinks
+
+popopd
+
+ a b c popopd
+------------------
+ cDefinition
+
+
+
+Crosslinks
+
+popopdd
+
+ a b c d popopdd
+---------------------
+ c dDefinition
+
+
+
+Crosslinks
+
+popopop
+
+ a b c popopop
+-------------------Definition
+
+
+
+Crosslinks
+
+pow
+a and b from the stack and raise a to the nth power. (b is on the top of the stack.)
+ a n pow
+-------------
+ (aⁿ)Example
+
+ 2 [2 3 4 5 6 7 8 9] [pow] map
+-----------------------------------
+ 2 [4 8 16 32 64 128 256 512]
+pred
+Definition
+
+
+Crosslinks
+
+
+primrec
+
+
+
+
+
+
+
+ 0 [Base] [Recur] primrec
+------------------------------
+ Base
+
+ n [Base] [Recur] primrec
+------------------------------------------ n > 0
+ n (n-1) [Base] [Recur] primrec RecurDiscussion
+Crosslinks
+
+
+product
+Definition
+
+
+
+
+
+
+
+quoted
+
+ a b quoted
+----------------
+ [a] bDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+range
+n on the stack and replace it with a list: [(n-1)...0].Example
+
+ 5 range
+-----------------
+ [4 3 2 1 0]
+
+ -5 range
+--------------
+ []Definition
+
+
+Discussion
+n is less than 1 the resulting list is empty.Crosslinks
+
+
+range_to_zero
+n from the stack and replace it with a list [0...n].Example
+
+ 5 range_to_zero
+---------------------
+ [0 1 2 3 4 5]Definition
+
+
+
+Discussion
+Crosslinks
+
+
+reco
+
+ a [b ...] reco
+--------------------
+ [a ...]Definition
+
+
+
+Crosslinks
+
+
+rem
+
+remainder
+
+remove
+
+ [1 2 3 1] 1 remove
+------------------------
+ [2 3 1]Definition
+
+rest
+
+ [a ...] rest
+------------------
+ [...]Crosslinks
+
+
+reverse
+Example
+
+ [1 2 3] reverse
+---------------------
+ [3 2 1]Definition
+
+
+
+
+rolldown
+
+ a b c rolldown
+--------------------
+ b c aDefinition
+
+
+
+Crosslinks
+
+
+rollup
+
+ a b c rollup
+------------------
+ c a bDefinition
+
+
+
+Crosslinks
+
+
+roll>
+
+roll<
+
+round
+Discussion
+
+rrest
+
+ [a b ...] rrest
+---------------------
+ [...]Definition
+
+
+
+Crosslinks
+
+
+rshift
+
+ a n rshift
+----------------
+ (a∕2ⁿ)Crosslinks
+
+
+run
+Example
+
+ [1 2 +] run
+-----------------
+ [3]Definition
+
+
+
+
+second
+
+ [a b ...] second
+----------------------
+ bDefinition
+
+
+
+Crosslinks
+
+
+select
+
+ [a b] false select
+------------------------
+ a
+
+ [a b] true select
+-----------------------
+ bDiscussion
+Crosslinks
+
+
+sharing
+Discussion
+Crosslinks
+
+
+shift
+Example
+
+ [x y z] [a b c] shift
+---------------------------
+ [a x y z] [b c]Definition
+
+
+
+Crosslinks
+
+
+shunt
+Example
+
+ [a b c] [d e f] shunt
+---------------------------
+ [f e d a b c] Definition
+
+
+
+Discussion
+Crosslinks
+
+
+size
+Example
+
+ [23 [cats] 4] size
+------------------------
+ 3Definition
+
+
+
+
+sort
+Example
+
+ [4 2 5 7 1] sort
+----------------------
+ [1 2 4 5 7]
+spiral_next
+Discussion
+
+split_at
+Example
+
+ [1 2 3 4 5 6 7] 4 split_at
+--------------------------------
+ [5 6 7] [4 3 2 1]Definition
+
+
+
+Discussion
+n from the stack, take n items from the top of the list and shunt them onto a new list that replaces the number n on the top of the stack.Crosslinks
+
+
+split_list
+
+ [1 2 3 4 5 6 7] 4 split_list
+----------------------------------
+ [1 2 3 4] [5 6 7]Definition
+
+
+
+Discussion
+Crosslinks
+
+
+sqr
+
+ n sqr
+------------
+ n²Definition
+
+
+
+
+sqrt
+Discussion
+
+stack
+
+ ... c b a stack
+---------------------------
+ ... c b a [a b c ...]Definition
+
+
+
+Discussion
+Crosslinks
+
+stackd
+Example
+
+ ... 1 2 3 stackd
+------------------------
+ ... 1 2 [2 1 ...] 3Definition
+
+
+
+
+step
+
+ ... [] [Q] step
+---------------------
+ ...
+
+
+ ... [a] [Q] step
+----------------------
+ ... a Q
+
+
+ ... [a b c] [Q] . step
+----------------------------------------
+ ... a . Q [b c] [Q] stepDiscussion
+Crosslinks
+
+
+step_zero
+
+ [...] [F] step_zero
+-------------------------
+ 0 [...] [F] stepDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+stuncons
+Example
+
+ 1 2 3 stuncons
+--------------------
+ 1 2 3 3 [2 1]Definition
+
+
+
+
+stununcons
+Example
+
+ 1 2 3 stununcons
+----------------------
+ 1 2 3 3 2 [1]Definition
+
+
+
+Crosslinks
+
+
+sub
+
+ a b sub
+-------------
+ (a-b)Crosslinks
+
+
+succ
+Definition
+
+
+Crosslinks
+
+
+sum
+Example
+
+ [1 2 3 4 5] sum
+---------------------
+ 15Definition
+
+
+Crosslinks
+
+
+swaack
+Example
+
+ 1 2 3 [4 5 6] swaack
+--------------------------
+ 6 5 4 [3 2 1]Discussion
+Crosslinks
+
+
+swap
+
+ a b swap
+--------------
+ b aCrosslinks
+
+
+swapd
+
+ a b c swapd
+-----------------
+ b a cDefinition
+
+
+
+Crosslinks
+
+
+swoncat
+Definition
+
+
+
+Crosslinks
+
+
+swons
+
+ [...] a swons
+-------------------
+ [a ...]Definition
+
+
+
+
+tailrec
+Definition
+
+
+
+Discussion
+Crosslinks
+
+
+take
+n and a list on the stack and replace them with a list with just the top n items in reverse order.
+ [a b c d] 2 take
+----------------------
+ [b a]Definition
+
+
+
+
+ternary
+
+ ... z y x [P] ternary
+-------------------------
+ ... aDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+third
+
+ [a b c ...] third
+-----------------------
+ cDefinition
+
+
+
+Crosslinks
+
+
+times
+n on the stack and do the program n times.
+ ... n [Q] . times
+----------------------- w/ n <= 0
+ ... .
+
+ ... 1 [Q] . times
+-----------------------
+ ... . Q
+
+ ... n [Q] . times
+------------------------------------- w/ n > 1
+ ... . Q (n-1) [Q] timesDefinition
+
+
+Discussion
+
+ 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
+truthy
+
+tuck
+
+ a b tuck
+--------------
+ b a bDefinition
+
+
+
+Crosslinks
+
+
+unary
+
+ ... x [P] unary
+---------------------
+ ... aDefinition
+
+
+
+Discussion
+Crosslinks
+
+
+uncons
+uncons an item from an empty list.
+ [a ...] uncons
+--------------------
+ a [...]Discussion
+Crosslinks
+
+
+unique
+
+unit
+
+ a unit
+------------
+ [a]Definition
+
+
+
+unquoted
+Example
+
+ 1 2 [3 4] 5 unquoted
+--------------------------
+ 1 2 3 4 5Definition
+
+
+
+Crosslinks
+
+
+unswons
+
+ [a ...] unswons
+---------------------
+ [...] aDefinition
+
+
+
+
+void
+Discussion
+
+warranty
+
+while
+P and runs it nullary.
+ [P] [F] while
+------------------- P -> false
+
+ [P] [F] while
+--------------------- P -> true
+ F [P] [F] whileDefinition
+
+
+
+Crosslinks
+
+
+words
+Discussion
+Crosslinks
+
+
+x
+F and run it with itself as the first item on the stack.
+ [F] x
+-----------
+ [F] FDefinition
+
+dup iDiscussion
+
+xor
+Crosslinks
+
+
+zip
+Example
+
+
+
diff --git a/docs/html/index.html b/docs/html/index.html
index ae68769..f3aea0f 100644
--- a/docs/html/index.html
+++ b/docs/html/index.html
@@ -33,14 +33,14 @@ Joy and its deeper facets as well as how to program in it and several
interesting aspects. It's quite a treasure trove. [1 2 3] [4 5 6] zip
+-------------------------
+ [[4 1] [5 2] [6 3]]
Here is an example of Joy code:
-[ [[abs] ii <=]
- [
- [<>] [pop !-] ||
- ] &&
-]
-[[ !-] [[++]] [[--]] ifte dip]
-[[pop !-] [--] [++] ifte ]
-ifte
+square_spiral ≡ [_p] [_then] [_else] ifte
+
+_p ≡ [_p0] [_p1] &&
+_p0 ≡ [abs] ii <=
+_p1 ≡ [<>] [pop !-] ||
+
+_then ≡ [ !-] [[++]] [[--]] ifte dip
+_else ≡ [pop !-] [--] [++] ifte
It might seem unreadable but with a little familiarity it becomes just as legible as any other notation.
This function accepts two integers on the stack and increments or
@@ -88,8 +88,8 @@ coordinate pair in a square spiral (like the kind used to construct an
Jupyter Notebooks
The docs/notebooks dir contains Jupyter notebooks, ... TODO
-Function Reference
-
+Function Reference
+
Building the Docs
Run make in the docs directory.
Basics of Joy
diff --git a/docs/reference/grabN.md b/docs/reference/grabN.md
index 0b2455d..a20495a 100644
--- a/docs/reference/grabN.md
+++ b/docs/reference/grabN.md
@@ -4,7 +4,7 @@
Function
-Expect a number on the top of the satck and put that many items from uner it onto a new list.
+Expect a number on the top of the stack and [cons] that many items from under it onto a new list.
### Example
diff --git a/docs/reference/mkref/FuncRef.html b/docs/reference/mkref/FuncRef.html
index bf874f1..9d7ed40 100644
--- a/docs/reference/mkref/FuncRef.html
+++ b/docs/reference/mkref/FuncRef.html
@@ -4,7 +4,7 @@
- Functor Reference
+ Function Reference