{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# Examples (and some documentation) for the Words in the Library" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Stack Chatter\n", "This is what I like to call the functions that just rearrange things on the stack. (One thing I want to mention is that during a hypothetical compilation phase these \"stack chatter\" words effectively disappear, because we can map the logical stack locations to registers that remain static for the duration of the computation. This remains to be done but it's \"off the shelf\" technology.)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `clear`" ] }, { "cell_type": "code", "execution_count": 132, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1 3 1 2 3" ] } ], "source": [ "1 2 3 " ] }, { "cell_type": "code", "execution_count": 133, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "clear" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dup` `dupd`" ] }, { "cell_type": "code", "execution_count": 134, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 3" ] } ], "source": [ "1 2 3 dup" ] }, { "cell_type": "code", "execution_count": 135, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "clear" ] }, { "cell_type": "code", "execution_count": 136, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 2 3" ] } ], "source": [ "1 2 3 dupd" ] }, { "cell_type": "code", "execution_count": 137, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "clear" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `enstacken` `disenstacken` `stack` `unstack`\n", "\n", "Replace the stack with a quote of itself." ] }, { "cell_type": "code", "execution_count": 138, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 2 1]" ] } ], "source": [ "1 2 3 enstacken" ] }, { "cell_type": "code", "execution_count": 139, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "clear" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unpack a list onto the stack." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4 5 6 [3 2 1] unstack" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Get the stack on the stack." ] }, { "cell_type": "code", "execution_count": 140, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 [3 2 1]" ] } ], "source": [ "1 2 3 stack" ] }, { "cell_type": "code", "execution_count": 141, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [] } ], "source": [ "clear" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Replace the stack with the list on top.\n", "The items appear reversed but they are not,\n", "is on the top of both the list and the stack." ] }, { "cell_type": "code", "execution_count": 142, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 5 4" ] } ], "source": [ "1 2 3 [4 5 6] disenstacken" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `pop` `popd` `popop`" ] }, { "cell_type": "code", "execution_count": 143, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2" ] } ], "source": [ "clear\n", "1 2 3 pop" ] }, { "cell_type": "code", "execution_count": 144, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3" ] } ], "source": [ "clear\n", "1 2 3 popd" ] }, { "cell_type": "code", "execution_count": 145, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1" ] } ], "source": [ "clear\n", "1 2 3 popop" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `roll<` `rolldown` `roll>` `rollup`\n", "The \"down\" and \"up\" refer to the movement of two of the top three items (displacing the third.)" ] }, { "cell_type": "code", "execution_count": 146, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2 3 1" ] } ], "source": [ "clear\n", "1 2 3 roll<" ] }, { "cell_type": "code", "execution_count": 147, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3 1 2" ] } ], "source": [ "clear\n", "1 2 3 roll>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `swap`" ] }, { "cell_type": "code", "execution_count": 148, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3 2" ] } ], "source": [ "clear\n", "1 2 3 swap" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `tuck` `over`" ] }, { "cell_type": "code", "execution_count": 149, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 3 2 3" ] } ], "source": [ "clear\n", "1 2 3 tuck" ] }, { "cell_type": "code", "execution_count": 150, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3 2" ] } ], "source": [ "clear\n", "1 2 3 over" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `unit` `quoted` `unquoted`" ] }, { "cell_type": "code", "execution_count": 151, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 [3]" ] } ], "source": [ "clear\n", "1 2 3 unit" ] }, { "cell_type": "code", "execution_count": 152, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 [2] 3" ] } ], "source": [ "clear\n", "1 2 3 quoted" ] }, { "cell_type": "code", "execution_count": 153, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 2 3" ] } ], "source": [ "clear\n", "1 [2] 3 unquoted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Unquoting evaluates. Be aware." ] }, { "cell_type": "code", "execution_count": 154, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 1 3" ] } ], "source": [ "clear\n", "1 [dup] 3 unquoted" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# List words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `concat` `swoncat` `shunt`" ] }, { "cell_type": "code", "execution_count": 155, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3 4 5 6]" ] } ], "source": [ "clear\n", "[1 2 3] [4 5 6] concat" ] }, { "cell_type": "code", "execution_count": 156, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 5 6 1 2 3]" ] } ], "source": [ "clear\n", "[1 2 3] [4 5 6] swoncat" ] }, { "cell_type": "code", "execution_count": 157, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[6 5 4 1 2 3]" ] } ], "source": [ "clear\n", "[1 2 3] [4 5 6] shunt" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `cons` `swons` `uncons`" ] }, { "cell_type": "code", "execution_count": 158, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]" ] } ], "source": [ "clear\n", "1 [2 3] cons" ] }, { "cell_type": "code", "execution_count": 159, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 3]" ] } ], "source": [ "clear\n", "[2 3] 1 swons" ] }, { "cell_type": "code", "execution_count": 160, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1 [2 3]" ] } ], "source": [ "clear\n", "[1 2 3] uncons" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `first` `second` `third` `rest`" ] }, { "cell_type": "code", "execution_count": 161, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1" ] } ], "source": [ "clear\n", "[1 2 3 4] first" ] }, { "cell_type": "code", "execution_count": 162, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2" ] } ], "source": [ "clear\n", "[1 2 3 4] second" ] }, { "cell_type": "code", "execution_count": 163, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3" ] } ], "source": [ "clear\n", "[1 2 3 4] third" ] }, { "cell_type": "code", "execution_count": 166, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3 4]" ] } ], "source": [ "clear\n", "[1 2 3 4] rest" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `flatten`" ] }, { "cell_type": "code", "execution_count": 167, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[1 2 [3] 4 5 6]" ] } ], "source": [ "clear\n", "[[1] [2 [3] 4] [5 6]] flatten" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `getitem` `at` `of` `drop` `take`\n", "\n", "`at` and `getitem` are the same function. `of == swap at`" ] }, { "cell_type": "code", "execution_count": 168, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "12" ] } ], "source": [ "clear\n", "[10 11 12 13 14] 2 getitem" ] }, { "cell_type": "code", "execution_count": 169, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1" ] } ], "source": [ "clear\n", "[1 2 3 4] 0 at" ] }, { "cell_type": "code", "execution_count": 170, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "3" ] } ], "source": [ "clear\n", "2 [1 2 3 4] of" ] }, { "cell_type": "code", "execution_count": 171, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[3 4]" ] } ], "source": [ "clear\n", "[1 2 3 4] 2 drop" ] }, { "cell_type": "code", "execution_count": 172, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 1]" ] } ], "source": [ "clear\n", "[1 2 3 4] 2 take" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " `take` reverses the order." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "`reverse` could be defines as `reverse == dup size take`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `remove`" ] }, { "cell_type": "code", "execution_count": 173, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[2 3 1 4]" ] } ], "source": [ "clear\n", "[1 2 3 1 4] 1 remove" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `reverse`" ] }, { "cell_type": "code", "execution_count": 174, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 3 2 1]" ] } ], "source": [ "clear\n", "[1 2 3 4] reverse" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `size`" ] }, { "cell_type": "code", "execution_count": 175, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4" ] } ], "source": [ "clear\n", "[1 1 1 1] size" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `swaack`\n", "\"Swap stack\" swap the list on the top of the stack for the stack, and put the old stack on top of the new one. Think of it as a context switch. Niether of the lists/stacks change their order." ] }, { "cell_type": "code", "execution_count": 176, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "6 5 4 [3 2 1]" ] } ], "source": [ "clear\n", "1 2 3 [4 5 6] swaack" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `choice` `select`" ] }, { "cell_type": "code", "execution_count": 180, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9" ] } ], "source": [ "clear\n", "23 9 true choice" ] }, { "cell_type": "code", "execution_count": 181, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23" ] } ], "source": [ "clear\n", "23 9 false choice" ] }, { "cell_type": "code", "execution_count": 182, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "9" ] } ], "source": [ "clear\n", "[23 9 7] 1 select" ] }, { "cell_type": "markdown", "metadata": { "scrolled": true }, "source": [ "(`select` is basically `getitem`, should retire it?)" ] }, { "cell_type": "code", "execution_count": 183, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23" ] } ], "source": [ "clear\n", "[23 9 7] 0 select" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `zip`" ] }, { "cell_type": "code", "execution_count": 184, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[[6 1] [5 2] [4 3]]" ] } ], "source": [ "clear\n", "[1 2 3] [6 5 4] zip" ] }, { "cell_type": "code", "execution_count": 185, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[7 7 7]" ] } ], "source": [ "clear\n", "[1 2 3] [6 5 4] zip [sum] map" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Math words" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `+` `add`" ] }, { "cell_type": "code", "execution_count": 186, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "32" ] } ], "source": [ "clear\n", "23 9 +" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `-` `sub`" ] }, { "cell_type": "code", "execution_count": 187, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "14" ] } ], "source": [ "clear\n", "23 9 -" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `*` `mul`" ] }, { "cell_type": "code", "execution_count": 188, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "207" ] } ], "source": [ "clear\n", "23 9 *" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `/` `div`" ] }, { "cell_type": "code", "execution_count": 189, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2" ] } ], "source": [ "clear\n", "23 9 /" ] }, { "cell_type": "code", "execution_count": 191, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2" ] } ], "source": [ "clear\n", "23 9 div" ] }, { "cell_type": "code", "execution_count": 193, "metadata": { "scrolled": false }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-3" ] } ], "source": [ "clear\n", "23 -9 div" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `%` `mod` `modulus` `rem` `remainder`" ] }, { "cell_type": "code", "execution_count": 195, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5" ] } ], "source": [ "clear\n", "23 9 %" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `neg`" ] }, { "cell_type": "code", "execution_count": 196, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "-23 5" ] } ], "source": [ "clear\n", "23 neg -5 neg" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### pow" ] }, { "cell_type": "code", "execution_count": 197, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1024" ] } ], "source": [ "clear\n", "2 10 pow" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sqr`" ] }, { "cell_type": "code", "execution_count": 198, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "529" ] } ], "source": [ "clear\n", "23 sqr" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `++` `succ` `--` `pred`" ] }, { "cell_type": "code", "execution_count": 199, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "2" ] } ], "source": [ "clear\n", "1 ++" ] }, { "cell_type": "code", "execution_count": 200, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0" ] } ], "source": [ "clear\n", "1 --" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `<<` `lshift` `>>` `rshift`" ] }, { "cell_type": "code", "execution_count": 201, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "16" ] } ], "source": [ "clear\n", "8 1 <<" ] }, { "cell_type": "code", "execution_count": 202, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "4" ] } ], "source": [ "clear\n", "8 1 >>" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `range` `range_to_zero` `down_to_zero`" ] }, { "cell_type": "code", "execution_count": 204, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[4 3 2 1 0]" ] } ], "source": [ "clear\n", "5 range" ] }, { "cell_type": "code", "execution_count": 205, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5]" ] } ], "source": [ "clear\n", "5 range_to_zero" ] }, { "cell_type": "code", "execution_count": 206, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "5 4 3 2 1 0" ] } ], "source": [ "clear\n", "5 down_to_zero" ] }, { "cell_type": "code", "execution_count": 207, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[0 1 2 3 4 5]" ] } ], "source": [ "clear\n", "[5 down_to_zero] run" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `product`" ] }, { "cell_type": "code", "execution_count": 208, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "30" ] } ], "source": [ "clear\n", "[1 2 3 5] product" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `sum`" ] }, { "cell_type": "code", "execution_count": 209, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "11" ] } ], "source": [ "clear\n", "[1 2 3 5] sum" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `min`" ] }, { "cell_type": "code", "execution_count": 210, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "1" ] } ], "source": [ "clear\n", "[1 2 3 5] min" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `gcd`" ] }, { "cell_type": "code", "execution_count": 211, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "15" ] } ], "source": [ "clear\n", "45 30 gcd" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Logic and Comparison" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `?` `truthy`\n", "Get the Boolean value of the item on the top of the stack." ] }, { "cell_type": "code", "execution_count": 216, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true" ] } ], "source": [ "clear\n", "23 bool" ] }, { "cell_type": "code", "execution_count": 217, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "false" ] } ], "source": [ "clear\n", "[] bool" ] }, { "cell_type": "code", "execution_count": 218, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "false" ] } ], "source": [ "clear\n", "0 bool" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ " ? == dup truthy" ] }, { "cell_type": "code", "execution_count": 219, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "23 true" ] } ], "source": [ "clear\n", "23 ?" ] }, { "cell_type": "code", "execution_count": 220, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[] false" ] } ], "source": [ "clear\n", "[] ?" ] }, { "cell_type": "code", "execution_count": 221, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "0 false" ] } ], "source": [ "clear\n", "0 ?" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `&` `and` " ] }, { "cell_type": "code", "execution_count": 223, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "false" ] } ], "source": [ "clear\n", "true false &" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `!=` `<>` `ne`" ] }, { "cell_type": "code", "execution_count": 224, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "true" ] } ], "source": [ "clear\n", "23 9 !=" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The usual suspects:\n", "- `<` `lt`\n", "- `<=` `le` \n", "- `=` `eq`\n", "- `>` `gt`\n", "- `>=` `ge`\n", "- `not`\n", "- `or`" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Miscellaneous" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `help`" ] }, { "cell_type": "code", "execution_count": 228, "metadata": { "scrolled": true }, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "==== Help on help ====\n", "\n", "Accepts a quoted symbol on the top of the stack and prints its docs.\n", "\n", "---- end ( help )\n", "\n", "\n" ] } ], "source": [ "clear\n", "[help] help" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `run`\n", "Evaluate a quoted Joy sequence." ] }, { "cell_type": "code", "execution_count": 229, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "[5]" ] } ], "source": [ "clear\n", "[1 2 dup + +] run" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# Combinators" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `app1` `app2` `app3`" ] }, { "cell_type": "code", "execution_count": 230, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "==== Help on app1 ====\n", "\n", "app1 ≡ grba infrst\n", "\n", "---- end ( app1 )\n", "\n", "\n" ] } ], "source": [ "clear\n", "[app1] help" ] }, { "cell_type": "code", "execution_count": 231, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 160" ] } ], "source": [ "clear\n", "10 4 [sqr *] app1" ] }, { "cell_type": "code", "execution_count": 232, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "10 90 160" ] } ], "source": [ "clear\n", "10 3 4 [sqr *] app2" ] }, { "cell_type": "code", "execution_count": 233, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\n", "==== Help on app2 ====\n", "\n", "app2 ≡ [grba swap grba swap] dip [infrst] cons ii\n", "\n", "---- end ( app2 )\n", "\n", "\n" ] } ], "source": [ "clear\n", "[app2] help" ] }, { "cell_type": "code", "execution_count": 234, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "160 90 40" ] } ], "source": [ "clear\n", "10 2 3 4 [sqr *] app3" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "(Wait... is that... backwards?)\n", "\n", "It should be `40 90 160` shouldn't it?" ] }, { "cell_type": "code", "execution_count": 235, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40 90 160" ] } ], "source": [ "clear\n", "10 2 3 4 [sqr *] 3 [grabN] codi map reverse disenstacken" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Ah! See? It needs `reverse` in there!" ] }, { "cell_type": "code", "execution_count": 236, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "40 90 160 250" ] } ], "source": [ "clear\n", "10 2 3 4 5 [sqr *] 4 [grabN] codi map reverse disenstacken" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `anamorphism`\n", "Given an initial value, a predicate function `[P]`, and a generator function `[G]`, the `anamorphism` combinator creates a sequence.\n", "\n", " n [P] [G] anamorphism\n", " ---------------------------\n", " [...]\n", "\n", "Example, `range`:\n", "\n", " range == [0 <=] [1 - dup] anamorphism" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [0 <=] [1 - dup] anamorphism')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `branch`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 4 1 [+] [*] branch')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 4 0 [+] [*] branch')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `cleave`\n", " ... x [P] [Q] cleave\n", "\n", "From the original Joy docs: \"The cleave combinator expects two quotations, and below that an item `x`\n", "It first executes `[P]`, with `x` on top, and saves the top result element.\n", "Then it executes `[Q]`, again with `x`, and saves the top result.\n", "Finally it restores the stack to what it was below `x` and pushes the two\n", "results P(X) and Q(X).\"\n", "\n", "Note that `P` and `Q` can use items from the stack freely, since the stack (below `x`) is restored. `cleave` is a kind of *parallel* primitive, and it would make sense to create a version that uses, e.g. Python threads or something, to actually run `P` and `Q` concurrently. The current implementation of `cleave` is a definition in terms of `app2`:\n", "\n", " cleave == [i] app2 [popd] dip" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 2 [+] [-] cleave')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dip` `dipd` `dipdd`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dip')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dipd')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] dipdd')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `dupdip`\n", "Expects a quoted program `[Q]` on the stack and some item under it, `dup` the item and `dip` the quoted program under it.\n", "\n", " n [Q] dupdip == n Q n" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('23 [++] dupdip *') # N(N + 1)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `genrec` `primrec`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[genrec] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [1 <=] [] [dup --] [i *] genrec')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `i`" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "V('1 2 3 [+ +] i')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `ifte`\n", " [predicate] [then] [else] ifte" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 [1] [+] [*] ifte')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 [0] [+] [*] ifte')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `infra`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 2 3 [4 5 6] [* +] infra')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `loop`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[loop] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('3 dup [1 - dup] loop')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `map` `pam`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 [1 2 3] [*] map')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('10 5 [[*][/][+][-]] pam')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `nullary` `unary` `binary` `ternary`\n", "Run a quoted program enforcing [arity](https://en.wikipedia.org/wiki/Arity)." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] nullary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] unary')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] binary') # + has arity 2 so this is technically pointless..." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('1 2 3 4 5 [+] ternary')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `step`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[step] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": { "scrolled": true }, "outputs": [], "source": [ "V('0 [1 2 3] [+] step')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `times`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('3 2 1 2 [+] times')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `b`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[b] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 2 [3] [4] b')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `while`\n", " [predicate] [body] while" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('3 [0 >] [dup --] while')" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### `x`" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[x] help')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "V('1 [2] [i 3] x') # Kind of a pointless example." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "# `void`\n", "Implements [**Laws of Form** *arithmetic*](https://en.wikipedia.org/wiki/Laws_of_Form#The_primary_arithmetic_.28Chapter_4.29) over quote-only datastructures (that is, datastructures that consist soley of containers, without strings or numbers or anything else.)" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[]] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[][[]]] void')" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "J('[[[]][[][]]] void')" ] } ], "metadata": { "kernelspec": { "display_name": "Joypy", "language": "", "name": "thun" }, "language_info": { "file_extension": ".joy", "mimetype": "text/plain", "name": "Joy" } }, "nbformat": 4, "nbformat_minor": 2 }