Update zipper notebook to use Joy kernel.
This commit is contained in:
parent
0ab8979d7a
commit
600dc05030
|
|
@ -11,15 +11,6 @@
|
|||
"Given a datastructure on the stack we can navigate through it, modify it, and rebuild it using the \"zipper\" technique."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"from notebook_preamble import J, V, define"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
|
|
@ -30,19 +21,19 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1 [2 [3 4 25 6] 7] 8]\n"
|
||||
"[1 [2 [3 4 25 6] 7] 8]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1 [2 [3 4 25 6] 7] 8]')"
|
||||
"[1 [2 [3 4 25 6] 7] 8]"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -62,16 +53,48 @@
|
|||
"Let's use them to change 25 into 625. The first time a word is used I show the trace so you can see how it works. If we were going to use these a lot it would make sense to write Python versions for efficiency, but see below."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1 [2 [3 4 25 6] 7] 8]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"[z-down [] swap uncons swap] inscribe\n",
|
||||
"[z-up swons swap shunt] inscribe\n",
|
||||
"[z-right [swons] cons dip uncons swap] inscribe\n",
|
||||
"[z-left swons [uncons swap] dip swap] inscribe"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" [1 [2 [3 4 25 6] 7] 8] • z-down\n",
|
||||
" [1 [2 [3 4 25 6] 7] 8] • [] swap uncons swap\n",
|
||||
"[1 [2 [3 4 25 6] 7] 8] [] • swap uncons swap\n",
|
||||
"[] [1 [2 [3 4 25 6] 7] 8] • uncons swap\n",
|
||||
"[] 1 [[2 [3 4 25 6] 7] 8] • swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] 1 • \n",
|
||||
"\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] 1"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"define('z-down == [] swap uncons swap')\n",
|
||||
"define('z-up == swons swap shunt')\n",
|
||||
"define('z-right == [swons] cons dip uncons swap')\n",
|
||||
"define('z-left == swons [uncons swap] dip swap')"
|
||||
"[z-down] trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -83,18 +106,23 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" . [1 [2 [3 4 25 6] 7] 8] z-down\n",
|
||||
" [1 [2 [3 4 25 6] 7] 8] . z-down\n",
|
||||
" [1 [2 [3 4 25 6] 7] 8] . [] swap uncons swap\n",
|
||||
"[1 [2 [3 4 25 6] 7] 8] [] . swap uncons swap\n",
|
||||
"[] [1 [2 [3 4 25 6] 7] 8] . uncons swap\n",
|
||||
"[] 1 [[2 [3 4 25 6] 7] 8] . swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] 1 . \n"
|
||||
" [] [[2 [3 4 25 6] 7] 8] 1 • z-right\n",
|
||||
" [] [[2 [3 4 25 6] 7] 8] 1 • [swons] cons dip uncons swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] 1 [swons] • cons dip uncons swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] [1 swons] • dip uncons swap\n",
|
||||
" [] • 1 swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [] 1 • swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [1] • [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [1] [[2 [3 4 25 6] 7] 8] • uncons swap\n",
|
||||
" [1] [2 [3 4 25 6] 7] [8] • swap\n",
|
||||
" [1] [8] [2 [3 4 25 6] 7] • \n",
|
||||
"\n",
|
||||
"[1] [8] [2 [3 4 25 6] 7]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"V('[1 [2 [3 4 25 6] 7] 8] z-down')"
|
||||
"[z-right] trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -106,26 +134,12 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" . [] [[2 [3 4 25 6] 7] 8] 1 z-right\n",
|
||||
" [] . [[2 [3 4 25 6] 7] 8] 1 z-right\n",
|
||||
" [] [[2 [3 4 25 6] 7] 8] . 1 z-right\n",
|
||||
" [] [[2 [3 4 25 6] 7] 8] 1 . z-right\n",
|
||||
" [] [[2 [3 4 25 6] 7] 8] 1 . [swons] cons dip uncons swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] 1 [swons] . cons dip uncons swap\n",
|
||||
"[] [[2 [3 4 25 6] 7] 8] [1 swons] . dip uncons swap\n",
|
||||
" [] . 1 swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [] 1 . swons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [] 1 . swap cons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" 1 [] . cons [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [1] . [[2 [3 4 25 6] 7] 8] uncons swap\n",
|
||||
" [1] [[2 [3 4 25 6] 7] 8] . uncons swap\n",
|
||||
" [1] [2 [3 4 25 6] 7] [8] . swap\n",
|
||||
" [1] [8] [2 [3 4 25 6] 7] . \n"
|
||||
"[1] [8] [] [[3 4 25 6] 7] 2"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"V('[] [[2 [3 4 25 6] 7] 8] 1 z-right')"
|
||||
"z-down"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -137,12 +151,12 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [] [[3 4 25 6] 7] 2\n"
|
||||
"[1] [8] [2] [7] [3 4 25 6]"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1] [8] [2 [3 4 25 6] 7] z-down')"
|
||||
"z-right"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -154,12 +168,12 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2] [7] [3 4 25 6]\n"
|
||||
"[1] [8] [2] [7] [] [4 25 6] 3"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1] [8] [] [[3 4 25 6] 7] 2 z-right')"
|
||||
"z-down"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -171,12 +185,12 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2] [7] [] [4 25 6] 3\n"
|
||||
"[1] [8] [2] [7] [3] [25 6] 4"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1] [8] [2] [7] [3 4 25 6] z-down')"
|
||||
"z-right"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -188,29 +202,21 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2] [7] [3] [25 6] 4\n"
|
||||
"[1] [8] [2] [7] [4 3] [6] 25"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1] [8] [2] [7] [] [4 25 6] 3 z-right')"
|
||||
"z-right"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2] [7] [4 3] [6] 25\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"J('[1] [8] [2] [7] [3] [25 6] 4 z-right')"
|
||||
"sqr"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -222,76 +228,41 @@
|
|||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2] [7] [4 3] [6] 625\n"
|
||||
"!= % & * + ++ - -- / // /floor < << <= <> = > >= >> ^ _Tree_add_Ee _Tree_delete_R0 _Tree_delete_clear_stuff _Tree_get_E abs add and app1 app2 app3 at b bool branch ccons choice clear cmp concat cond cons dip dipd dipdd disenstacken div divmod drop dup dupd dupdd dupdip eq first first_two floor floordiv fourth gcd2 ge genrec getitem gt help i id infra inscribe le loop lshift lt map max min mod modulus mul ne neg not or over parse pick pm pop popd popdd popop popopd popopdd pow pred primrec rem remainder remove rest reverse roll< roll> rolldown rollup round rrest rshift second select sharing shunt sort sqrt stack step stuncons stununcons sub succ sum swaack swap swons take third times trace truthy tuck uncons unique unit unswons void warranty words x xor z-down z-left z-right z-up zip •\n",
|
||||
"\n",
|
||||
"[1] [8] [2] [7] [4 3] [6] 25"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"J('[1] [8] [2] [7] [4 3] [6] 25 sqr')"
|
||||
"words"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" . [1] [8] [2] [7] [4 3] [6] 625 z-up\n",
|
||||
" [1] . [8] [2] [7] [4 3] [6] 625 z-up\n",
|
||||
" [1] [8] . [2] [7] [4 3] [6] 625 z-up\n",
|
||||
" [1] [8] [2] . [7] [4 3] [6] 625 z-up\n",
|
||||
" [1] [8] [2] [7] . [4 3] [6] 625 z-up\n",
|
||||
" [1] [8] [2] [7] [4 3] . [6] 625 z-up\n",
|
||||
" [1] [8] [2] [7] [4 3] [6] . 625 z-up\n",
|
||||
"[1] [8] [2] [7] [4 3] [6] 625 . z-up\n",
|
||||
"[1] [8] [2] [7] [4 3] [6] 625 . swons swap shunt\n",
|
||||
"[1] [8] [2] [7] [4 3] [6] 625 . swap cons swap shunt\n",
|
||||
"[1] [8] [2] [7] [4 3] 625 [6] . cons swap shunt\n",
|
||||
"[1] [8] [2] [7] [4 3] [625 6] . swap shunt\n",
|
||||
"[1] [8] [2] [7] [625 6] [4 3] . shunt\n",
|
||||
" [1] [8] [2] [7] [3 4 625 6] . \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"V('[1] [8] [2] [7] [4 3] [6] 625 z-up')"
|
||||
"[z-up] trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1] [8] [2 [3 4 625 6] 7]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"J('[1] [8] [2] [7] [3 4 625 6] z-up')"
|
||||
"z-up"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1 [2 [3 4 625 6] 7] 8]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"J('[1] [8] [2 [3 4 625 6] 7] z-up')"
|
||||
"z-up"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -304,45 +275,11 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" . [1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] infra\n",
|
||||
" [1 [2 [3 4 25 6] 7] 8] . [[[[[[sqr] dipd] infra] dip] infra] dip] infra\n",
|
||||
"[1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] . infra\n",
|
||||
" 8 [2 [3 4 25 6] 7] 1 . [[[[[sqr] dipd] infra] dip] infra] dip [] swaack\n",
|
||||
" 8 [2 [3 4 25 6] 7] 1 [[[[[sqr] dipd] infra] dip] infra] . dip [] swaack\n",
|
||||
" 8 [2 [3 4 25 6] 7] . [[[[sqr] dipd] infra] dip] infra 1 [] swaack\n",
|
||||
" 8 [2 [3 4 25 6] 7] [[[[sqr] dipd] infra] dip] . infra 1 [] swaack\n",
|
||||
" 7 [3 4 25 6] 2 . [[[sqr] dipd] infra] dip [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 25 6] 2 [[[sqr] dipd] infra] . dip [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 25 6] . [[sqr] dipd] infra 2 [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 25 6] [[sqr] dipd] . infra 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 25 4 3 . [sqr] dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 25 4 3 [sqr] . dipd [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 25 . sqr 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 25 . dup mul 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 25 25 . mul 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 625 . 4 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 625 4 . 3 [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 625 4 3 . [7] swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 6 625 4 3 [7] . swaack 2 [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 625 6] . 2 [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 625 6] 2 . [8] swaack 1 [] swaack\n",
|
||||
" 7 [3 4 625 6] 2 [8] . swaack 1 [] swaack\n",
|
||||
" 8 [2 [3 4 625 6] 7] . 1 [] swaack\n",
|
||||
" 8 [2 [3 4 625 6] 7] 1 . [] swaack\n",
|
||||
" 8 [2 [3 4 625 6] 7] 1 [] . swaack\n",
|
||||
" [1 [2 [3 4 625 6] 7] 8] . \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"V('[1 [2 [3 4 25 6] 7] 8] [[[[[[sqr] dipd] infra] dip] infra] dip] infra')"
|
||||
"[[[[[[sqr] dipd] infra] dip] infra] dip] [infra] trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -363,11 +300,20 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"define('Z == [[] cons cons] step i')"
|
||||
"clear"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"[Z [[] ccons] step i] inscribe"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -379,69 +325,36 @@
|
|||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
" . 1 [2 3 4] Z\n",
|
||||
" 1 . [2 3 4] Z\n",
|
||||
" 1 [2 3 4] . Z\n",
|
||||
" 1 [2 3 4] . [[] cons cons] step i\n",
|
||||
" 1 [2 3 4] [[] cons cons] . step i\n",
|
||||
" 1 2 [[] cons cons] . i [3 4] [[] cons cons] step i\n",
|
||||
" 1 2 . [] cons cons [3 4] [[] cons cons] step i\n",
|
||||
" 1 2 [] . cons cons [3 4] [[] cons cons] step i\n",
|
||||
" 1 [2] . cons [3 4] [[] cons cons] step i\n",
|
||||
" [1 2] . [3 4] [[] cons cons] step i\n",
|
||||
" [1 2] [3 4] . [[] cons cons] step i\n",
|
||||
" [1 2] [3 4] [[] cons cons] . step i\n",
|
||||
" [1 2] 3 [[] cons cons] . i [4] [[] cons cons] step i\n",
|
||||
" [1 2] 3 . [] cons cons [4] [[] cons cons] step i\n",
|
||||
" [1 2] 3 [] . cons cons [4] [[] cons cons] step i\n",
|
||||
" [1 2] [3] . cons [4] [[] cons cons] step i\n",
|
||||
" [[1 2] 3] . [4] [[] cons cons] step i\n",
|
||||
" [[1 2] 3] [4] . [[] cons cons] step i\n",
|
||||
"[[1 2] 3] [4] [[] cons cons] . step i\n",
|
||||
" [[1 2] 3] 4 [[] cons cons] . i i\n",
|
||||
" [[1 2] 3] 4 . [] cons cons i\n",
|
||||
" [[1 2] 3] 4 [] . cons cons i\n",
|
||||
" [[1 2] 3] [4] . cons i\n",
|
||||
" [[[1 2] 3] 4] . i\n",
|
||||
" . [[1 2] 3] 4\n",
|
||||
" [[1 2] 3] . 4\n",
|
||||
" [[1 2] 3] 4 . \n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"V('1 [2 3 4] Z')"
|
||||
"1 [2 3 4] [Z] trace"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"clear"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "markdown",
|
||||
"metadata": {},
|
||||
"source": [
|
||||
"And here it is doing the main thing."
|
||||
"And here it is doing the thing."
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"[1 [2 [3 4 625 6] 7] 8]\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"J('[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z')"
|
||||
"[1 [2 [3 4 25 6] 7] 8] [sqr] [dip dip infra dip infra dip infra] Z"
|
||||
]
|
||||
},
|
||||
{
|
||||
|
|
@ -463,13 +376,6 @@
|
|||
" [ n [ n [ n n x ...\n",
|
||||
" i d i d i d d Bingo!"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
}
|
||||
],
|
||||
"metadata": {
|
||||
|
|
|
|||
Loading…
Reference in New Issue