4 "cell_type": "markdown",
10 " [1 2 3 4] 5 remove\n",
11 " ------------------------\n",
14 " [1 2 3 4] 2 remove\n",
15 " ------------------------\n",
19 " ------------------------\n",
24 "First let's handle the case of an empty list:\n",
26 " remove == [pop not] [pop] [remove'] ifte\n",
28 "For non-empty lists, the predicate and base case are easy:\n",
30 " remove' == [swap first =] [pop rest] [R0] [R1] genrec\n",
32 "The recursive branch:\n",
34 " [1 2 3 4] 3 R0 [remove'] R1\n",
36 "For `R0` use `[uncons] dip`:\n",
38 " [1 2 3 4] 3 [uncons] dip [remove'] R1\n",
39 " [1 2 3 4] uncons 3 [remove'] R1\n",
40 " 1 [2 3 4] 3 [remove'] R1\n",
42 "For `R1` let's try `i cons`:\n",
44 " 1 [2 3 4] 3 [remove'] i cons\n",
45 " 1 [2 3 4] 3 remove' cons\n",
47 " 1 2 [3 4] 3 remove' cons cons\n",
49 " 1 2 [4] cons cons\n",
55 " remove' == [swap first =] [pop rest] [[uncons] dip] [i cons] genrec\n",
56 " remove == [pop not] [pop] [remove'] ifte\n"
67 "output_type": "stream",
72 "[remove' [swap first =] [pop rest] [[uncons] dip] [i cons] genrec] inscribe\n",
73 "[remo [pop not] [pop] [remove'] ifte] inscribe"
84 "output_type": "stream",
102 "output_type": "stream",
113 "cell_type": "markdown",
117 "So far so good but I made a mistake. The recursive part doesn't handle empty lists, so it's broken for the case of the item not being in the list:"
122 "execution_count": 4,
128 "output_type": "stream",
140 "execution_count": null,
149 "cell_type": "markdown",
153 "## Second attempt\n",
155 " remove == [pop not] [pop] [R0] [R1] genrec\n",
156 " remove == [pop not] [pop] [R0 [remove] R1] ifte\n",
160 " [a b c] item R0 [remove] R1\n",
164 " -----------------------------------------------------\n",
165 " [swap first =] [pop rest] [E1 [remove] E2] ifte\n",
167 "Recursive branch:\n",
169 " [a b c] d E1 [remove] E2\n",
173 " E1 == [uncons] dip\n",
176 " [a b c] d [uncons] dip [remove] i cons\n",
177 " a [b c] d [remove] i cons\n",
178 " a [b c] d remove cons\n",
182 " R0 == [swap first =] [pop rest]\n",
186 " R1 == [[uncons] dip [remove] i cons] ifte\n",
188 "But of course `[remove]` can't appear in there like that, we have to package it up:\n",
190 " R1 == [i cons] cons [[uncons] dip] swoncat ifte\n",
194 " [[uncons] dip remove cons] ifte\n",
196 " R1 == [cons] concat [[uncons] dip] swoncat ifte\n",
198 "Clunky, but it works:\n",
200 " remove == [pop not] [pop] [[swap first =] [pop rest]] [[cons] concat [[uncons] dip] swoncat ifte] genrec\n",
206 "execution_count": 6,
212 "output_type": "stream",
219 "[remo2 [pop not] [pop] [[swap first =] [pop rest]] [[cons] concat [[uncons] dip] swoncat ifte] genrec] inscribe"
224 "execution_count": 7,
230 "output_type": "stream",
242 "execution_count": 8,
248 "output_type": "stream",
260 "execution_count": 9,
266 "output_type": "stream",
277 "cell_type": "markdown",
281 "## Third attempt\n",
283 "What if we let `[remove]` be on the stack instead of building the else-part each iteration?:\n",
285 " remove == [pop not] [pop] [] [[P] [THEN] [ELSE] ifte] genrec\n",
286 " remove == [pop not] [pop] [ [remove] [P] [THEN] [ELSE] ifte] ifte\n",
290 " [a b c] item [remove] [P] [THEN] [ELSE] ifte\n",
292 " P == pop swap first =\n",
294 " THEN == popop rest\n",
300 " [a b c] item [remove] ELSE\n",
301 " [a b c] item [remove] [uncons] dipd i cons\n",
302 " a [b c] item [remove] i cons\n",
303 " a [b c] item remove cons\n",
310 " ELSE == [uncons] dipd i cons\n",
314 " remove == [pop not] [pop] [] [[pop swap first =] [popop rest] [[uncons] dipd i cons] ifte] genrec"
319 "execution_count": 10,
325 "output_type": "stream",
332 "[remo3 [pop not] [pop] [] [[pop swap first =] [popop rest] [[uncons] dipd i cons] ifte] genrec] inscribe"
337 "execution_count": 11,
343 "output_type": "stream",
355 "execution_count": 12,
361 "output_type": "stream",
373 "execution_count": 13,
379 "output_type": "stream",
391 "execution_count": 14,
397 "output_type": "stream",
409 "execution_count": 15,
415 "output_type": "stream",
427 "execution_count": 16,
433 "output_type": "stream",
445 "execution_count": 17,
451 "output_type": "stream",
463 "execution_count": null,
472 "display_name": "Joypy",
477 "file_extension": ".joy",
478 "mimetype": "text/plain",