Derive zip.

This commit is contained in:
sforman
2023-10-09 11:45:28 -07:00
parent 787dc6a7b3
commit da0e1e685e
5 changed files with 137 additions and 27 deletions
+54
View File
@@ -0,0 +1,54 @@
# Zip
Let's derive `zip`.
[a b c ...] [e f g ...] zip
---------------------------------
[[a e] [b f] [c g] ...]
It's a `genrec`:
zip == [null] [popop []] [R0] [R1] genrec
If the top list is empty, pop both lists and put a new empty list...
Hmm...
zip == [null] [pop] [R0] [R1] genrec
We will assume that both lists are the same size, so if the top list is empty the second list shall be too, and we can reuse it to store our pairs.
Now then, we have two non-empty lists:
[a b c ...] [e f g ...] R0 [zip] R1
Let's imagine a function `shift-pair`:
[a ...] [e ...] shift-pair
--------------------------------
[a e] [...] [...]
I'm going to defer derivation of that for now.
[a b c ...] [e f g ...] shift-pair [zip] R1
[a e] [b c ...] [f g ...] [zip] R1
And so `R1` is `i cons` (it's a list builder.)
zip == [null] [pop] [shift-pair] [i cons] genrec
And now:
shift-pair == uncons-two [quote-two] dipd
w/
uncons-two == [uncons] ii swapd
quote-two == unit cons
[zip [null] [pop] [shift-pair] [i cons] genrec] inscribe
[shift-pair uncons-two [quote-two] dipd] inscribe
[uncons-two [uncons] ii swapd] inscribe
[quote-two unit cons] inscribe