From 9998e7947b2a2dcaf5498645741b37bd33fc194e Mon Sep 17 00:00:00 2001
From: sforman We can apply it to a quoted program consisting of some value Let Evaluation would go like this:a and some function B:[a B] x
+[a B] dup i
+[a B] [a B] i
[a B] a B
B function swap the a with the quote and run some function C on it to generate a new value b:
+B == swap [C] dip
-
-[a B] a B
+[a B] a B
[a B] a swap [C] dip
a [a B] [C] dip
a C [a B]
@@ -345,45 +348,59 @@ otherwise we discard the term and the generator leaving the sum on the stack:
joy? [pop >4M] [popop] [[PE2.1] dip third-term] tailrec
4613732
-Consider the Fib seq with algebraic variables:
a b
b a+b
a+b a+b+b
a+b+b a+a+b+b+b
-So if (a,b) and a is even then the next even term pair is (a+2b, 2a+3b)
+So starting with (a b) and assuming a is even then the next even term pair is (a+2b, 2a+3b).
Reconsider:
[b a F] x
[b a F] b a F
From here we want to arrive at:
(a+2b) [(2a+3b) (a+2b) F]
-
-b a F
-b a [F0] [F1] fork
-
- b a over [+] ii
----------------------
- a+2b
-And:
- b a over [dup + +] ii
----------------------------
- 2a+3b
+Let's derive F. We have two values and we want two new values so that's a clop:
+b a F
+b a [F0] [F1] clop
+
+Where F0 computes a+2b:
+F0 == over [+] ii
+b a over [+] ii
+b a b [+] ii
+b a + b +
+b+a b +
+a+2b
+
+And F1 computes 2a+3b:
+F1 == over [dup + +] ii
-[over [dup + +] ii] [over [+] ii] clop
-roll< rrest [tuck] dip ccons
+b a over [dup + +] ii
+b a b [dup + +] ii
+b a dup + + b dup + +
+b a a + + b b + +
+...
+2a+3b
+
+So after that we have
+[b a F] (a+2b) (2a+3b) F'
-
-[b a F] b a F
-
-[b a F] (2a+3b) (a+2b) roll<
-(2a+3b) (a+2b) [b a F] rrest
-(2a+3b) (a+2b) [F] [tuck] dip ccons
-
-
-joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
+ [b a F] b‴ a‴ roll<
+ b‴ a‴ [b a F] rrest
+ b‴ a‴ [F] [tuck] dip ccons
+ b‴ a‴ tuck [F] ccons
+a‴ b‴ a‴ [F] ccons
+a‴ [b‴ a‴ F]
+
+Putting it all together (and deferring factoring) we have:
+F == [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons
+
+Let's try it out:
+joy? [1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
[1 0 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
joy? x
@@ -399,5 +416,7 @@ joy? x
2 8 34 144 [233 144 [over [dup + +] ii] [over [+] ii] clop roll< rrest [tuck] dip ccons]
And so it goes...
+Conclusion
+Generator programs like these are fun and interesting.