ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ» º Lesson 7 Part 150 F-PC 3.5 Tutorial by Jack Brown º ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Using Recursion in Forth Programming ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ I can remember, it seems like only yesterday, studying arithmetic progressions in high school. I don't know why but for some reason I hated them. We started of with a definition something like the following: Definition 5.0101 An arithmetic progression is a sequence in which each term after the first is obtained by adding the same fixed number, called the common difference, to the preceding term. I realize now what the problem was, and why I was bothered! This is a recursive definition. Apparently humans are supposed to relate well to recursive definitions. But why do we call this a recursive definition? It is because the next term of the progression is defined in terms of the previous one. Here is what the definition is saying. If d is the common difference, a fixed constant for any given arithmetic progression then you can find the nth term by the following formula: A(n) = A(n-1) + d (n) and (n-1) are subscripts where A(0) = a the 0th term ( some call it the 1st) Example: if A(0) = a = 3 and d = 5 then then: A(1) = A(0) + d = 3 + 5 = 8 A(2) = A(1) + d = 8 + 5 = 13 A(3) = A(2) + d = 13 + 5 = 18 And so on and so on ad infinitum! But what's all this got to do with FORTH programming? Well... we can write a FORTH program to find the nth term of an arithmetic progression even though the only formula I have given for the nth term is "recursive" Here is the algorithm: To find A(n): examine n: IF ( n is not zero ) call myself with argument (n-1) and add d, the common difference to result. ELSE ( n is equal to zero ) then the answer is just A(0) = a , the 0th term. End of algorithm: Implementing this algorithm in FORTH is going to present a slight problem. If we call the procedure APN for nth term of an arithmetic progression. Then we are going to have to use the procedure (word) APN in its own definition. FORTH program for computing the nth term of an arithmetic progression: VARIABLE A 3 A ! ( the 0th term ) VARIABLE D 5 D ! ( the common difference ) : APN ( n -- nth) DUP IF ( n is not zero ) 1- APN D @ + ELSE ( n is zero ) DROP A @ THEN ; If you try to enter this you are in for a rude surprise. FORTH does not like you using a definition in itself! Depending on the FORTH system you are using the procedure for getting a word to compile itself is different. If you are using a fig-FORTH or FORTH-79 and some FORTH-83 systems you would replace the word APN in the middle of the definition with the word MYSELF or RECURSE Check your system to see if you have these words. If you don't here are their definitions. : MYSELF LAST @ NAME> , ; IMMEDIATE \ F83 definition : MYSELF LAST @ NAME> X, ; IMMEDIATE \ F-PC definition : RECURSE LAST @ NAME> , ; IMMEDIATE \ F83 definition : RECURSE LAST @ NAME> X, ; IMMEDIATE \ F-PC definition \ Already in F-PC 3.5 You don't need both, either one will do. LAST is just a variable which contains the name field address of the most recently defined word. NAME> converts the name field address into the compilation address or cfa and the , compiles the cfa into the dictionary. The definition must be immediate so that MYSELF or RECURSE execute during compilation so as to compile the word being defined ( APN in our example ) ÚÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ¿ ³ Please Move to Lesson 7 Part 160 ³ ÀÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÙ