papierkorb:recurse.blk
Unterschiede
Hier werden die Unterschiede zwischen zwei Versionen angezeigt.
| papierkorb:recurse.blk [2025-08-16 17:50] – ↷ Seite von projects:recurse.blk nach papierkorb:recurse.blk verschoben mka | papierkorb:recurse.blk [Unbekanntes Datum] (aktuell) – gelöscht - Externe Bearbeitung (Unbekanntes Datum) 127.0.0.1 | ||
|---|---|---|---|
| Zeile 1: | Zeile 1: | ||
| - | === Tutorial on Recursion === | ||
| - | < | ||
| - | |||
| - | print Screen 0 not modified | ||
| - | 0 \ Examples for lecture number eight. | ||
| - | 1 \ Last change: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 1 not modified | ||
| - | 0 \ A Tutorial Recursion in FORTH Programming. | ||
| - | 1 \ I can remember, it seems like only yesterday, studying | ||
| - | 2 \ arithmetic progressions in high school. | ||
| - | 3 \ for some reason I hated them. We started of with a definition | ||
| - | 4 \ something like the following: | ||
| - | | ||
| - | 6 \ Definition 5.0101 | ||
| - | 7 \ which each term after the first is obtained by adding the same | ||
| - | 8 \ fixed number, called the common difference, to the preceding | ||
| - | 9 \ term. | ||
| - | 10 | ||
| - | 11 \ I realise now what the problem was, and why I was bothered! | ||
| - | 12 \ This is a recursive definiton. | ||
| - | 13 \ to relate well to recursive definitions. | ||
| - | 14 \ this a recursive definition? | ||
| - | 15 \ the progression is defined in terms of the previous one. | ||
| - | |||
| - | |||
| - | Screen 2 not modified | ||
| - | 0 \ Here is what the definition is saying. | ||
| - | 1 \ If d is the common difference, a fixed constant for any given | ||
| - | 2 \ arithmetic progression then you can find the nth term by the | ||
| - | 3 \ following formula: | ||
| - | 4 \ A(n) = A(n-1) + d (n) and (n-1) are subscripts | ||
| - | 5 \ | ||
| - | | ||
| - | 7 \ Example: | ||
| - | 8 \ then: A(1) = A(0) + d = 3 + 5 = 8 | ||
| - | 9 \ A(2) = A(1) + d = 8 + 5 = 13 | ||
| - | 10 \ A(3) = A(2) + d = 13 + 5 = 18 | ||
| - | 11 \ And so on and so on adinfinitum! | ||
| - | 12 \ But whats all this got to do with FORTH programming? | ||
| - | 13 \ Well... we can write a FORTH program to find the nth term | ||
| - | 14 \ of an arithmetic progression even though the only formula | ||
| - | 15 \ I have given for the nth term is " | ||
| - | |||
| - | |||
| - | Screen 3 not modified | ||
| - | 0 \ Here is the algorithm: | ||
| - | 1 \ To find A(n): | ||
| - | 2 \ | ||
| - | 3 \ IF ( n is not zero ) | ||
| - | 4 \ call myself with argument (n-1) | ||
| - | 5 \ and add d, the common difference to result. | ||
| - | 6 \ ELSE ( n is equal to zero ) | ||
| - | 7 \ then the answer is just | ||
| - | 8 \ A(0) = a , the 0th term. | ||
| - | 9 \ End of algorithm: | ||
| - | 10 | ||
| - | 11 \ Implementing this algorithm in FORTH is going to present a | ||
| - | 12 \ slight problem. | ||
| - | 13 \ of an arithmetic progression. | ||
| - | 14 \ Then we are going to have to use the procedure | ||
| - | 15 \ (word) APN in its own definition. | ||
| - | |||
| - | |||
| - | Screen 4 not modified | ||
| - | 0 \ FORTH program for computing the nth term | ||
| - | 1 \ of an arithmetic progression: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 \ If you try to enter this you are in for a rude suprize. | ||
| - | 13 \ FORTH does not like you using a definition in itself! | ||
| - | 14 \ Depending on the FORTH system you are using the procedure | ||
| - | 15 \ for getting a word to compile itself is different. | ||
| - | |||
| - | |||
| - | Screen 5 not modified | ||
| - | 0 \ If you are using a fig-FORTH or FORTH-79 | ||
| - | 1 \ and some FORTH-83 systems you would replace the word APN in | ||
| - | 2 \ the middle of the definition with the word MYSELF or RECURSE | ||
| - | 3 \ Check your system to see if you have these words. If you don't | ||
| - | 4 \ here are their definitons. | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 9 \ You don't need both, either one will do. LAST is just a | ||
| - | 10 \ variable which contains the name field address of the most | ||
| - | 11 \ recently defined word. NAME> converts the name field address | ||
| - | 12 \ into the compiliation address or cfa and the , compiles | ||
| - | 13 \ the cfa into the dictionary. | ||
| - | 14 \ so that MYSELF or RECURSE | ||
| - | 15 \ compile the word being defined ( APN in our example ) | ||
| - | |||
| - | |||
| - | Screen 6 not modified | ||
| - | 0 \ Here is how the definition would look using | ||
| - | 1 \ MYSELF . I have added a line at the beginning of the word | ||
| - | 2 \ definition and at the end of the word definition to monitor | ||
| - | 3 \ the stack and status. | ||
| - | 4 \ what is going on. | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 DUP IF ( n is not zero ) | ||
| - | 11 1- MYSELF | ||
| - | 12 ELSE ( n is zero ) | ||
| - | 13 DROP A @ | ||
| - | 14 | ||
| - | 15 CR ." Leaving " .S ; | ||
| - | |||
| - | |||
| - | Screen 7 not modified | ||
| - | 0 \ If you are using Laxen and Pery F83 for the PC | ||
| - | 1 \ there is a different approach to making recursive definitions. | ||
| - | 2 \ In L&P you can simply declare a word to be recursive using | ||
| - | 3 \ the word RECURSIVE or course, and then you can use the word | ||
| - | 4 \ you are defining in its own definition. Here is L&P version: | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 DUP IF ( n is not zero ) | ||
| - | 11 1- APN D @ + | ||
| - | 12 ELSE ( n is zero ) | ||
| - | 13 DROP A @ | ||
| - | 14 | ||
| - | 15 CR ." Leaving " .S ; | ||
| - | |||
| - | |||
| - | Screen 8 not modified | ||
| - | 0 \ Teacher Hat On! Now here is your homework. | ||
| - | 1 \ I didn't like the definition I was given for the geometric | ||
| - | 2 \ progression any better than the one I was given for the | ||
| - | 3 \ arithmetic progression ( you see it to is recursive). | ||
| - | 4 \ Here it is: | ||
| - | 5 \ Definition 14.2013 | ||
| - | 6 \ which each term after the first is obtained by multiplying | ||
| - | 7 \ the same fixed number, called the common ratio, by the | ||
| - | 8 \ preceding term. | ||
| - | 9 \ Your homework: Write a recursive definition to compute the | ||
| - | 10 \ nth term of a geometric progression. | ||
| - | 11 \ Use G(0) = A for the first term. and R for the common ratio. | ||
| - | 12 \ With A = 3 and R = 2 your the geomtric progression would be: | ||
| - | 13 | ||
| - | 14 \ 3, 6, 12, 24, 48, 96, etc | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 9 not modified | ||
| - | 0 \ Hints on homework problem: | ||
| - | 1 \ Here is the word algorithm for the geometric progression. | ||
| - | 2 \ To find G(n): | ||
| - | 3 \ | ||
| - | 4 \ IF ( n is not zero ) | ||
| - | 5 \ call myself with argument (n-1) | ||
| - | 6 \ and multiply the result by r, the common ratio. | ||
| - | 7 \ ELSE ( n is equal to zero ) | ||
| - | 8 \ then the answer is just | ||
| - | 9 \ G(0) = a , the 0th term. | ||
| - | 10 \ End of algorithm: | ||
| - | 11 \ The program would start as follows: | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 : GPN ( n nth) | ||
| - | 15 Opps, I just realized I'm doing your homework! | ||
| - | |||
| - | |||
| - | Screen 10 not modified | ||
| - | 0 \ More examples of recursive definitons. | ||
| - | 1 \ Remove the lines at the begining and end of each definition | ||
| - | 2 \ that print the stack once you understand what is going on. | ||
| - | 3 \ FACTORIAL | ||
| - | 4 \ Recursive definition: | ||
| - | 5 \ Examples: | ||
| - | 6 \ Algorithm: | ||
| - | 7 \ To fine n!: | ||
| - | 8 \ | ||
| - | 9 \ IF ( n is not zero ) | ||
| - | 10 \ call myself with n-1 | ||
| - | 11 \ multiply result by n | ||
| - | 12 \ ELSE ( n is zero ) | ||
| - | 13 \ return result of 1 | ||
| - | 14 \ | ||
| - | 15 \ End of n!; | ||
| - | |||
| - | |||
| - | Screen 11 not modified | ||
| - | 0 \ Program for n! | ||
| - | 1 : FACTORIAL | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 \ If you try this with negative n you will be sorry! | ||
| - | 11 \ Fix it so an error message is given if negative is on stack. | ||
| - | 12 \ What is the largest factorial you can calculate with this | ||
| - | 13 \ definition? | ||
| - | 14 \ Implement FACTORIAL using double numbers. | ||
| - | 15 \ If you don't have it is on the FORTH Board in Area 2 ! | ||
| - | |||
| - | |||
| - | Screen 12 not modified | ||
| - | 0 \ Power function | ||
| - | 1 \ Definition: | ||
| - | 2 \ Algorithm: | ||
| - | 3 \ To find 2^n: | ||
| - | 4 \ | ||
| - | 5 \ IF ( n not zero) call myself with argument n-1 | ||
| - | 6 \ and multiply the result by 2 | ||
| - | 7 \ ELSE ( n is zero ) just return with 1 for the answer. | ||
| - | 8 \ | ||
| - | 9 \ Program: | ||
| - | 10 : 2^n ( n 2^n ) RECURSIVE | ||
| - | 11 CR ." Entering" | ||
| - | 12 DUP 0> IF | ||
| - | 13 ELSE DROP 1 | ||
| - | 14 | ||
| - | 15 CR ." Leaving " .S ; | ||
| - | |||
| - | |||
| - | Screen 13 not modified | ||
| - | 0 \ The famous Fibonacci numbers! | ||
| - | 1 \ 0 1 1 2 3 5 8 13 21 34 etc | ||
| - | 2 \ Definition: | ||
| - | 3 \ F(n) = n if n = 0 or 1 | ||
| - | 4 \ Wow! this one is doubly recursive. | ||
| - | 5 \ Here is the program. | ||
| - | 6 : FIBONACCI ( n | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 SWAP 2- FIBONACCI | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 CR ." leaving " .S ; | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 14 not modified | ||
| - | 0 \ Greatest common divisor | ||
| - | 1 \ The greatest common divisor of two numbers a and b is the | ||
| - | 2 \ largest number n that will divide into both a and b. | ||
| - | 3 \ Here is the recursive definition, | ||
| - | | ||
| - | 5 : GCD ( a b n ) | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 CR ." Leaving " .S ; | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 15 not modified | ||
| - | 0 \ Recursove Bubble Sort on the Stack. | ||
| - | | ||
| - | 2 \ BUBBLE does one pass. It is the recursive word! | ||
| - | 3 : BUBBLE | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 \ You can get rid of the entering and leaving . | ||
| - | 11 \ To use: put any numbers on the stack and type SORT | ||
| - | 12 : SORT ( n n n n ... m m m m ... sorted ) | ||
| - | 13 DEPTH 1 > IF | ||
| - | 14 DEPTH 1- 0 DO BUBBLE | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 16 not modified | ||
| - | 0 \ Directional Recursive Bubble Sort on the stack. | ||
| - | | ||
| - | 2 : ASCENDING | ||
| - | 3 : COMPARE | ||
| - | | ||
| - | 5 : BUBBLE | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 | ||
| - | 11 | ||
| - | 12 | ||
| - | 13 : SORT ( n n n n ... m m m m ... sorted ) | ||
| - | 14 DEPTH 1 > IF | ||
| - | 15 DEPTH 1- 0 DO BUBBLE | ||
| - | |||
| - | |||
| - | Screen 17 not modified | ||
| - | 0 \ TOWERS-1 | ||
| - | 1 \ The Famous Towers of Hanoi by Peter Midnight. | ||
| - | 2 \ From FORTH Dimensions Volume 2 Number 2. | ||
| - | 3 \ Converted to Laxen and Perry F83 by Jack Brown. | ||
| - | 4 \ The original author left out the stack comments and I don' | ||
| - | 5 \ have time to put them in today. Send me a copy with them in! | ||
| - | 6 : CLS ( -- -- ) 27 EMIT ." [2J" ; \ Clear screen. | ||
| - | 7 CODE AT ( row col -- ) \ position cursor. | ||
| - | | ||
| - | | ||
| - | 10 \ : 4DUP ( a b c d a b c d a b c d ) 2OVER 2OVER ; | ||
| - | 11 12 CONSTANT NMAX VARIABLE (N) NMAX (N) ! | ||
| - | 12 : N ( -- n ) (N) @ ; \ fetch value of n. | ||
| - | 13 61 CONSTANT COLOR | ||
| - | 14 VARIABLE HFO 3 HFO ! | ||
| - | 15 VARIABLE RING N ALLOT | ||
| - | |||
| - | |||
| - | Screen 18 not modified | ||
| - | 0 \ TOWERS-2 | ||
| - | | ||
| - | 2 : DELAY ( n -- ) | ||
| - | | ||
| - | 4 : POS ( n | ||
| - | | ||
| - | 6 : HALFD ( char size -- ) | ||
| - | | ||
| - | 8 : < | ||
| - | | ||
| - | 10 THEN EMIT HALFD ; | ||
| - | 11 : DISPLAY | ||
| - | 12 SWAP >R -ROT OVER - R@ AT | ||
| - | 13 R> -ROT < | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 19 not modified | ||
| - | 0 \ TOWERS-3 | ||
| - | | ||
| - | 2 : PRESENCE ( n flag ) | ||
| - | | ||
| - | 4 : LINE | ||
| - | | ||
| - | | ||
| - | 7 : RAISE | ||
| - | | ||
| - | | ||
| - | 10 -1 +LOOP 2DROP ; | ||
| - | 11 : LOWER | ||
| - | 12 DUP POS SWAP LINE 1+ 2 | ||
| - | 13 DO 2DUP I 1- BL DISPLAY 2DUP I COLOR DISPLAY | ||
| - | 14 LOOP 2DROP ; | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 20 not modified | ||
| - | 0 \ TOWERS-4 | ||
| - | 1 : MOVELEFT | ||
| - | | ||
| - | | ||
| - | | ||
| - | 5 : MOVERIGHT | ||
| - | | ||
| - | | ||
| - | | ||
| - | 9 : TRAVERS | ||
| - | 10 2DUP > IF MOVELEFT ELSE MOVERIGHT THEN ; | ||
| - | 11 | ||
| - | 12 : MOVE1 | ||
| - | 13 KEY? IF 0 16 AT ABORT THEN | ||
| - | 14 -ROT 2DUP RAISE >R 2DUP R> ROT TRAVERS | ||
| - | 15 2DUP RING + 1- C! SWAP LOWER ; | ||
| - | |||
| - | |||
| - | Screen 21 not modified | ||
| - | 0 \ TOWERS-5 | ||
| - | 1 : MULTIMOV | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 6 : MAKETOWER | ||
| - | | ||
| - | 8 : MAKEBASE | ||
| - | | ||
| - | 10 : MAKERING | ||
| - | 11 2DUP RING + 1- C! SWAP LOWER ; | ||
| - | 12 : SETUP | ||
| - | 13 CLS N 1+ 0 DO 1 RING I + C! LOOP 3 0 DO I | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | Screen 22 not modified | ||
| - | 0 \ TOWERS-6 | ||
| - | 1 \ Values of n larger than 7 take a fair amount of time. | ||
| - | 2 : TOWERS | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | | ||
| - | 10 0 0 AT | ||
| - | 11 N 0 DO BEEP 5 DELAY LOOP ; | ||
| - | 12 | ||
| - | 13 : HANOI 7 TOWERS ; | ||
| - | 14 | ||
| - | 15 | ||
| - | |||
| - | |||
| - | </ | ||
papierkorb/recurse.blk.1755359401.txt.gz · Zuletzt geändert: 2025-08-16 17:50 von mka