home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Power-Programmierung
/
CD1.mdf
/
forth
/
compiler
/
fpc
/
tutor
/
l7p160
< prev
next >
Wrap
Text File
|
1990-07-15
|
3KB
|
89 lines
╔═════════════════════════════════════════════════════╗
║ Lesson 7 Part 160 F-PC 3.5 Tutorial by Jack Brown ║
╚═════════════════════════════════════════════════════╝
┌───────────────────────────────────────┐
│ Arithmetic Progression by Recursion │
└───────────────────────────────────────┘
Here is how the definition would look using MYSELF . I have added a
line at the beginning of the word definition and at the end of the word
definition to monitor the stack and status. Remove these lines when you
understand what is going on.
VARIABLE A 3 A ! ( the 0th term )
VARIABLE D 5 D ! ( the common difference )
: APN ( n nth)
CR ." Entering " .S
DUP IF ( n is not zero )
1- MYSELF D @ + \ Use RECURSE for F-PC
ELSE ( n is zero )
DROP A @
THEN
CR ." Leaving " .S ;
If you are using Laxen and Perry F83 or F-PC for the PC there is a
different approach to making recursive definitions. You can simply
declare a word to be recursive using the word RECURSIVE , and then you
can use the word you are defining in its own definition. Here is F-PC
version:
VARIABLE A 3 A ! ( the 0th term )
VARIABLE D 5 D ! ( the common difference )
: APN ( n nth) RECURSIVE \ Recursive declaration
CR ." Entering " .S
DUP IF ( n is not zero )
1- APN D @ +
ELSE ( n is zero )
DROP A @
THEN
CR ." Leaving " .S ;
┌─────────────────────────────────────────────────┐
│ Recursive Definition of Geometric Progression │
└─────────────────────────────────────────────────┘
╓────────────────╖
║ Problem 7.11 ║
╙────────────────╜
I didn't like the definition I was given for the geometric progression
any better than the one I was given for the arithmetic progression ( you
see it to is recursive). Here it is:
Definition 14.2013 A geometric progression is a sequence in which each
term after the first is obtained by multiplying the same fixed number,
called the common ratio, by the preceding term. Your homework: Write a
recursive definition to compute the nth term of a geometric progression.
Call the word GPN . Use G(0) = A for the first term. and R for the
common ratio. With A = 3 and R = 2 your the geometric progression would
be:
3, 6, 12, 24, 48, 96, etc
The problem is to write a recursive definition to compute geometric
progressions. Hints on homework problem:
Here is the word algorithm for the geometric progression.
To find G(n):
examine n:
IF ( n is not zero )
call myself with argument (n-1)
and multiply the result by r, the common ratio.
ELSE ( n is equal to zero )
then the answer is just
G(0) = a , the 0th term.
End of algorithm:
The program would start as follows:
VARIABLE A 3 A ! ( The first term )
VARIABLE R 2 R ! ( The common ratio )
: GPN ( n nth) RECURSIVE ( if you are using L&P F83 )
Opps, I just realized I'm doing your homework!
┌────────────────────────────────────┐
│ Please Move to Lesson 7 Part 170 │
└────────────────────────────────────┘