Doing Recursive Programming In SiMPLE
As you already know, a SiMPLE task (or function) can call other tasks. But a task can also call itself. Whenever a task invokes itself (either directly or indirectly), the task is said to be "Recursive".
As an example of creating a small recursive task (recursive ones quite often
are small!), let's write a Pro-SiMPLE program that
starts at an initial location on the graphics screen (X=80, Y=0) and draws a
vertical line straight down to a new location. It then turns left, and draws a
slightly shorter horizontal line from there. It then turns left again, and
draws an even shorter vertical line straight up. It then turns left again,
and ... so on:
move to (80, 0)
draw to (0, 1, 475) @
Task draw to (Int dx, Int dy, Int long)
If long<=0 Return
line rel (long*dx, long*dy)
draw to (dy, -dx, long-5) @
(It is interesting to note that, even though the program draws many lines,
no Do-Loops are used!)
|
When writing a recursive task, it is important to keep in mind the phrase, "The buck stops here!" A task cannot just go on calling itself forever, or else your program will never end. (Actually, it will ... when your system crashes!) Hence the task's If-statement that stops the recursion and returns back to the caller when the length of the requested line becomes too small.
To see some more examples of SiMPLE recursive programs,
look at "Chess", "Dragon Curves", and "Tower of Hanoi"
in The SiMPLE Program Gallery.
[
Webmaster |
FAQ's |
Home Page |
Contact Us ]