What do you expect to learn? Using recursion is simple enough, the best way is through practice. More complicated is learning when and how to avoid recursion when it is inefficient.
When using recursion with Maple, one can frequently use the remember/cache option to significantly speed up a compuation. For example, consider the classic Fibonacci sequence:
f(0) = 0: f(1) = 1; f(n) = f(n-1) + f(n-2):
fib := proc(n::nonnegint)
if n = 0 then return 0
elif n = 1 then return 1
else return fib(n-1) + fib(n-2);
end if:
end proc:
This is quite inefficient:
time(fib(30));
8.150
However, by adding the cache option, so that previous calls are cached, it becomes significantly faster
fib := proc(n::nonnegint)
option cache;
if n = 0 then return 0
elif n = 1 then return 1
else return fib(n-1) + fib(n-2);
end if:
end proc:
time(fib(1000));
0.012
It is also possible to explicitly store results without using a cache/remember table. That can be done with an assignment statement, fib(1) := 1.
goal
What do you expect to learn? Using recursion is simple enough, the best way is through practice. More complicated is learning when and how to avoid recursion when it is inefficient.
When using recursion with Maple, one can frequently use the remember/cache option to significantly speed up a compuation. For example, consider the classic Fibonacci sequence:
This is quite inefficient:
However, by adding the cache option, so that previous calls are cached, it becomes significantly faster
It is also possible to explicitly store results without using a cache/remember table. That can be done with an assignment statement, fib(1) := 1.