t> PROGRAMMING WITH MAPLE
b>
t> As was mentioned in the introduction to this tutorial, over 90% of the
t> built-in commands in Maple are programmed in Maple's own Pascal-like
t> programming language. Procedural programming is the very heart of
t> Maple.  While many useful and exciting things can be done in Maple
t> without doing any programming at all, when you become adept at using
t> Maple, you most likely will find yourself delving deeper and deeper
t> into the programming possibilities.
b>
t> There are two major reasons to use Maple's programming language. The
t> most straightforward reason is to use its constructs to automate
t> repetitive calculations you are currently doing in "command-line" mode. 
t> The more important reason is to create your own Maple commands, augmenting
t> the existing command library. The former of these reasons can be
t> explained quite thoroughly here, the latter is well beyond this
t> tutorial.
b>
c1>
t> Basic Constructs
b>
t> Maple offers the basic programming constructs that are found in most
t> other programming languages. Following is a short tour of each of
t> them.
b>
c1>
t> if/then/else/fi
b>
t> Maple handles conditional statements with the if/then/else/if
t> construct.  Basically, you provide Maple with conditions (that evaluate
t> to either true or false) and Maple branches as you specify. As
t> always, an example is worth a thousand words.
b>
x> if 9/23 < 13/31 then 13/31 else 9/23 fi;
t> The above example compares two fractions and prints out the larger.
t> Note the "fi" (if backwards) at the end of the statement - this is the
t> terminator for the structure. Any amount of Maple code can be inserted
t> after each of "then" and "else"; you need not restrict yourself to a single
t> command as above. Note also that the entire command ends in a semicolon.
t> This is not a coincidence; the if/then/else/fi is a Maple expression as
t> much as any command or polynomial and must end with a valid terminator.
b>
c1>
t> An extra element, "elif", can be added to the structure to make it
t> if/then/elif/then/.../else/fi. Any number of these secondary conditions
t> can be provided.
b>
x> if isprime(221) then p elif numtheory[issqrfree](221) then s else neither fi;
c1>
t> for/from/by/to/do/od
b>
t> Maple handles repetition with two different constructs. The first,
t> for/from/by/to/do/od takes an upper and lower bound on a repetition
t> variable, as well as a step value to get from the lower to the upper
t> bound, and performs the statements between the "do" and the "od"
t> the appropriate number of times. The repetition variable can be
t> included in the calculations, but does not strictly have to be. Again,
t> as with if/then/else/fi, any number of Maple statements can be included
t> between "do" and "od".
b>
c1>
x> for i from 1 to 11 by 2 do print(i!) od;
c1>
t> while/do/od
b> 
t> Repetition is also achieved with the while/do/od structure. Maple
t> loops through the statement(s) between "do" and "od" as long as the
t> condition stated after the "while" is true.
b>
c1>
x> x := 1;
x> while x < 100 do x := ithprime(x); print(x); od:
c1>
t> Procedures
b>
t> Above we learned how to use the basic constructs found in Maple's
t> programming language. But these examples will only work when they
t> are entered in full. The major advantage in programming is that you
t> can set up procedures that automate these calculations for you. In
t> order to create a procedure you can use over and over again with
t> different input, use the proc/end structure.
b>
t> When you define a Maple procedure you extend the functionality of the
t> system. And if you save the procedure appropriately, this extension can
t> be carried over to other Maple sessions. In the following example a
t> very simple procedure, largestfactor, takes an integer, n, and
t> finds the largest prime factor of that integer (and its multiplicity).
b>
c1>
x> largestfactor := proc(n:integer) op(nops(ifactor(n)), ifactor(n)); end;
t> Basically, we have pulled off the last operand (the number of which is
t> equal to the number of operands) of the integer factorization of n.
t> Let's try it a few times.
b>
c1>
x> largestfactor(2387);
x> largestfactor(118277523);
c1>
t> Of course, this is a extremely simplified example of a Maple
t> procedure.  Many of the procedures in the Maple library are hundreds of
t> lines long and contain extensive data type testing, error checking, and
t> alternative algorithms.
b>
t> For a more detailed explanation of Maple programming, refer to your
t> Maple manuals.
b>
eof>
