Maple/Programming with Maple

From Wikibooks, open books for an open world
Jump to navigation Jump to search

Maple is a programming language as well as a mathematical system.

Maple, as a programming language, has several attributes common to object oriented languages. In particular variables and procedure parameters can hold any valid Maple object. In principle Maple has no declarations, it is up to the functions and up to the user to enforce typing restrictions.

Maple is a procedural programming language. It also includes a number of functional pro­gramming constructs. Maple is a dynamically typed, interpreted language. Ie not strongly typed like C and Pascal. No declarations are required.

Procedures[edit | edit source]

All Maple procedures are functions, in that they are given values or names of objects as arguments, and return a value as a result.

In Maple, functions can be assigned to variables in the same way numbers or expressions can be.

One form of Maple function definition consists assigning a programming variable a proc expression.

Each variable in a Maple procedure is either a formal parameter, a local variable or a global variable.

Only two possibilities exist: either a variable is local to the one procedure which it immediately occurs in, or it is global to the entire Maple session. Local variables are local only to their own procedure. They are not known in other procedures, even within procedures which appear within the procedure which defines them.

If you do not declare your variables to be global or local, Maple decides for you. If a variable appears on the left-hand side of an explicit assignment, then Maple assumes that you intend the variable to be local. Otherwise , Maple assumes than the variable is global to the whole session.

Maple evaluates parameters to zero levels, local variables to one level and global variables fully, except for last name evaluation.

The standard Maple behavior is that a command does not directly affect the value of the parameters passed to it.

In terms of traditional parameter passing mechanisms used by various computer languages, Maple's parameter passing could be termed called by evaluated name.

Maple evaluates the arguments before invoking the procedure. Once, Maple evaluates the arguments, it replaces all occurrences of the procedure's formal parameters with the actual arguments. Then Maple invokes the procedure.

When you call a procedure, Maple evaluates the arguments appropriately, given the context in which the call occurs.

The modest performance penalty incurred by type checking is usually worth the price because it reduces programming or usage errors.

Maple creates the local variables of a procedure each time you call the procedure. Thus, local variables are local to a specific invocation of a procedure.

Local variables are programming variables that are used only during execution of the procedure, and then discarded.

Local variables are temporary storage places within a procedure.

Global variables are available from inside any procedure in Maple as well as at the interactive level.

Parameters and local variables in Maple procedures are not subject to aliases.

Functional Operators[edit | edit source]

A functional operator in Maple is a special form of a procedure. Functional operators are written using the arrow operator ->:

>f := x -> 3*x + 5;
>f(2);
>g:=(u,v,w)->1/u+exp(u+v)+(u-v+w)^2;
>g(2*a,b,3*c);

Ie. functional operators are written using the notation

vars -> result

where vars is a name (the variable), or a sequence of names between brackets (for two or more variables), and result is an expression (the result of the function acting on the variable(s) vars).