11.07.2015 Views

Advanced Programming Guide

Advanced Programming Guide

Advanced Programming Guide

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

4 • Chapter 2: Procedures, Variables, and Extending Mapleenvironment, the map command is used to apply an operation to theelements of a structure. For example, you can divide each element of alist by a number, such as 8.> lst := [8, 4, 2, 16]:> map( x->x/8, lst);[1, 1 2 , 1 4 , 2]Consider a variation on the map command, which appears in the followingprocedure.Example This new procedure divides each element of a list by the firstelement of that list.> nest := proc(x::list)> local v;> v := x[1];> map( y -> y/v, x );> end proc:> nest(lst);[1, 1 2 , 1 4 , 2]The procedure nest contains a second procedure, map, which in thiscase is the Maple command map. Maple applies its lexical scoping rules,which declare the v within the call to map as the same v as in the outerprocedure, nest.Scoping RulesThis section explains Maple scoping rules. You will learn how Maple determineswhich variables are local to a procedure and which are global.You must have a basic understanding of Maple evaluation rules for parameters,and for local and global variables. For more information, seeChapter 6 of the Introductory <strong>Programming</strong> <strong>Guide</strong>.Local Versus Global VariablesIn general, when writing a procedure, you should explicitly declare whichvariables are global and which are local. Declaring the scope of the variablesmakes your procedure easier to read and debug. However, sometimesdeclaring the variables is not the best method. In the previous nest procedure,the variable in the map command is defined by the surrounding

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!