(***********************************************************************
Mathematica-Compatible Notebook
This notebook can be used on any computer system with Mathematica 4.0,
MathReader 4.0, or any compatible application. The data for the notebook
starts with the line containing stars above.
To get the notebook into a Mathematica-compatible application, do one of
the following:
* Save the data starting with the line of stars above into a file
with a name ending in .nb, then open the file inside the application;
* Copy the data starting with the line of stars above to the
clipboard, then use the Paste menu command inside the application.
Data for notebooks contains only printable 7-bit ASCII and can be
sent directly in email or through ftp in text mode. Newlines can be
CR, LF or CRLF (Unix, Macintosh or MS-DOS style).
NOTE: If you modify the data for this notebook not in a Mathematica-
compatible application, you must delete the line below containing the
word CacheID, otherwise Mathematica-compatible applications may try to
use invalid cache data.
For more information on notebooks and Mathematica-compatible
applications, contact Wolfram Research:
web: http://www.wolfram.com
email: info@wolfram.com
phone: +1-217-398-0700 (U.S.)
Notebook reader applications are available free of charge from
Wolfram Research.
***********************************************************************)
(*CacheID: 232*)
(*NotebookFileLineBreakTest
NotebookFileLineBreakTest*)
(*NotebookOptionsPosition[ 51373, 1835]*)
(*NotebookOutlinePosition[ 52462, 1870]*)
(* CellTagsIndexPosition[ 52418, 1866]*)
(*WindowFrame->Normal*)
Notebook[{
Cell[CellGroupData[{
Cell[TextData["9. Modules"], "Title",
Evaluatable->False,
AspectRatioFixed->True],
Cell["Last revision: February 9 1998", "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
"In this section we consider the drawbacks of using code spread out over \
several ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" cells and discuss the solution of using a ",
StyleBox["Module", "Input",
FontWeight->"Plain"],
" to put several lines of code together. We consider local and global \
variables and their scopes, side effects of ",
StyleBox["Module", "Input",
FontWeight->"Plain"],
"s, as well as indentation for readability and other aspects of good \
programming style."
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" operations/structures introduced: \n Module\n With[]\n ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["variable scope",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"]
}], "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Modules and Organization"], "Section",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Organizing Code"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
"Entering code into ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" line by line, as you will almost certainly have concluded by now, can be \
dangerous. Variables may have not been defined when you thought they were, \
or---perhaps worse---they have changed in value from what they should have \
been. The same is true for functions, and indeed for all global names in ",
StyleBox["Mathematica",
FontSlant->"Italic"],
". This danger is common to nearly every programming language and demands \
attention."
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Another (related) difficulty with ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" in its line by line usage is that the user must \"evaluate\" several \
cells to complete a task; there is no general \"organization\" to the \
different pieces of a programming task (except in the bracketing of the \
cells). While this difficulty is more common to interpreted languages like ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" than to compiled languages like C, Fortran, or Pascal, the solution to it \
(and to the danger above) makes ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" more similar to compiled languages. The solution is to use a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["To see the utility of a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
", let's examine several alternatives to performing a sequence of \
operations in ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" which declare some variables and functions and compute some intermediate \
values. The subject of our examinations will be an efficient method to find, \
given real numbers ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`y\_1\)]],
", ",
Cell[BoxData[
\(TraditionalForm\`y\_2\)]],
", to ",
Cell[BoxData[
\(TraditionalForm\`y\_n\)]],
StyleBox[", an expression for the polynomial of degree ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`n - 1\)]],
StyleBox[" which passes through the points ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((1, y\_1\)\)]],
StyleBox["), ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((2, y\_2\)\)]],
StyleBox["), and so forth, to ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((1, y\_n\)\)]],
StyleBox[
"). We will leave a proof of the result to a numerical analysis course. \
First we show how the interpolation works, and then we examine ways to \
improve the code using some techniques we know now, without using a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[CellGroupData[{
Cell[TextData["Interpolation"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Let's begin with six points, ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((1, y\_1)\)\)]],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((2, y\_2)\)\)]],
StyleBox[", up to ",
Evaluatable->False,
AspectRatioFixed->True],
Cell[BoxData[
\(TraditionalForm\`\((6, y\_6)\)\)]],
StyleBox[", where the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["y",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" values are 4, 8, \[Dash]3, 16, 7, and 0. We will start by placing ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" values and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["y",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" values in separate lists. We use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Range[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to generate the numbers 1 through 6.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["xs = Range[6]", "Input",
AspectRatioFixed->True],
Cell["ys = {4,8,-3,16,7,0}", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"To find the points through which we are interpolating, we can always use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Transpose[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[":",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["pts = Transpose[{xs,ys}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Now ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" has its own function to find the polynomial we are looking for, called ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["InterpolatingPolynomial[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
", for which the first argument is the list of points and the second \
argument is the name of the variable for the polynomial. In the next input \
cell, we ask ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" to find and to plot this polynomial in order to verify that it satisfies \
our conditions. ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"(For those interested, the following cell contains two interesting pieces \
of code: prolog instructions, which provide the large disks around the \
points corresponding to our list ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["pts",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", and a correct use of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Set[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", using the equals sign, instead of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["SetDelayed[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", using the colon-equals sign. We use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Set[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" because we require ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["InterpolatingPolynomial[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to be executed immediately and then assigned to ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["f",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["; we do not want the value of the argument of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["f",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" substituted for ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" in the expression first.)",
Evaluatable->False,
AspectRatioFixed->True]
}], "SmallText",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[f, x]
f[x_] = InterpolatingPolynomial[pts,x]
Plot[f[x],{x,1,6},Prolog->{PointSize[0.03],
RGBColor[1,0,0],Map[Point,pts]}]\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Now we could use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Expand[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" or ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Simplify[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" to simplify the polynomial, but the current form is of most interest: \
how did ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" find the numbers ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["4, 4, -15/2, 15/2, -103/24, ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["and",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox[" 191/120",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"? (Notice that the other numbers simply correspond to the integers 1 \
through 5.) Our goal in what follows is not to produce the polynomial, since \
we can have ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" do so already, but rather to explore a simple and efficient method, \
called divided difference quotients, which produces these mysterious numbers. \
",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["First we define a function ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["diff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" which takes a list and forms the successive differences of elements of a \
list. Note that we saw a similar function in section 7.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["diff[list_] := Drop[list,1] - Drop[list,-1]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["First we begin with ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ys",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[":",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["ys", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Then we apply ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["diff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" repeatedly, dividing by a successive integer each time, beginning with \
1.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["diff[%]/1", "Input",
AspectRatioFixed->True],
Cell["diff[%]/2", "Input",
AspectRatioFixed->True],
Cell["diff[%]/3", "Input",
AspectRatioFixed->True],
Cell["diff[%]/4", "Input",
AspectRatioFixed->True],
Cell["diff[%]/5", "Input",
AspectRatioFixed->True],
Cell[TextData["Notice the first element of each of the last 6 lists!"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"We can figure out the correct denominator at each stage by examining the \
length of the list. If we do so, we can define a function ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["divDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and use ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["NestList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to perform our operations for us.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
divDiff[list_]:= (Drop[list,1]-Drop[list,-1])/
(7-Length[list])\
\>", "Input",
AspectRatioFixed->True],
Cell["NestList[divDiff,ys,5]", "Input",
AspectRatioFixed->True],
Cell["Map[First,%]", "Input",
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Improving Code"], "Subsubsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Now we may begin to consider how we might improve this code. We begin then \
with the following lines:"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["ys = {4,8,-3,16,7,0}", "Input",
AspectRatioFixed->True],
Cell["\<\
divDiff[list_]:= (Drop[list,1]-Drop[list,-1]) / \
(7-Length[list])\
\>", "Input",
AspectRatioFixed->True],
Cell["NestList[divDiff,ys,5]", "Input",
AspectRatioFixed->True],
Cell["Map[First,%]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["First, it would be best to execute ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Clear[divDiff]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" in front so we don't run into problems later with other definitions of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["divDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[". (Note that variable names used as formal parameters---",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ys",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["list",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" in our case---do not present this danger and do not need to be separately \
cleared.) Also, we should place it all in one cell so that the user is \
prevented from executing only some instead of all of the commands. Let's do \
that.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[divDiff]
ys = {4,8,-3,16,7,0}
divDiff[list_]:= (Drop[list,1]-Drop[list,-1])/(7-Length[list])
NestList[divDiff,ys,5]
Map[First,%]\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[
"To an experienced eye, this code has several problems. First of all, it \
does not look very readable. An important programming principle is that code \
should be as readable as possible, since you may be bequeathing your code \
onto someone else. While this may not seem important in this class, imagine \
you're working for a bank or the national defense system. Life is too short \
to cause others too many difficulties with your work! To clean it up, we'll \
indent so that a command that runs onto another line is clearly indicated. \
Another problem is that we see too many intermediate results. We'd really \
just like the last ones, so we'll place semicolons after every expression \
save the last one."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[divDiff];
ys = {4,8,-3,16,7,0};
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(7-Length[list]);
NestList[divDiff,ys,5];
Map[First,%]\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
" What if we wanted to make this a function easily executed by the user \
with different values? Specifically, suppose we would like to have the \
process work on a list of 6 elements of our own choosing? We could try the \
following, changing the variable ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["ys",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to a parameter and adding a function ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["doDivDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[". Here our indentation makes more of a difference:",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[divDiff,doDivDiff];
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(7-Length[list]);
doDivDiff[ys_] :=
Map[
First,
NestList[
divDiff,
ys,
5
]
];
\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Notice how we vertically line up arguments of the same function, and we \
align the closing bracket \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" with the beginning of the associated function. Even with the \
indentation, however, this code is hard to understand, since we have so many \
functions inside others in the definition of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["doDivDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[". We might do better to assign a the result of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["NestList[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to a temporary variable, and then use it in the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Map[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" command. We can do so by using \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["(",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["command",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["; ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["command",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["; ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["command",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[")",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"\" syntax, where we are allowed to have several commands completed as part \
of one expression:",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[divDiff,doDivDiff,temp];
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(7-Length[list]);
doDivDiff[ys_] :=
(temp = NestList[
divDiff,
ys,
5
];
Map[First,temp]
);
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["This all looks fine, except that ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["divDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["temp",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" are now defined for later, which is unnecessary---and perhaps \
dangerous---if the user never needs their values and might want to use the \
name ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["temp",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" again. We'd like to define the two names just in order to define ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["doDivDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and have them otherwise invisible. We might try",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[ys,divDiff,doDivDiff,temp];
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(7-Length[list]);
doDivDiff[ys_] :=
(temp = NestList[
divDiff,
ys,
5
];
Map[First,temp]
);
Clear[temp,divDiff];
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["But now ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox["'s forgotten what ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["divDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" means altogether. Frustrated? Do you think the code is looking too \
complicated for what little we want to do? Good. The solution to these \
sometimes competing concerns of readability, limited definition of names, and \
encapsulation into one cell or structure is a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Modules"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["A ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" allows the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[
" programmer to code a sequence of operations together with some local \
names (variables, functions, and so on). These local names will not be \
usable by any code outside the module, so the user is safe. Also, a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" allows the sequence of operations to be encapsulated simply in a \
programming unit, so that we do not need to rely on a cell.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["The syntax of a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" is the following: ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module[{a,b,...}, ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["instruction",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["; ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["instruction",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox["; ",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["etc",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier",
FontSlant->"Italic"],
StyleBox[".]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". The result of the last instruction will be the result of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". (Note that if we place a semicolon after the last instruction, then ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" will insert another instruction, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Null",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" at the end, and this will be the result of our ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["!) The names we place in the list ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["{a,b,...}",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" (the first argument) will be the local variables; every time we use them \
in the instructions, the names will refer to the local variables, even if \
there are some global variables with the same name. ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData["Let's look at our interpolator in Module form:"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[doDivDiff];
doDivDiff[ys_] := Module[{temp,divDiff},
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(7-Length[list]);
temp = NestList[
divDiff,
ys,
5
];
Map[First,temp]
];
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"This code is much cleaner; the indentation works well and does not \
distract; the function ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["divDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" is only locally defined; we are permitted to name our intermediate value \
",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["temp",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" instead of nesting the functions all in huge command as before. In \
short, we get everything we want. ",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell["Caveats, Side Effects, and \"Local Memory\"", "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["Added functionality always comes at a price. What price ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"s? First, the programmer may inadvertently leave a name meant to be local \
out of the list in the first argument, or might place a name meant to be \
global---meant to be used both inside and outside of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"---in the list. For this reason, it is best to place a comment at the \
beginning of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" explicitly declaring which global variables the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" may use or change. A comment is text that ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" ignores while interpreting your code, and the syntax is ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["(* comment *)",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". When we explicitly state what global variables might change, we are \
admitting what the possible \"side effects\" of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" are.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"As an example, suppose we want to keep track of names in a sports roster. \
We want the roster itself, as well as a variable saying how many people are \
in the roster, to be global, and we want to define ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"s which add or delete names from the list and insure that there are no \
duplicates. Here's a possible solution:",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[sportsNames,totalNames,addName,deleteName]
sportsNames = {};
totalNames = 0;
addName[s_] :=
Module[{},
\t (* uses sportsNames, totalNames *)
\t sportsNames = Union[sportsNames,{s}];
\t totalNames = Length[sportsNames];
\t ]
deleteName[s_] :=
\t Module[{},
\t \t (* uses sportsNames, totalNames *)
\t \t sportsNames = Complement[sportsNames,{s}];
\t \t totalNames = Length[sportsNames];
\t ]
\t \
\>", "Input",
AspectRatioFixed->True],
Cell["\<\
addName[\"Magic\"]
addName[\"Shaq\"]
totalNames\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Our solution is both efficient, easy to read, and it admits up front what \
global variables are changed when ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["addName[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["deleteName[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" are used. The two functions have a very definite, desirable side effect, \
namely, updating ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["sportsNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["A second caveat is that ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
"s do not remember the values of the local variables inside them when they \
are executed again. For instance, we could not make ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" local to either ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", even if we were only using ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["addName",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[":",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[sportsNames,totalNames,addName]
sportsNames = {};
addName[s_] :=
Module[{totalNames},
\t (* uses sportsNames *)
\t sportsNames = Union[sportsNames,{s}];
\t totalNames = totalNames + 1;
\t Print[totalNames]
\t ]
\t \
\>", "Input",
AspectRatioFixed->True],
Cell["\<\
addName[\"Magic\"]
addName[\"Shaq\"]\
\>", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["The problem here is that ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["totalNames",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" was not defined when the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" was first executed, so incrementing it by one caused ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" to spill some red ink in confusion.",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData[
"Initialization of Local Variables, Constants, and Nesting"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Let's return to our interpolator. What if we want to change the fact that \
there are 6 y-values? Perhaps we'd like to change it to 7 or 8. We notice \
that the \"5\" inside the function needs to be replaced by one less than the \
number of elements in the initial list, and the \"7\" by one greater. While \
we can certainly search for the 5 and 7 inside the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" and change them by hand, the next programmer will more easily understand \
the function if we show that each depends on one fixed number---the number of \
elements in the list. Note that the situation would be much worse if our ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" contained several 5's and 7's, some of which did pertain to the length \
and some did not. Which 5's and 7's would the programmer look to change? \
The best thing to do is replace each of these integers with an expression \
involving the fixed number, which itself should have a name (",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["len", "Input",
Evaluatable->False,
AspectRatioFixed->True,
FontWeight->"Plain"],
StyleBox[
", for instance), and initialize the name explicitly at the beginning of \
the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[". We can perform the initialization at the beginning of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" by doing so inside the first list:",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[doDivDiff];
doDivDiff[ys_] := Module[
\t {len=7 (* length of list *),
\t temp,divDiff},
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(len+1-Length[list]);
temp = NestList[
divDiff,
ys,
len-1
];
Map[First,temp]
];
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3,2}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox["There is a programming term for ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["len",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
", which is constant. A constant is a variable initialized once and never \
changed throughout a programming unit. It is useful for a reader to know \
that there is no possibility of the constant's changing, so that he or she \
does not have to be on the lookout for redefinitions of that variable. ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Mathematica",
Evaluatable->False,
AspectRatioFixed->True,
FontSlant->"Italic"],
StyleBox[" has a particular construct for such a variable, the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["With[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" construct. ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["With[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" works just like ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
", except that the variables in the first list must be initialized and may \
never change inside. A legitimate question at this point is whether ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["With",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" or ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" is appropriate for ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["doDivDiff[]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
", since we have both local variables and constants. The answer is both; \
there is no reason not to nest ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["With",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" constructs and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" constructs, which is to say that you may have one inside the other. \
Here's the nice solution:",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[doDivDiff];
doDivDiff[ys_] := With[{len=7 (* length of list *)},
Module[{temp,divDiff},
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(len+1-Length[list]);
temp = NestList[
divDiff,
ys,
len-1
];
Map[First,temp]
]];
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3,2}]", "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Finally, what if we wanted the function to take a list of any length and \
determine itself what the length was? The nicest solution, now that we see \
we only need to change ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["len",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", is to initialize the value of ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["len",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" to ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Length[ys]",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[".",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell["\<\
Clear[doDivDiff];
doDivDiff[ys_] := With[{len=Length[ys]},
\t Module[{temp,divDiff},
divDiff[list_] := (Drop[list,1]-Drop[list,-1]) /
(len+1-Length[list]);
temp = NestList[
divDiff,
ys,
len-1
];
Map[First,temp]
]];
\
\>", "Input",
AspectRatioFixed->True],
Cell["doDivDiff[{3,10,20,-5,2,3}]", "Input",
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Scope"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"The foregoing discussion raises an issue: when a name (variable, \
function, or what have you) is defined, for what section of the code does it \
have meaning? We've seen that names defined outside of any ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" are global, meaning that they can be used freely within or outside of any \
function, while names mentioned in the first argument of a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" are local to the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and lose their meaning outside of the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". When we want to be specific about the section of code a name has \
meaning in, we use the word scope; in the context of computer languages, \
scope refers to the extent of code in which a certain name is defined. Here \
is an example.\n",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"x = 2; z = 3; \nmyModule := \n Module[{x,y}, \n x = z; \
\n y = 3; \n ]; \ny = 2; "], "Input",
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"We must be careful counting the variables here. We see three global \
variables, ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["y",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["z",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", and two variables ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["y",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" which are local to the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". The scope of the global variables is then the entire section of code, \
while the scope of the local variables is the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" itself. Notice that if a local variable in a ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" has the same name as a global variable, it may be \"hidden\" in the sense \
that inside the ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Module",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
" we cannot refer to the global variable. In our example, any occurrence \
of x inside the module must refer to the local ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", not the global ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[
". Try to decide by hand what the result of the cell is: what would the \
values of the global variables ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["x",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["y",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[", and ",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["z",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox[" be? (Answer: 2, 2, 3.)",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]],
Cell[CellGroupData[{
Cell[TextData["Programming Style"], "Subsection",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"In this section we have touched on several aspects of basic programming \
style, which we repeat here. Programmers are expected to use good style when \
writing programs, just as they are expected to do when writing papers. \
Assume that the audience for a program is always a fellow programmer who \
wants to read the code quickly and not encounter any difficulties. Part of \
your evaluation in each assignment will depend on how well you achieve good \
style, even if your code is syntactically and logically correct. In \
particular:"], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Indent as you nest functions or modules inside each other, keeping \
successive arguments lined up vertically and closing brackets lined up with \
the beginning of the function."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Declare to the reader the side effects of your function by using a \
comment."], "Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[
"Name variables and functions with descriptive names, and capitalize all but \
the first word in the name if there are several words joined together."],
"Text",
Evaluatable->False,
AspectRatioFixed->True],
Cell[TextData[{
StyleBox[
"Unless specifically requested otherwise, name intermediate expressions and \
use the names in successive lines, thus breaking up deeply nested functions \
such as \"",
Evaluatable->False,
AspectRatioFixed->True],
StyleBox["Apply[Map[Table[...",
Evaluatable->False,
AspectRatioFixed->True,
FontFamily->"Courier"],
StyleBox["\".\n\n",
Evaluatable->False,
AspectRatioFixed->True]
}], "Text",
Evaluatable->False,
AspectRatioFixed->True]
}, Closed]]
}, Open ]]
}, Open ]]
},
FrontEndVersion->"4.0 for Macintosh",
ScreenRectangle->{{0, 1152}, {0, 850}},
WindowToolbars->{},
WindowSize->{518, 431},
WindowMargins->{{71, Automatic}, {Automatic, 3}},
PrintingCopies->1,
PrintingPageRange->{1, Automatic},
PrivateNotebookOptions->{"ColorPalette"->{RGBColor, -1}},
ShowCellLabel->True,
ShowCellTags->False,
RenderingOptions->{"ObjectDithering"->True,
"RasterDithering"->False},
MacintoshSystemPageSetup->"\<\
00<0004/0B`000003809T?o>old"
]
(***********************************************************************
Cached data follows. If you edit this Notebook file directly, not using
Mathematica, you must remove the line containing CacheID at the top of
the file. The cache data will then be recreated when you save this file
from within Mathematica.
***********************************************************************)
(*CellTagsOutline
CellTagsIndex->{}
*)
(*CellTagsIndex
CellTagsIndex->{}
*)
(*NotebookFileOutline
Notebook[{
Cell[CellGroupData[{
Cell[1739, 51, 85, 2, 98, "Title",
Evaluatable->False],
Cell[1827, 55, 99, 2, 26, "SmallText",
Evaluatable->False],
Cell[1929, 59, 591, 16, 80, "Text",
Evaluatable->False],
Cell[2523, 77, 429, 15, 68, "SmallText",
Evaluatable->False],
Cell[CellGroupData[{
Cell[2977, 96, 101, 2, 50, "Section",
Evaluatable->False],
Cell[CellGroupData[{
Cell[3103, 102, 95, 2, 46, "Subsection",
Evaluatable->False],
Cell[3201, 106, 607, 14, 98, "Text",
Evaluatable->False],
Cell[3811, 122, 1339, 41, 116, "Text",
Evaluatable->False],
Cell[5153, 165, 2000, 66, 128, "Text",
Evaluatable->False],
Cell[CellGroupData[{
Cell[7178, 235, 96, 2, 42, "Subsubsection",
Evaluatable->False],
Cell[7277, 239, 1387, 50, 66, "Text",
Evaluatable->False],
Cell[8667, 291, 56, 1, 27, "Input"],
Cell[8726, 294, 63, 1, 27, "Input"],
Cell[8792, 297, 398, 15, 30, "Text",
Evaluatable->False],
Cell[9193, 314, 67, 1, 27, "Input"],
Cell[9263, 317, 1013, 33, 100, "Text",
Evaluatable->False],
Cell[10279, 352, 1946, 65, 82, "SmallText",
Evaluatable->False],
Cell[12228, 419, 181, 6, 72, "Input"],
Cell[12412, 427, 1642, 56, 114, "Text",
Evaluatable->False],
Cell[14057, 485, 473, 15, 46, "Text",
Evaluatable->False],
Cell[14533, 502, 86, 1, 27, "Input"],
Cell[14622, 505, 328, 13, 30, "Text",
Evaluatable->False],
Cell[14953, 520, 45, 1, 27, "Input"],
Cell[15001, 523, 404, 15, 46, "Text",
Evaluatable->False],
Cell[15408, 540, 52, 1, 27, "Input"],
Cell[15463, 543, 52, 1, 27, "Input"],
Cell[15518, 546, 52, 1, 27, "Input"],
Cell[15573, 549, 52, 1, 27, "Input"],
Cell[15628, 552, 52, 1, 27, "Input"],
Cell[15683, 555, 127, 2, 30, "Text",
Evaluatable->False],
Cell[15813, 559, 667, 22, 62, "Text",
Evaluatable->False],
Cell[16483, 583, 116, 4, 42, "Input"],
Cell[16602, 589, 65, 1, 27, "Input"],
Cell[16670, 592, 55, 1, 27, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[16762, 598, 97, 2, 42, "Subsubsection",
Evaluatable->False],
Cell[16862, 602, 178, 4, 46, "Text",
Evaluatable->False],
Cell[17043, 608, 63, 1, 27, "Input"],
Cell[17109, 611, 117, 4, 42, "Input"],
Cell[17229, 617, 65, 1, 27, "Input"],
Cell[17297, 620, 55, 1, 27, "Input"],
Cell[17355, 623, 1233, 39, 94, "Text",
Evaluatable->False],
Cell[18591, 664, 185, 7, 102, "Input"],
Cell[18779, 673, 791, 12, 142, "Text",
Evaluatable->False],
Cell[19573, 687, 196, 8, 102, "Input"],
Cell[19772, 697, 831, 24, 78, "Text",
Evaluatable->False],
Cell[20606, 723, 260, 15, 207, "Input"],
Cell[20869, 740, 2185, 75, 127, "Text",
Evaluatable->False],
Cell[23057, 817, 290, 14, 192, "Input"],
Cell[23350, 833, 70, 1, 27, "Input"],
Cell[23423, 836, 1129, 38, 78, "Text",
Evaluatable->False],
Cell[24555, 876, 314, 15, 207, "Input"],
Cell[24872, 893, 70, 1, 27, "Input"],
Cell[24945, 896, 958, 31, 80, "Text",
Evaluatable->False]
}, Closed]]
}, Closed]],
Cell[CellGroupData[{
Cell[25952, 933, 87, 2, 30, "Subsection",
Evaluatable->False],
Cell[26042, 937, 1015, 32, 96, "Text",
Evaluatable->False],
Cell[27060, 971, 2519, 87, 130, "Text",
Evaluatable->False],
Cell[29582, 1060, 120, 2, 30, "Text",
Evaluatable->False],
Cell[29705, 1064, 303, 14, 192, "Input"],
Cell[30011, 1080, 70, 1, 27, "Input"],
Cell[30084, 1083, 765, 26, 78, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[30886, 1114, 113, 2, 30, "Subsection",
Evaluatable->False],
Cell[31002, 1118, 1981, 62, 144, "Text",
Evaluatable->False],
Cell[32986, 1182, 636, 18, 78, "Text",
Evaluatable->False],
Cell[33625, 1202, 477, 18, 252, "Input"],
Cell[34105, 1222, 98, 5, 57, "Input"],
Cell[34206, 1229, 1057, 38, 62, "Text",
Evaluatable->False],
Cell[35266, 1269, 1026, 36, 62, "Text",
Evaluatable->False],
Cell[36295, 1307, 293, 12, 162, "Input"],
Cell[36591, 1321, 87, 4, 42, "Input"],
Cell[36681, 1327, 797, 27, 48, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[37515, 1359, 138, 3, 30, "Subsection",
Evaluatable->False],
Cell[37656, 1364, 2059, 55, 206, "Text",
Evaluatable->False],
Cell[39718, 1421, 350, 16, 222, "Input"],
Cell[40071, 1439, 72, 1, 27, "Input"],
Cell[40146, 1442, 2649, 86, 176, "Text",
Evaluatable->False],
Cell[42798, 1530, 349, 15, 207, "Input"],
Cell[43150, 1547, 72, 1, 27, "Input"],
Cell[43225, 1550, 863, 30, 62, "Text",
Evaluatable->False],
Cell[44091, 1582, 338, 15, 207, "Input"],
Cell[44432, 1599, 70, 1, 27, "Input"]
}, Closed]],
Cell[CellGroupData[{
Cell[44539, 1605, 85, 2, 30, "Subsection",
Evaluatable->False],
Cell[44627, 1609, 1468, 43, 158, "Text",
Evaluatable->False],
Cell[46098, 1654, 187, 3, 117, "Input"],
Cell[46288, 1659, 3150, 114, 142, "Text",
Evaluatable->False]
}, Closed]],
Cell[CellGroupData[{
Cell[49475, 1778, 97, 2, 30, "Subsection",
Evaluatable->False],
Cell[49575, 1782, 617, 10, 110, "Text",
Evaluatable->False],
Cell[50195, 1794, 252, 5, 46, "Text",
Evaluatable->False],
Cell[50450, 1801, 152, 4, 30, "Text",
Evaluatable->False],
Cell[50605, 1807, 226, 5, 46, "Text",
Evaluatable->False],
Cell[50834, 1814, 499, 16, 94, "Text",
Evaluatable->False]
}, Closed]]
}, Open ]]
}, Open ]]
}
]
*)
(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)