(***********************************************************************
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[ 7506, 262]*)
(*NotebookOutlinePosition[ 8379, 292]*)
(* CellTagsIndexPosition[ 8335, 288]*)
(*WindowFrame->Normal*)
Notebook[{
Cell[CellGroupData[{
Cell[TextData[{
StyleBox["Safe and Effective Mathematica",
FontSlant->"Italic"],
" Programming"
}], "Title"],
Cell["", "Subtitle"],
Cell["by Peter Fritzson", "Author"],
Cell["Version 0.1, 2000-04-19", "Commentary"],
Cell[CellGroupData[{
Cell["Safe Programming with Mathematica", "Section"],
Cell[CellGroupData[{
Cell[TextData[{
"Common Bugs in ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" Programming"
}], "Subsection"],
Cell[TextData[{
"The following list presents common problems in programming in ",
StyleBox["Mathematica",
FontSlant->"Italic"],
", which are avoided by using the functions ",
StyleBox["BeginFunction",
FontWeight->"Bold"],
" and ",
StyleBox["EndFunction",
FontWeight->"Bold"],
" provided by the ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" ",
"package ",
"\"MathCore`SafeMathProg`\"",
"."
}], "Text"],
Cell[TextData[{
"1. Automatically clear obsolete rules in rule based programming. This is \
quite useful since otherwise after updating a rule in a function definition, \
it often happens that the old rule is still left in the ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" workspace."
}], "Text"],
Cell["\<\
2. Avoid returning huge unevaluated expressions, when there is an argument \
mismatch in function calls. Instead catch the error and print out in which \
function the error occurs.\
\>", "Text"],
Cell["\<\
3. Avoids contamination of package context by local symbols during \
interactive function definition. A local Private environment just within the \
cell containing the function rules is created to avoid such contamination.\
\>", "Text"],
Cell["\<\
4. When there happens to be both a Private and a non-Private version of a \
symbol-usually causing strange bugs, this is detected. The 4:th bug occurs if \
you forget both to mention the function name as an exported symbol and to put \
the package context mark on the symbol at the call to BeginFunction.\
\>", "Text"],
Cell[TextData[{
"5. A fifth bug-unintended function rule ordering-can be avoided by \
enclosing the rule definitions within ",
StyleBox["FunctionOrderedRules[...]",
FontWeight->"Bold"],
",which makes sure that rules are placed exactly in the specified order. \
Sometimes Mathematica change the order for which function rule are tested \
compared to the order of their definitions. "
}], "Text"],
Cell[TextData[{
"6. Accidently misspelled variable or function names, are detected fairly \
reliably by ",
StyleBox["EndFunction",
FontWeight->"Bold"],
". Any name which is not declared as a function, local variable, global \
variable, is assumed to be a undefined variable."
}], "Text"],
Cell["\<\
7. Forgetting to put an underscore after a function parameter name, e.g., \
param instead of param_, is also caught by EndFunction. In the case when you \
want to have a function argument patterns the matches certain named symbol, \
this symbol must be available outside the function in order to be passed in \
at a function call. Therefore such a symbol should be defined either as a \
public or as a private package symbol. \
\>", "Text"]
}, Open ]],
Cell[CellGroupData[{
Cell["Simple Examples of using SafeMathProg", "Subsection"],
Cell[TextData[{
"In case you just want to use the SafeMathProg package for some prototyping \
without creating your own named package as explained in the next section, you \
can just execute a Needs statement to load SafeMathProg. This will adopt the \
currently active package context for your function, usually the default \
context Global. However, for any software development in ",
StyleBox["Mathematica",
FontSlant->"Italic"],
" of more than a page or two, it is strongly recommended to use the package \
structure explained in the next section."
}], "Text"],
Cell["Needs[\"MathCore`SafeMathProg`\"];", "Input"],
Cell["\<\
A simple example of a function definition cell using BeginFunction and \
EndFunction from the SafeMathProg package for safer programming practice, is \
for the gcd function below:\
\>", "Text"],
Cell["\<\
BeginFunction[gcd];
gcd::usage = \"gcd[x,y] computes the greatest common divisor of x and y\";
gcd[u_,0] := u;
gcd[u_,v_] := gcd[v, Mod[u,v]];
EndFunction[];\
\>", "Input"],
Cell["\<\
Let us evaluate the gcd function definition cell defined above, and then \
call gcd:\
\>", "Text"],
Cell[CellGroupData[{
Cell["gcd[4,20]", "Input"],
Cell[BoxData[
\(4\)], "Output"]
}, Open ]],
Cell["Try a call with the wrong number of parameters:", "Text"],
Cell[CellGroupData[{
Cell["gcd[4,20,3]", "Input"],
Cell[BoxData[
\(gcd::"wrongargs" \(\(:\)\(\ \)\)
"\!\(gcd\) called with \!\(3\) arguments. Wrong number or nonmatching \
arguments: [\!\(\"4, 20, 3\"\)]"\)], "Message"]
}, Open ]],
Cell["\<\
Define another function, gcd2, where we misspelled the local parameter u to \
uu in one place:\
\>", "Text"],
Cell[CellGroupData[{
Cell["\<\
BeginFunction[gcd2];
gcd2[u_,0] := u;
gcd2[u_,v_] := gcd2[v, Mod[uu,v]];
EndFunction[];\
\>", "Input"],
Cell[BoxData[
\(gcd2::"errornames" \(\(:\)\(\ \)\)
"Undeclared or misspelled names: \!\(\"{uu}\"\) occurred within \
function \!\(\"Global`gcd2\"\)"\)], "Message"]
}, Open ]],
Cell["\<\
Define yet another function, gcd3, where we forgot the underscore of the \
formal parameter v in the second rule:\
\>", "Text"],
Cell[CellGroupData[{
Cell["\<\
BeginFunction[gcd3];
gcd3[u_,0] := u;
gcd3[u_,v] := gcd3[v, Mod[u,v]];
EndFunction[];\
\>", "Input"],
Cell[BoxData[
\(gcd3::"errornames" \(\(:\)\(\ \)\)
"Undeclared or misspelled names: \!\(\"{v}\"\) occurred within function \
\!\(\"Global`gcd3\"\)"\)], "Message"]
}, Open ]]
}, Open ]]
}, Open ]]
}, Open ]]
},
FrontEndVersion->"4.0 for Microsoft Windows",
ScreenRectangle->{{0, 1024}, {0, 719}},
AutoGeneratedPackage->None,
ScreenStyleEnvironment->"Presentation",
WindowToolbars->"EditBar",
WindowSize->{834, 671},
WindowMargins->{{20, Automatic}, {Automatic, 7}},
PrintingCopies->1,
PrintingPageRange->{Automatic, Automatic},
Magnification->1.25,
StyleDefinitions -> "MathCoreReport.nb"
]
(***********************************************************************
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, 116, 4, 138, "Title"],
Cell[1858, 57, 20, 0, 51, "Subtitle"],
Cell[1881, 59, 35, 0, 90, "Author"],
Cell[1919, 61, 45, 0, 33, "Commentary"],
Cell[CellGroupData[{
Cell[1989, 65, 52, 0, 72, "Section"],
Cell[CellGroupData[{
Cell[2066, 69, 123, 5, 49, "Subsection"],
Cell[2192, 76, 445, 17, 93, "Text"],
Cell[2640, 95, 311, 7, 93, "Text"],
Cell[2954, 104, 204, 4, 67, "Text"],
Cell[3161, 110, 246, 4, 93, "Text"],
Cell[3410, 116, 328, 5, 93, "Text"],
Cell[3741, 123, 404, 8, 119, "Text"],
Cell[4148, 133, 297, 7, 93, "Text"],
Cell[4448, 142, 450, 7, 145, "Text"]
}, Open ]],
Cell[CellGroupData[{
Cell[4935, 154, 59, 0, 49, "Subsection"],
Cell[4997, 156, 574, 10, 145, "Text"],
Cell[5574, 168, 51, 0, 52, "Input"],
Cell[5628, 170, 203, 4, 67, "Text"],
Cell[5834, 176, 188, 10, 204, "Input"],
Cell[6025, 188, 109, 3, 41, "Text"],
Cell[CellGroupData[{
Cell[6159, 195, 26, 0, 52, "Input"],
Cell[6188, 197, 35, 1, 53, "Output"]
}, Open ]],
Cell[6238, 201, 63, 0, 41, "Text"],
Cell[CellGroupData[{
Cell[6326, 205, 28, 0, 52, "Input"],
Cell[6357, 207, 179, 3, 61, "Message"]
}, Open ]],
Cell[6551, 213, 118, 3, 41, "Text"],
Cell[CellGroupData[{
Cell[6694, 220, 116, 8, 166, "Input"],
Cell[6813, 230, 174, 3, 61, "Message"]
}, Open ]],
Cell[7002, 236, 137, 3, 67, "Text"],
Cell[CellGroupData[{
Cell[7164, 243, 114, 8, 166, "Input"],
Cell[7281, 253, 173, 3, 61, "Message"]
}, Open ]]
}, Open ]]
}, Open ]]
}, Open ]]
}
]
*)
(***********************************************************************
End of Mathematica Notebook file.
***********************************************************************)