Introduction to Programming with Mathematica, 3rd Edition

First printing
Errata last updated: Day MonthName Year

1 Introduction

ButtonBox["Spikey",
   ButtonFunction:>CompoundExpression[
	Needs["Graphics`Polyhedra`"], 	
	Show[Symbol["Stellate"][Symbol["Polyhedron"][Symbol["Icosahedron"]]]]], 
ButtonEvaluator->Automatic, Active->True] //DisplayForm

Delete paragraph beginning “The formatted version of the…”.
Change paragraph starting “Clicking the button…” to: “Clicking the button will cause the Mathematica code in the ButtonFunction to be evaluated and the graphic of the icosahedron will then be displayed in your notebook.”

3 Lists

4 Functional programming

7 Recursion

merge[{a_, ra___}, {b_, rb___}] := 
If[
        a ≤ b, 
Join[{a}, 
          merge[{ra}, {b, rb}]], 
Join[{b}, 
          merge[{a, ra}, {rb}]]
]

All of the remaining inputs that use merge and MergeSort will now be correct.

9 Graphics programming

10 Front end programming

Here is the button code. Note that we have added an option to ButtonBox to set the background and also wrapped the ButtonBox text in StyleBox in order to add several font options there.

ButtonBox[StyleBox["Spikey",
      FontFamily->"Times",FontColor->White],
    ButtonFunction:>CompoundExpression[
        Needs["Graphics`Polyhedra`"],
        Show[Graphics3D[Symbol["Stellate"][Symbol["Icosahedron"][]]]]],
    ButtonEvaluator->Automatic,
    Background->GrayLevel[.5],
    Active->True] //DisplayForm

Clicking the button will produce the stellated icosahedron as displayed above.

Note that we have wrapped each of the symbols Stellate and Icosahedron in Symbol["…"].  The reason this is necessary is a bit technical but is roughly as follows: Prior to evaluating any input, the kernel first parses that input and determines which context any symbols in that input live. So prior to evaluation, the symbols Stellate and Icosahedron are parsed and put in the current context (typically Global`). Then, when the evaluation takes place and the package is loaded, shadowing of symbols occurs and a warning message will be issued. By using Symbol["Stellate"], the string argument prevents the parser from putting a symbol in the wrong context. Actually, this code would be more robust if we used the full context name, Symbol["Graphics`Polyhedra`Stellate"], as this would work even if shadowing had occurred elsewhere.

11 Examples and applications

12 Writing packages

References

Solutions to exercises

bisect[f_, {a_, b_, ϵ_}] := 
      Module[{midpt = N[((a + b)/2)], low = a, high = b}, 
        While[Abs[f[midpt]] > ϵ, 
If[
            Sign[f[low]] == Sign[f[midpt]], low = midpt, 
            high = midpt]; 
midpt = 
            N[((low + high)/2)]]; 
midpt]

f[x_] := Cos[x] - x

bisect[f, {0, 1, (10^(-12))}]

f[x_] := (x^2) - 2

bisect[f, {1, 2, (10^(-12))}]

f[x_] := 4 - (x^2)

bisect[f, {0, 4, (10^(-12))}]

Options[ShowTable] = {Headings -> {}};

ShowTable[data_, opts___?OptionQ]:=Module[{headstyle, headings},
headstyle[str_] := 
              StyleBox[(MakeBoxes[#, StandardForm] &)@str, 
                FontFamily -> "Helvetica", 
                FontWeight -> "Bold", FontColor -> Blue, 
                FontSize -> 10];
headings = Headings /. Flatten[{opts}] /. 
                  Options[ShowTable];
\(DisplayForm[
              StyleBox[
GridBox[
                  If[headings == {}, data, 
                    Prepend[data, 
                      Map[headstyle, 
                        headings]]], 
GridFrame -> 2, 
                  GridFrameMargins -> {{1, 1}, {1, 
                        1}}, 
RowLines -> 1, 
                  ColumnLines -> 
                    1], 
FontFamily -> "Times", 
Background -> GrayLevel[ .8], 
                SingleLetterItalics -> True]]\)]

Index