(*********************************************************************** 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[ 9121, 305]*) (*NotebookOutlinePosition[ 10211, 340]*) (* CellTagsIndexPosition[ 10167, 336]*) (*WindowFrame->Normal*) Notebook[{ Cell[CellGroupData[{ Cell[TextData["9. Modules: Problems"], "Title", Evaluatable->False, AspectRatioFixed->True], Cell["Last revision: February 9 1998", "SmallText", Evaluatable->False, AspectRatioFixed->True], Cell[CellGroupData[{ Cell[TextData["Author: YourNameGoesHere"], "Subtitle", CellFrame->True, Evaluatable->False, AspectRatioFixed->True, Background->GrayLevel[0.899992]], Cell[CellGroupData[{ Cell[TextData["1: plotWalk"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Create a functioning program Module ", Evaluatable->False, AspectRatioFixed->True], StyleBox["plotWalk[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " that contains all of the following steps with a formal parameter ", Evaluatable->False, AspectRatioFixed->True], StyleBox["m ", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ "instead of the number 50. Make names local whenever possible, and \ consider using local constants where appropriate.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["dirList = {{1,0},{0,1},{-1,0},{0,-1}}"], "Input", AspectRatioFixed->True], Cell[TextData["ranDirec := Part[dirList, Random[Integer,{1,4}]]"], "Input", AspectRatioFixed->True], Cell[TextData["\nranSteps[n_] := Table[ranDirec,{n}]"], "Input", AspectRatioFixed->True], Cell[TextData["origin = {0,0}"], "Input", AspectRatioFixed->True], Cell[TextData["stops[n_] := FoldList[Plus,origin,ranSteps[n]]"], "Input", AspectRatioFixed->True], Cell[TextData[ "ListPlot[stops[50], PlotJoined->True, PlotRange->All]"], "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["2: base16String2Num"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Create a functioning program ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Module", "Input", Evaluatable->False, AspectRatioFixed->True, FontWeight->"Plain"], StyleBox[" ", Evaluatable->False, AspectRatioFixed->True], StyleBox["base16String2Num[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " that contains all of the following steps with a formal parameter ", Evaluatable->False, AspectRatioFixed->True], StyleBox["s", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" instead of the string ", Evaluatable->False, AspectRatioFixed->True], StyleBox["\"12aB\"", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ ". Make names local whenever possible, and consider using local constants \ where appropriate.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["string1 = ToUpperCase[\"12aB\"]"], "Input", AspectRatioFixed->True], Cell[TextData["list1 = ToCharacterCode[string1] - 48"], "Input", AspectRatioFixed->True], Cell[TextData[{ "MakeABCDEFInto101112131415[i_] := \n\tIf[i>=17, i-7, i]\n", StyleBox[ "(* Because we want A's to be 10's and the\ncharacter code of A is 65, we \ must subtract 7\nmore from the code of each A, B, C, D, E, and F.\nThis \ function will allow us to do so *)", FontColor->RGBColor[0, 0, 1]] }], "Input", AspectRatioFixed->True], Cell[TextData["list2 = Map[MakeABCDEFInto101112131415,list1]"], "Input", AspectRatioFixed->True], Cell[TextData[ "list3 = Reverse[\n Table[\n 16^j,\n \ {j,0,StringLength[\"12aB\"]-1}\n ]\n ]"], "Input", AspectRatioFixed->True], Cell[TextData["list4 = list2 * list3"], "Input", AspectRatioFixed->True], Cell[TextData["Apply[Plus,list4]"], "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["3: variable & function scope; side-effects"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox[ "In the following code, determine how many different names (variables and \ functions) there are, the scope of each, and the side effects of any \ functions. Also, write sentences explaining exactly what happens in each \ function. (You may need to use the \"?\" command to determine how ", Evaluatable->False, AspectRatioFixed->True], StyleBox["If[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ " works.) Note that on your midterm you may have a problem, similar to \ this one, on which you will not be able to use a computer to answer the \ question; you should hone your programming reading skills as well as writing \ skills. You may also wish to assess the style of this code.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True], Cell[TextData["pet = \"dog\" "], "Input", AspectRatioFixed->True], Cell[TextData["pet2 = \"cat\""], "Input", AspectRatioFixed->True], Cell[TextData["procreate[animal_] := {animal,animal}"], "Input", AspectRatioFixed->True], Cell[TextData[ "runFarmA[purchase_] :=\n Module[{pet, procreate}, \n pet = \"pig\";\n \ procreate[animal_] := {animal,animal,animal};\n \ {procreate[\"sheep\"], procreate[pet], purchase}\n ]"], "Input", AspectRatioFixed->True], Cell[TextData[ "runFarmB[purchase_] :=\n Module[{pet, procreate},\n pet = \"pig\";\n \ pet2 = \"declawed cat\";\n procreate[animal_] := \ {animal,animal,animal};\n {procreate[\"sheep\"], procreate[pet], \ purchase}\n ]"], "Input", AspectRatioFixed->True], Cell[TextData[ "declaw[anAnimal_] := \n If[anAnimal==\"cat\", \"declawed cat\", \ anAnimal]"], "Input", AspectRatioFixed->True] }, Closed]], Cell[CellGroupData[{ Cell[TextData["4 Extra Credit: myInterpolate"], "Subsection", Evaluatable->False, AspectRatioFixed->True], Cell[TextData[{ StyleBox["Complete the definition of ", Evaluatable->False, AspectRatioFixed->True], StyleBox["myInterpolate[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[ ", a function which uses the divided difference quotients algorithm from \ the section to return the desired polynomial back to the user. ", Evaluatable->False, AspectRatioFixed->True], StyleBox["myInterpolate[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" should return the same polynomials, then, as the ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Mathematica", Evaluatable->False, AspectRatioFixed->True, FontSlant->"Italic"], StyleBox[" function ", Evaluatable->False, AspectRatioFixed->True], StyleBox["InterpolatingPolynomial[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[". Use a ", Evaluatable->False, AspectRatioFixed->True], StyleBox["Fold[]", Evaluatable->False, AspectRatioFixed->True, FontFamily->"Courier"], StyleBox[" command to produce the polynomial.", Evaluatable->False, AspectRatioFixed->True] }], "Text", Evaluatable->False, AspectRatioFixed->True] }, Closed]] }, Open ]] }, Open ]] }, FrontEndVersion->"4.0 for Macintosh", ScreenRectangle->{{0, 1152}, {0, 850}}, WindowToolbars->{}, WindowSize->{520, 509}, WindowMargins->{{12, Automatic}, {Automatic, 16}}, 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, 96, 2, 98, "Title", Evaluatable->False], Cell[1838, 55, 100, 2, 26, "SmallText", Evaluatable->False], Cell[CellGroupData[{ Cell[1963, 61, 156, 4, 77, "Subtitle", Evaluatable->False], Cell[CellGroupData[{ Cell[2144, 69, 91, 2, 46, "Subsection", Evaluatable->False], Cell[2238, 73, 706, 23, 62, "Text", Evaluatable->False], Cell[2947, 98, 90, 1, 27, "Input"], Cell[3040, 101, 101, 1, 27, "Input"], Cell[3144, 104, 90, 1, 42, "Input"], Cell[3237, 107, 67, 1, 27, "Input"], Cell[3307, 110, 99, 1, 27, "Input"], Cell[3409, 113, 107, 2, 42, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[3553, 120, 99, 2, 30, "Subsection", Evaluatable->False], Cell[3655, 124, 1053, 37, 62, "Text", Evaluatable->False], Cell[4711, 163, 84, 1, 27, "Input"], Cell[4798, 166, 90, 1, 27, "Input"], Cell[4891, 169, 352, 8, 102, "Input"], Cell[5246, 179, 98, 1, 27, "Input"], Cell[5347, 182, 183, 3, 102, "Input"], Cell[5533, 187, 74, 1, 27, "Input"], Cell[5610, 190, 70, 1, 27, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[5717, 196, 122, 2, 30, "Subsection", Evaluatable->False], Cell[5842, 200, 890, 21, 126, "Text", Evaluatable->False], Cell[6735, 223, 68, 1, 27, "Input"], Cell[6806, 226, 67, 1, 27, "Input"], Cell[6876, 229, 90, 1, 27, "Input"], Cell[6969, 232, 247, 4, 102, "Input"], Cell[7219, 238, 280, 5, 117, "Input"], Cell[7502, 245, 131, 3, 42, "Input"] }, Closed]], Cell[CellGroupData[{ Cell[7670, 253, 109, 2, 30, "Subsection", Evaluatable->False], Cell[7782, 257, 1299, 43, 80, "Text", Evaluatable->False] }, Closed]] }, Open ]] }, Open ]] } ] *) (*********************************************************************** End of Mathematica Notebook file. ***********************************************************************)