September 14 lecture notes ------------------------------------------------------------- I) go over homework II) questions ? III) this week : programs --- Modules ----------------------------------- myProgram::usage="This is the documentation. So there."; myProgram[stuff_] := Module[{a,b}, (* temporary variables *) doThis[stuff]; a=10; (* a comment about "a" *) b=20; (* a comment about "b" *) doThat[a,b]; doSomethingElse[]; ]; := delayed execution stuff_ match a single thing Module a sequence of things to do (* *) internal comment, "how it works" documentation ::usage external API, "how to use it" documentation ; put a semi-colon between consecutive things to do {a,b}, put local variable names at the beginning of the module --- Loops & Conditionals ------------------------------------ Do[ this[i]; (* do this with i=1, i=2, ..., i=n *) that[], {i,1,n}] (* or just *) Do[ this[]; (* do this stuff n times *) that[]; other[], {n}] If[ booleanExpression, stuffToDoIfTrue[], stuffToDoIfFalse[] ]; While[ booleanExpression, stuffToRepeat[] ]; --- in class practice ---------------------------------------- i. the turtle world * find a recipe to draw * a triangle * a square * a hexagon * write a program to draw an arbitrary n-gon * what arguments do you want to pass to it? * write a program to draw a collage of randomly chosen shapes, with random colors * what arguments might you pass to it? * turtle homework for this week ii. perfect number n : sum of factors < n == n example: 6 is perfect: 1 + 2 + 3 = 6 * write a program that finds the n'th perfect number * ... but first write one that tests whether a number is perfect * ... but first write one that returns a list of the factors of a number iii. back up a bit : * How to we create these recipes (i.e. algorithms) ? * Isn't any simple answer. But there are hints. * Hints: * break problem into smaller pieces * an art that can be developed with practice * state recipe with words and/or pictures before code * be as explicit as you can * How do we know that they work ? * TESTING and DEBUGGING (more on this later) * assume nothing; Print[] often; evaluate small bits and watch it. * How do we keep from re-inventing the wheel ? * DOCUMENTATION * SMALL RE-USABLE PIECES ------------------------------------------------------------------- screen output : Print screen input : Input, InputString