CSC151 2007S, Class 08: Writing Your Own Procedures Admin: * We'll spend a little bit of time going over the first quiz. * Assignment 2 returned. We'll go over it a bit. * Are there questions on Assignment 3? * You may only use one image-creation model for each of the three images * I still haven't completely caught up on grading, but I'm getting closer. * Advance notice: Extra credit for attending Thursday's convo. * Thusdays at 11 a.m. * These days, usually in JRC * But maybe in Herrick on Thursday * There are two readings for tomorrow, one on numeric values one on symbolic values Overview: * Why define your own procedures? * How to define your own procedures. * Lab! ==Notes on the Quiz== * Most of the problems were on problem 3 * I don't recommend that you compare grades, as such comparison rarely provides a net gain in happiness ===Notes on Problem 3=== (define new-width (/ image-width 4)) * Note that image-width is a procedure. Dividing a procedure by an integer (or by anything) makes no sense. The correct answer is An error would be reported. * Some of you said that "The image width would shrink to 1/4 of its original size". However, definitions generally don't change the underlying thing. * Procedures that do change the underlying value usually end with an exclamation point * If we wanted to use a procedure to change the width, we might use something like image-set-width!. (No such procedure exists.) ==Notes on Assignment 2== * Good lesson for Sam: Don't ask for complex instructions. * Be careful of equality in describing circles. (Yes, this will be an issue.) * Drawing the yellow part of the circle with turtles was hard. ==Why Define Your Own Procedures?== * YOu've been using procedures as long as you've been using Scheme * turtle-forward! * turtle-down! * image-show * + * Where do they come from? * Some are built-in to the language * Some come from the DrFu library, which means that a Grinnellian wrote them * Some come from GIMP/Script-Fu, some wiley hacker wrote them * Most programmers (eventually) want to write their own procedures * Avoids repetitious code * Copy and paste is a pain * Copy and paste and update is more of a pain * Copy and paste and FIX is even more of a pain * Clarifies code repeat 100 times repeat 360 times forward 01.cm turn 1 degree turn 90 degrees forward 0.1cm turn -90 degrees VS (turtle-draw-filled-circle! ...) * NOTE: THE PREVIOUS CODE DOES NOT DRAW A FILLED CIRCLE! IT HAS A BUG * Encourages us to generalize code * Make the preceding code draw a filled circle of an arbitrary radius ==How to Define Your Own Procedures== (define NAME-OF-PROC (lambda (PARAM_1 ... PARAM_N) INSTRUCTIONS)) ; THE FOLLOWING CODE HAS A BUG, TOO (define turtle-draw-filled-circle! (lambda (turtle radius) (repeat! 100 (repeat! 360 (turtle-forward! turtle (/ (* 2 pi radius) 360)) (turtle-turn! turtle 1)) (turtle-turn! turtle 90) (turtle-forward! turtle 0.1) (turtle-turn! turtle -90) (set! radius (- radius 0.1)))) ==Lab==