CSC151 2009F, Class 08: Writing Your Own Procedures Admin: * EC/Support: * Come watch women's soccer at 5:30 today. * Come watch David run at the Les Duke invitational this weekend. * EC/Academic: * Thursday extra at 4:30 in Science 3821. Jerod Weinman on cool vision stuff. * Thursday forum at 11:00 in Science 2022. Larry Dahl on Symmetry and Its Importance in Art and Science. * Assignment 3 is now ready. It follows up on today's lab. * The next reading (on lists) is new for this semester. * I'm behind in writing. * It is not yet ready for public consumption. * Please wait until Thursday afternoon to read it. * Given the shift in topics, you don't even need to do it until this weekend! Overview: * Review: Algorithm components. * Why define your own procedures? * How to define your own procedures (in Scheme). Side note: We're learning a new language * We need to learn the syntax of the language * We need to learn the semantics of the language * We need to learn the vocabulary of the language * The vocabulary is huge (or at least very large) * The dictionary is your friend (as is your notebook) Important Algorithm Components * Core building blocks: Basic values and the operations on those values + Numeric values, sqrt, +, -, *, etc + Symbols, [haven't used any] symbol? + Drawings: shift, scale, color, etc, height, width + Strings: string-append, string? + Images: select areas, fill selected area, open, show, height, width Note: In image-select-rectangle!, the ! is part of the procedure name, and means "hey, this modfiies the image!" * Sequence: Do this operation, then this operation, then this operation * Example: I have three things to do (1) scale the unit circle (2) shift it right 25 units (3) shift it down 30 units (4) recolor it blue * How do I tell Scheme to do those four things in that order? * "Inside out" Nest the calls (drawing-recolor (drawing-vshift (drawing-hshift (drawing-scale drawing-unit-circle 50) 25) 30) "blue") * Write sequence of instructions (define d1 (drawing-scale drawing-unit-circle 50)) (define d2 (drawing-hshift d1 25)) (define d3 (drawing-vshift d2 30)) (define d4 (drawing-recolor d3 "blue")) ... * Conditionals: If this condition holds, do this, otherwise, do that * We've learned the predicates, which will allow us to eventually write tests * We did see one cool trick for something we'd describe conditionally * Repetition: Doing things again and again and again and again * Haven't learned any explicit Scheme yet * We did learn how to make lots and lots of copies of a drawinga * We'll learn more of that on Monday * Explict repetition comes a bit later * Procedures: NAMED and PARAMETERIZED sets of instructions * By NAMED, we mean that we can refer to the set of instructions using (a helpful (hopefully)) name * By PARAMETERIZED, we mean that it takes input, and that we name the input * Our current goal is to learn how to make procedures in Scheme * Form or example? Example * Idea: Let's see how good of an estimate sqrt gives us > (- 2 (square (sqrt 2))) * Let's generalize that, so that the number we're checking is a parameter > (- n (square (sqrt n))) (define accuracy-of-square-root (lambda (n) (- n (square (sqrt n))))) * Form (define NAME-OF-PROCEDURE (lambda (NAMES OF PARAMETERS) SET OF INSTRUCTIONS)) (define exact-square-root? (lambda (n) (zero? (accuracy-of-square-root n)))) (define mario (lambda (x y) (* (+ x y) (- x y )))) Semantics: * When you apply a procedure to some values: * Figure out what name goes with which value * Plugs in those values for the names in the set of instructions * Evaluate the set of instructions