section09 - Section Notes 9 CS51-Spring 2009 Week of 1...

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Section Notes 9 CS51—Spring 2009 Week of April 12, 2009 1 Outline 1. Macros 2. Lazy Lists 3. Least squares fitting 4. Gradient descent 5. Mutation and aliasing At the end of this section, you should be able to write simple macros, understand streams, and understand some of the concepts behind fitting 2D data to the line of best fit. 2 Macros Recall John Scheme, and his attempt to rewrite or in the following way: (define (my-or val1 val2) (if val1 #t val2)) Why didn’t his version work? Because the Scheme interpreter will evaluate val1 and val2 as they get passed into my-or , as opposed to or , which properly delays evaluation. or is a special-form in Scheme, as are many of the other built-in functions we’ve worked with, such as and and cond and if , which all have special evaluation rules that are different from the typical order in which Scheme evaluates functions. If we want to delay evaluation of certain functions, we can define a macro in Scheme. Defining a macro is like defining a function, but instead of immediately evaluating the arguments, as with regular functions, it will replace the syntax around the arguments with whatever is specified in the definition, then evaluate it. Macros are defined as follows: (define-syntax-rule (NAME [ARG...]) (DEFINITION incorporating the ARGS provided) Use ... in place of ARGS to specify an unknown number of arguments. Macros do syntactic replacement of the parameters with the structure in the definition. A fixed definition of my-or: (define-syntax-rule (my-or-fixed val1 val2) (if val1 #t val2)) Here’s a mental model for thinking about how Scheme handles macros: 1. Find all the calls to macros in the program. 1
Image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
2. Replace each call with the body of the macro, replacing the arguments with the expressions that are being passed as parameters. 1 3. Now that there aren’t any macros, run the program using the usual Scheme evaluation rules that we know and love. Thus, a call to (my-or-fixed (= a 0) (/ a (- 2 2))) will take the arguments and replace the val1 and val2 arguments to the macro with the expressions passed: (if (= a 0) #t (/ a (- 2 2))) . Then it’ll use the rules for if , and thus avoid evaluating the division by 0. 2.1 Macro examples Define a macro to increment a variable by a certain value. ; Here is an example in use: (define x 5) (+= x 2) ; x => 7 (define-syntax-rule (+= variable value) ( Define a macro to add one or more values to a variable. ; Here is an example in use: (define x 2) (+= x 7) ; x => 9 (+= x 3 4) ; x => 16 (define-syntax-rule (+= variable value ...) ( Define a macro for swapping the contents of two variables. (define-syntax-rule (swap x y) ( Define a macro for the while-loop. 1 There is a bit more magic about replacing duplicated variable names, but this is pretty close. See the lecture notes for an example. 2
Image of page 2
(define-syntax-rule (while e1 e2 ...) (letrec 3 Lazy Lists or Streams A stream consists of 1) a current item, and 2) a “promise” to produce the rest of the stream on request. In Scheme, a stream is a cons cell whose cdr is a function that returns a stream. Note the similarity to the definition of a list. The critical difference is that the cdr is not itself a stream, but an unevaluated
Image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern