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

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: 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 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....
View Full Document

This note was uploaded on 07/26/2009 for the course COMPUTERSC CS51 taught by Professor Gregmorrisett during the Spring '09 term at Harvard.

Page1 / 9

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

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online