CSC151 2007S, Class 52: Metaprogramming Admin: * Due: Exam 3 * No reading for Monday. * EC for tonight's concert at 7:30. * I'm a bit disappointed at the 2 a.m. questions. * So ... * Why did 23 people pre-enroll in my section of 151? * Why did only 6 people pre-enroll in 152? Overview: * Code vs. Data in Scheme. * Rewriting Named let structures. * Generating Code for Objects. /Code vs. Data/ * What is (f 2 3) * The application of f to the values 2 and 3 * A list of three elements, f, 2, and 3 * An interesting feature of Scheme - code and data look the same * The interpretation depends on the context * Why is it useful to be able to switch between the representations? * General benefit of read: We can read 'entire things' (lists, including compound lists) * General benefit of read: If it's something that Scheme recognizes, we don't have to worry about figuring out what it is * Once you read a value, you can do different things with it "cut and paste", perhaps run it * You can (easily) write programs that manipulate the source code of other programs * Read * Modify * Write /Named Lets/ (let proc ((param1 init1) (param2 init2) ... (paramn initn)) body1 ... bodym) Vs. (letrec ((proc (lambda (param1 ... paramn) body1 ... bodym))) (proc init1 ... initn)) * A named let is a list * car of the list is the symbol let * cadr of the list is the procedure name, which is a symbol * caddr list of two element lists * car of each of those is a symbol * cadr of each is an expression * rest (cdddr) of the named let is a list of expressions, the body * To convert to the letrec form (define tmp (read-one-value "writenumbers.scm")) (define proc (cadr tmp)) (define params (caddr tmp)) (define body (cdddr tmp)) (define make-letrec (lambda (proc params body) (list 'letrec (list (list proc (cons 'lambda (cons (map car params) body)))) (cons proc (map cadr params))))) /Generating Code/ * Observation: Objects have a lot of very similar code (define make-object (lambda () (let ((field1 (vector _)) (field2 (vector _)) ...) (lambda (method . params) (cond ...))))) (define make-maker (lambda (object fields methods) (list 'define (string->symbol (string-append "make-" (symbol->string object))) (list 'let (list null) (list 'lambda (cons 'method 'params) (list 'cond ))))))