lec15 - ; ; CSE341 Spring 2008, Scheme macros ; a cosmetic...

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

View Full Document Right Arrow Icon
;; CSE341 Spring 2008, Scheme macros ; ;; a cosmetic macro -- adds then, else (define-syntax my-if (syntax-rules (then else) [(my-if e1 then e2 else e3) (if e1 e2 e3)])) ;; a macro to delay evaluation (call-by-need) (define-syntax my-delay (syntax-rules () [(my-delay e) (cons #f (lambda () e))])) (define-syntax my-force (syntax-rules () [(my-force e) (let ([x e]) (if (car x) (cdr x) (begin (set-car! x #t) (set-cdr! x ((cdr x))) (cdr x))))])) ;; a bad-style macro -- a function is better (define-syntax double (syntax-rules () [(double e) (* 2 e)])) ;(define (double x) (* 2 x)) ; ;; a worse macro -- effects repeated (define-syntax double2 (syntax-rules () [(double2 e) (+ e e)])) ;; equal in badness to double, thanks to hygiene (define-syntax double3 (syntax-rules () [(double3 e) (let* ([zero 0] [x e]) (+ x x zero))])) ;; in particular, hygiene makes this work how we want (define (f zero) (double3 zero)) (define x (f 17)) ( ;; another example where you want to use locals (define-syntax bad-from
Background image of page 1

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

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

This note was uploaded on 10/12/2009 for the course CSE 341 taught by Professor Staff during the Spring '08 term at University of Washington.

Page1 / 2

lec15 - ; ; CSE341 Spring 2008, Scheme macros ; a cosmetic...

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

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