20-CS-4003-001 Organization of Programming Languages Fall 2017

Lambda calculus, Type theory, Formal semantics, Program analysis

    Prev     Next     All lectures        Code

Macros in the Scheme language

(define x 2)
(define w 2)

(define f 
  (lambda (z) 
    (let ((temp x)) 
      (begin (set! x (* 2 z)) temp))))
(define g 
  (lambda (z) 
    (let ((temp w)) 
      (begin (set! w (* 2 z)) temp))))

;; Use let so x and y are called 
;; just one time at most
(define-syntax mini
  (syntax-rules ()
    ((mini a b) 
     (let ((x a) (y b)) 
       (if (< x y) x y)))))

(define-syntax mini-1
  (syntax-rules ()
    ((mini-1 a b) (if (< a b) a b))))

(define main
  (lambda ()
    (let ((b 3) (c 1))
      (cons (mini (+ b c) (f 3)) 
            (mini-1 (+ b c) (g 3))))))
 -  The corresponding MIN macro in Scheme has the same problem as in C but is easily controlled via the let.