lec14 - CSE341 Winter 2008 Lecture 14 Like letrec and...

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

; ;; Like letrec and unlike ML, Scheme lets variables be bound to subsequent ;; forward definitions (caught at run-time if undefined) ; ;(define x y) ;; not defined when evaluated ;(define y 1) ; (define f1 (lambda () y));; constant function, or so it seems (define y 1) ( ;; evil mutation (define f2 (lambda (x) (+ x y))) (define f3 (lambda () (begin (set! y 3) 7))) ;; (define y 4) ; (define (pow y) (lambda (x) (if (= y 0) 1 (* x ((pow (- y 1)) x))))) (define cube (pow 3)) ( ;; more evil redefinitions ;(define pow 6) ;; run-time failure ;(define (pow y) (lambda (x) 2)) ;; wrong answer ;(define - +) ;; now pow will go into an infinite loop ; ;; protect-yourself: copy-to-locals (define safe-pow (let ([= =] [* *] [- -]) (letrec ([f (lambda (y) (lambda (x) (if (= y 0) 1 (* x ((f (- y 1)) x)))))]) f))) ;; delayed evaluation (define (factorial x) (if (= x 0) 1 (* x (factorial (- x 1))))) (define (my-if-bad e1 e2 e3) (if e1 e2 e3)) (define (factorial2 x) (my-if-bad (= x 0)

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

View Full Document
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 / 3

lec14 - CSE341 Winter 2008 Lecture 14 Like letrec and...

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

View Full Document
Ask a homework question - tutors are online