lec14 - ; ; CSE341, Winter 2008, Lecture 14 ; Like letrec...

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

View Full Document Right Arrow Icon
; ;; 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)
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.

Page1 / 3

lec14 - ; ; CSE341, Winter 2008, Lecture 14 ; Like letrec...

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