From: Ramakrishnan Muthukrishnan Date: Sat, 19 Nov 2011 05:05:38 +0000 (+0530) Subject: bug fixes X-Git-Url: https://git.rkrishnan.org/specifications/%5B/%5D%20/uri/flags/$rel_link?a=commitdiff_plain;h=018629b42d482b2825b75c0622b04212197e81af;p=sicp.git bug fixes --- diff --git a/src/sicp/ex4_11.rkt b/src/sicp/ex4_11.rkt index 00f79a2..0b60119 100644 --- a/src/sicp/ex4_11.rkt +++ b/src/sicp/ex4_11.rkt @@ -1,4 +1,8 @@ -#lang racket +#lang r5rs + +(define (error x) + (display x) + (newline)) (define (make-frame variables values) (if (not (= (length variables) @@ -13,4 +17,49 @@ (map cdr frame)) (define (add-binding-to-frame! var val frame) - (set! frame (cons (cons var val) frame))) + (define (add-to-end-of-frame! b f) + (if (null? (cdr f)) + (set-cdr! f b) + (add-to-end-of-frame! b (cdr f)))) + (add-to-end-of-frame! (cons var val) frame)) + +(define (lookup-variable-value var env) + (define (env-loop env) + (define (scan frame) + (if (null? frame) + (env-loop (enclosing-environment env)) + (let ((p (car frame))) + (if (eq? var (car p)) + (cdr p) + (scan (cdr frame)))))) + (if (eq? env the-empty-environment) + (error "variable not defined") + (let ((f (first-frame env))) + (scan f)))) + (env-loop env)) + +(define (set-variable-value! var val env) + (define (env-loop env) + (define (scan frame) + (if (null? frame) + (env-loop (enclosing-environment env)) + (let ((p (car frame))) + (if (eq? var (car p)) + (set-cdr! p val) + (scan (cdr frame)))))) + (if (eq? env the-empty-environment) + (error "variable not defined") + (let ((f (first-frame env))) + (scan f)))) + (env-loop env)) + +(define (define-variable! var val env) + (let ((frame (first-frame env))) + (define (scan frame) + (if (null? frame) + (add-binding-to-frame var val frame) + (let ((p (car frame))) + (if (eq? (car p) var) + (set-cdr! p val) + (scan (cdr frame)))))) + (scan frame))) diff --git a/src/sicp/ex4_7.rkt b/src/sicp/ex4_7.rkt index 956095d..8bb4aa0 100644 --- a/src/sicp/ex4_7.rkt +++ b/src/sicp/ex4_7.rkt @@ -1,5 +1,7 @@ #lang racket +(provide let*? let*->nested-lets) + (define (tagged-list? exp tag) (if (pair? exp) (eq? (car exp) tag) diff --git a/src/sicp/ex4_8.rkt b/src/sicp/ex4_8.rkt index d58eb4d..1647aea 100644 --- a/src/sicp/ex4_8.rkt +++ b/src/sicp/ex4_8.rkt @@ -1,6 +1,6 @@ #lang racket -(provide let->combination) +(provide let->combination let?) (define (tagged-list? exp tag) (if (pair? exp)