From f7c95404a008e7ed6efdb2525cdf5d8a14a2f1ee Mon Sep 17 00:00:00 2001
From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com>
Date: Sun, 20 Nov 2011 10:46:49 +0530
Subject: [PATCH] solutions to 4.12 and 4.13.

---
 src/sicp/ex4_12.rkt | 40 ++++++++++++++++++++++++++++++++++++++++
 src/sicp/ex4_13.rkt | 21 +++++++++++++++++++++
 2 files changed, 61 insertions(+)
 create mode 100644 src/sicp/ex4_12.rkt
 create mode 100644 src/sicp/ex4_13.rkt

diff --git a/src/sicp/ex4_12.rkt b/src/sicp/ex4_12.rkt
new file mode 100644
index 0000000..d918daf
--- /dev/null
+++ b/src/sicp/ex4_12.rkt
@@ -0,0 +1,40 @@
+#lang racket
+
+;; given a frame, get the value of a given variable
+;; false if not var is not found in the frame
+(define (get-var-in-frame var frame)
+  ...)
+
+;; set a value for a given variable in a frame
+;; if frame already contain var, change its binding, else
+;; if frame does not have a binding for var, then create one.
+(define (set-var-in-frame! var val frame)
+  ...)
+
+;; now using these two procedures, expressing `lookup-variable-value'
+(define (lookup-variable-value var env)
+  (define (env-loop env)
+    (let ((frame (first-frame env)))
+      (let ((var-val (get-var-in-frame var frame)))
+        (if var-val
+            var-val
+            (env-loop (enclosing-environment env))))))
+  (if (eq? env the-empty-environment)
+      (error "Unbound variable" var)
+      (env-loop env)))
+
+
+(define (set-variable-value! var val env)
+  (define (env-loop env)
+    (let ((frame (first-frame env)))
+      (let ((var-val (get-var-in-frame var frame)))
+        (if var-val
+            (set-var-in-frame! var val frame)
+            (env-loop (enclosing-environment env))))))
+  (if (eq? env the-empty-environment)
+      (error "Unbound variable -- SET!" var)
+      (env-loop env)))
+
+(define (define-variable! var val env)
+  (let ((frame (first-frame env)))
+    (set-var-in-frame! var val frame)))
diff --git a/src/sicp/ex4_13.rkt b/src/sicp/ex4_13.rkt
new file mode 100644
index 0000000..55535b2
--- /dev/null
+++ b/src/sicp/ex4_13.rkt
@@ -0,0 +1,21 @@
+#lang racket
+
+(define (make-unbound! var val env)
+  (let ((frame (first-frame env)))
+    (define (scan vars vals)
+      (cond ((null? vars)
+             (error "variable not bound" var))
+            ((eq? var (car vars))
+             (begin
+               (set-car! vals (cdr vals))
+               (set-cdr! vars (cdr vars))))
+            (else (scan (cdr vars) (cdr vals)))))
+    (scan (frame-variables frame)
+          (frame-values frame))))
+
+#|
+
+Remove binding from the first frame, as removing it from elsewhere
+would mean also removing all bindings built from it.
+
+|#
\ No newline at end of file
-- 
2.45.2