]> git.rkrishnan.org Git - sicp.git/commitdiff
solutions to 4.50..4.54
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Tue, 26 Mar 2013 12:57:48 +0000 (18:27 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Tue, 26 Mar 2013 12:57:48 +0000 (18:27 +0530)
src/sicp/ex4_50.rkt [new file with mode: 0644]
src/sicp/ex4_51.rkt [new file with mode: 0644]
src/sicp/ex4_52.rkt [new file with mode: 0644]
src/sicp/ex4_54.rkt [new file with mode: 0644]

diff --git a/src/sicp/ex4_50.rkt b/src/sicp/ex4_50.rkt
new file mode 100644 (file)
index 0000000..69f61b8
--- /dev/null
@@ -0,0 +1,13 @@
+#lang racket
+
+#|
+
+In this original definition of amb-choices,
+
+(define (amb-choices expr) (rest expr))
+
+instead return the shuffled values
+
+(define (amb-choices expr) (shuffle (rest expr)))
+
+|#
\ No newline at end of file
diff --git a/src/sicp/ex4_51.rkt b/src/sicp/ex4_51.rkt
new file mode 100644 (file)
index 0000000..ccb5caa
--- /dev/null
@@ -0,0 +1,28 @@
+#lang racket
+
+;;; assignment
+(define (analyze-assignment expr)
+  (let ([var (assignment-var expr)]
+        [vproc (analyze (assignment-value expr))])
+    (lambda (env succeed fail)
+      (vproc env
+             (lambda (val fail2)
+               (let ([old-val (lookup-variable-value var env)])
+                 (set-variable-value! var val)
+                 (succeed 'ok
+                          (lambda ()
+                            (set-variable-value! var old-val env)
+                            (fail2)))))
+             fail))))
+
+;; permenent-set!
+;;; assignment
+(define (analyze-permanent-assignment expr)
+  (let ([var (perm-assignment-var expr)]
+        [vproc (analyze (perm-assignment-value expr))])
+    (lambda (env succeed fail)
+      (vproc env
+             (lambda (val fail2)
+               (set-variable-value! var val)
+               (succeed 'ok fail2))
+             fail))))
diff --git a/src/sicp/ex4_52.rkt b/src/sicp/ex4_52.rkt
new file mode 100644 (file)
index 0000000..3c60f2c
--- /dev/null
@@ -0,0 +1,12 @@
+#lang racket
+
+;; if-fail
+
+(define (analyze-if-fail expr)
+  (let ([sproc (analyze (if-fail-success expr))]
+        [fproc (analyze (if-fail-failure expr))])
+    (lambda (env succeed fail)
+      (sproc env
+             succeed
+             (lambda ()
+               (fexpr env succeed fail))))))
\ No newline at end of file
diff --git a/src/sicp/ex4_54.rkt b/src/sicp/ex4_54.rkt
new file mode 100644 (file)
index 0000000..79dcb0c
--- /dev/null
@@ -0,0 +1,15 @@
+#lang racket
+
+(define (assert? expr) (tagged-list? expr 'assert))
+
+(define (assert-predicate expr) (first (rest expr)))
+
+(define (analyze-assert exp)
+  (let ((pproc (analyze (assert-predicate exp))))
+    (lambda (env succeed fail)
+      (pproc env
+             (lambda (pred-value fail2)
+               (if (not (true? pred-value))
+                   (fail)
+                   (succeed 'ok fail2)))
+             fail))))
\ No newline at end of file