]> git.rkrishnan.org Git - sicp.git/commitdiff
solutions to 4.9, 4.10 and 4.11
authorRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sun, 13 Nov 2011 15:55:16 +0000 (21:25 +0530)
committerRamakrishnan Muthukrishnan <vu3rdd@gmail.com>
Sun, 13 Nov 2011 15:55:16 +0000 (21:25 +0530)
src/sicp/ex4_10.rkt [new file with mode: 0644]
src/sicp/ex4_11.rkt [new file with mode: 0644]
src/sicp/ex4_9.rkt [new file with mode: 0644]

diff --git a/src/sicp/ex4_10.rkt b/src/sicp/ex4_10.rkt
new file mode 100644 (file)
index 0000000..4708f50
--- /dev/null
@@ -0,0 +1,8 @@
+#lang racket
+
+#|
+
+The assignment for example can be changed from 'set!' to 'setq' for instance.
+Only the leaf functions which checks for the type tag needs to be changed.
+
+|#
\ No newline at end of file
diff --git a/src/sicp/ex4_11.rkt b/src/sicp/ex4_11.rkt
new file mode 100644 (file)
index 0000000..00f79a2
--- /dev/null
@@ -0,0 +1,16 @@
+#lang racket
+
+(define (make-frame variables values)
+  (if (not (= (length variables)
+              (length values)))
+      (error "Number of variables and values should be the same -- MAKE-FRAME")
+      (map cons variables values)))
+
+(define (frame-variables frame)
+  (map car frame))
+
+(define (frame-values frame)
+  (map cdr frame))
+
+(define (add-binding-to-frame! var val frame)
+  (set! frame (cons (cons var val) frame)))
diff --git a/src/sicp/ex4_9.rkt b/src/sicp/ex4_9.rkt
new file mode 100644 (file)
index 0000000..946ba20
--- /dev/null
@@ -0,0 +1,40 @@
+#lang racket
+
+;; 4.9
+#|
+
+do, for, while, until, when
+
+a. for
+
+(for <var> in <seq>
+  <body>)
+
+bind var with each value in sequence, execute the <body> and 
+collect the result of the last expression into a list.
+
+b. (dotimes n <number>
+     <body>)
+
+Bind n to the number and execute the body that many times for
+side effects.
+
+c. (while <predicate>
+     <body>)
+
+Check the value of the predicate, if true, execute the body,
+check the predicate again until it is false, in which case,
+stop executing the body.
+
+|#
+
+#|  implementation:
+
+Derived expressions can be used to implement all the above 
+constructs. For example, 'for' can be implementing by transforming
+the body into an iterative procedure. 'dotimes' is similar and
+simpler. 'while' can be implemented using recursive procedure
+and 'if'.
+
+|#
+