From: Ramakrishnan Muthukrishnan Date: Sun, 13 Nov 2011 15:55:16 +0000 (+0530) Subject: solutions to 4.9, 4.10 and 4.11 X-Git-Url: https://git.rkrishnan.org/(%5B%5E?a=commitdiff_plain;h=7a5e9033d69843fa6aa84e0b36eed26ee54fe292;p=sicp.git solutions to 4.9, 4.10 and 4.11 --- diff --git a/src/sicp/ex4_10.rkt b/src/sicp/ex4_10.rkt new file mode 100644 index 0000000..4708f50 --- /dev/null +++ b/src/sicp/ex4_10.rkt @@ -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 index 0000000..00f79a2 --- /dev/null +++ b/src/sicp/ex4_11.rkt @@ -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 index 0000000..946ba20 --- /dev/null +++ b/src/sicp/ex4_9.rkt @@ -0,0 +1,40 @@ +#lang racket + +;; 4.9 +#| + +do, for, while, until, when + +a. for + +(for in + ) + +bind var with each value in sequence, execute the and +collect the result of the last expression into a list. + +b. (dotimes n + ) + +Bind n to the number and execute the body that many times for +side effects. + +c. (while + ) + +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'. + +|# +