From 7a5e9033d69843fa6aa84e0b36eed26ee54fe292 Mon Sep 17 00:00:00 2001 From: Ramakrishnan Muthukrishnan <vu3rdd@gmail.com> Date: Sun, 13 Nov 2011 21:25:16 +0530 Subject: [PATCH] solutions to 4.9, 4.10 and 4.11 --- src/sicp/ex4_10.rkt | 8 ++++++++ src/sicp/ex4_11.rkt | 16 ++++++++++++++++ src/sicp/ex4_9.rkt | 40 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 64 insertions(+) create mode 100644 src/sicp/ex4_10.rkt create mode 100644 src/sicp/ex4_11.rkt create mode 100644 src/sicp/ex4_9.rkt 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 <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'. + +|# + -- 2.45.2