Archives for March 2015

sicp-4-3-3

Posted on 2015-03-16 21:02:58 +0900 in SICP Lisp

4.51 It would always be (a b 1). 4.52 (define (analyze-if-fail exp) (let ((first (analyze (cadr exp))) (second (analyze (caddr exp)))) (lambda (env succeed fail) (first env (lambda (value fail2) (succeed value fail2)) (lambda () (second env succeed fail)))))) 4.53 (let ((pairs '())) (if-fail (let ((p (prime-sum-pair '(1 3 5...

Read more...

sicp-4-3-2

Posted on 2015-03-15 06:36:17 +0900 in SICP Lisp

4.39 (require (> miller cooper)) (require (not (= baker 5))) (require (not (= cooper 1))) (require (not (= fletcher 5))) (require (not (= fletcher 1))) (require (not (= (abs (- smith fletcher)) 1))) (require (distinct? (list baker cooper fletcher miller smith)))(require (not (= (abs (- fletcher cooper)) 1))) The answer...

Read more...

sicp-4-3-1

Posted on 2015-03-13 22:47:34 +0900 in SICP Lisp

I just find 4.3 very hard to understand. Eventhough I followed the author’s ideas, and find it work. But this time I found even try to understand the callings through debug would be very difficult. There should be theoretical foundations for this, finally, though this tutorial, I found the idea...

Read more...

sicp-4-2-3

Posted on 2015-03-12 20:27:03 +0900 in SICP Lisp

4.32 In the current version, car is lazy as well, which makes it possible to build structures such as infinite tree. 4.33 ;'(a b c)-->(cons 'a (cons 'b (cons 'c '()))) ((quoted? exp) (text-of-quotation exp env)) (define (list->cons lst) (if (null? lst) '() (list 'cons (list 'quote (car lst)) (list->cons...

Read more...

sicp-4-2-1

Posted on 2015-03-11 19:20:01 +0900 in SICP Lisp

4.25 (define (unless condition usual-value exceptional-value) (if condition exceptional-value usual-value)) (define (factorial n) (unless (= n 1) (* n (factorial (- n 1))) 1)) In applicative-order, factorial will enter an infinite loop, as the exceptional-value is evaluated regardless the condition. While in normal-order, the condition is judged before exceptional-value is...

Read more...

sicp-4-1-6-4-1-7

Posted on 2015-03-09 23:37:54 +0900 in SICP Lisp

4.16 a: (define (lookup-variable-value var env) (define (env-loop env) (define (scan vars vals) (cond ((null? vars) (env-loop (enclosing-environment env))) ((eq? var (car vars)) (if (equal? (car vals) '*unassigned*) (error "Unassigned variable" var) (car vals))) (else (scan (cdr vars) (cdr vals))))) (if (eq? env the-empty-environment) (error "Unbound variable" var) (let ((frame...

Read more...

sicp-4-1-3-4-1-5

Posted on 2015-03-06 20:12:10 +0900 in SICP Lisp

4.11 (define (make-frame variables values) (if (= (length variables) (length values)) (map cons variables values) (error "length mismatch -- MAKE-FRAME" variables values))) (define (frame-variables frame) (map car frame)) (define (frame-values frame) (map cdr frame)) 4.12 I can only come up with a find-binding-frame function and manually controls its process under...

Read more...

sicp-4-1

Posted on 2015-03-06 00:51:11 +0900 in SICP Lisp

4.1 It takes me several minutes to really understand the quanstion. :( It really only has something to do with the order of the parameter evaluation. To restrict the order, the parameters passed in to cons should be taken out explicitly before being used. (define (list-of-values-left-right exps env) (if (no-operands...

Read more...

sicp-3-5-4-3-5-5

Posted on 2015-03-05 06:11:43 +0900 in SICP Lisp

3.77 (define (integral delayed-integrand initial-value dt) (cons-stream initial-value (let ((integrand (force delayed-integrand))) (if (stream-null? integrand) the-empty-stream (integral (delay (stream-cdr integrand)) (+ (* dt (stream-car integrand)) initial-value) dt))))) 3.78 (define (integral delayed-integrand initial-value dt) (cons-stream initial-value (let ((integrand (force delayed-integrand))) (if (stream-null? integrand) the-empty-stream (integral (delay (stream-cdr integrand)) (+ (* dt...

Read more...

sicp-3-5-3

Posted on 2015-03-03 22:24:29 +0900 in SICP Lisp

3.63 guess is used to share the computation result between the succssive calling. Otherwise each call to sqrt-stream x would require recursively calling. If memo-proc is not used, these two versions would be the same efficiency. 3.64 (define (stream-limit s tolerance) (let ((s2 (stream-cdr s))) (if (< (abs (- (stream-car...

Read more...

sicp-3-5-1-3-5-2

Posted on 2015-03-02 20:24:25 +0900 in SICP Lisp

Code from the text. (define (stream-ref s n) (if (= n 0) (stream-car s) (stream-ref (stream-cdr s) (- n 1)))) (define (stream-map proc s) (if (stream-null? s) the-empty-stream (cons-stream (proc (stream-car s)) (stream-map proc (stream-cdr s))))) (define (stream-for-each proc s) (if (stream-null? s) 'done (begin (proc (stream-car s)) (stream-for-each proc...

Read more...

sicp-3-4

Posted on 2015-03-02 01:03:22 +0900 in SICP Lisp

3.39 101: P1 , P2 121: P2, P1 100: P1 computes (* x x), then P2 completes and sets x to 101, then P1 executes the assignment. 3.41 As withdraw and diposit are both single modification operations, it is not necessary to protect the balance value, as it would always...

Read more...

sicp-3-3-5

Posted on 2015-03-01 22:37:18 +0900 in SICP Lisp

3.33 (define (average a b c) (let ((x (make-connector)) (y (make-connector))) (adder a b x) (multiplier c v u) (constant 2 v)) 'ok) 3.34 it is only one-directianl computation. Given the value of b, the value of a can not be evaluated, as the multiplier does not know the left...

Read more...