Home > Archive > Scheme > March 2007 > ex1.6 of the SICP book
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
ex1.6 of the SICP book
|
|
| Matthew Zhou 2007-03-19, 8:12 am |
| Any one also reading the book, Structure and Interpretation of
Computer Programs may help. I cannot work out the answer of ex1.6
I translated this problem to the code below, what's wrong with it?
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(esle else-clause)))
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (improve guess x)
(average guess (/ x guess)))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.1))
(define (square x)
(* x x))
(define (abs x)
(if (> x 0) x
(- x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
As I run this program, the interpreter gave me the message below:
;Aborting!: maximum recursion depth exceeded
| |
| Jussi Piitulainen 2007-03-19, 8:12 am |
| Matthew Zhou writes:
> Any one also reading the book, Structure and Interpretation of
> Computer Programs may help. I cannot work out the answer of ex1.6
>
> I translated this problem to the code below, what's wrong with it?
>
> (define (new-if predicate then-clause else-clause)
> (cond (predicate then-clause)
....
It's a matter of timing: sometimes, like in this exercise, it really
is too late to decide whether you want to compute something or not;
you have done it already! The argument names are misleading.
> As I run this program, the interpreter gave me the message below:
> ;Aborting!: maximum recursion depth exceeded
That is an expected result. The exercise asks for an explanation. How
does a procedure call, like (acos (- 1)), work? How does a conditional
expression, like (if (> 0 0) (/ 0) 0), work?
|
|
|
|
|