I've messed with this for quite awhile and I just can't figure it out. The book gives some definitions for various procedures to be used in the computation of the square root of a number, as follows:
(define (sqrt-iter guess x)
(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.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
That isn't extremely important. However, the book then poses the question regarding definition a "new-if" as such:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
Using this, it redefines sqrt-iter as such:
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
Basically using a cond instead of if through another procedure. Now, it says "What happens when Alyssa (fictional character) attempts to use this to compute square roots? Explain."
Now I've done this myself and found it justs pauses at execution (perhaps continually running sqrt-iter, meaning the cond is always evaluating good-enough? to false), but I can't figure out exactly why this is occurring. I see nothing wrong in the structure of the code or the concept of using cond instead of if (and if instead of using the new-if procedure I actually type in the cond statement, it seems to work).
So, if anyone knows Scheme well, please help me. I suspect that maybe you aren't allowed to use functions as parameters in other function definitions (which would be the case in passing the function (good-enough? guess x) to new-if). However, I want to be sure about this.
TIA. If you need more details to help me out, let me know.
EDIT: I'm still experimenting so here's more of what I found. If I define new-if as follows:
(define (new-if predicate then-clause else-clause)
(if predicate then-clause else-clause))
It still does not work within the sqrt-iter function (however, it does work, as does the original version with cond, by itself). Furthermore, I've tried defining a junk function that does a very simple test like this:
(define (m x y) (< x y))
Then I tried this with new-if--(new-if (m 1 2) 3 4) returns 3, (new-if (m 2 1) 3 4) returns 4)--and it works just fine, meaning that you are allowed to pass a function to another function (I originally thought this to be true).
(define (sqrt-iter guess x)
(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.001))
(define (sqrt x)
(sqrt-iter 1.0 x))
That isn't extremely important. However, the book then poses the question regarding definition a "new-if" as such:
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
Using this, it redefines sqrt-iter as such:
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x)
x)))
Basically using a cond instead of if through another procedure. Now, it says "What happens when Alyssa (fictional character) attempts to use this to compute square roots? Explain."
Now I've done this myself and found it justs pauses at execution (perhaps continually running sqrt-iter, meaning the cond is always evaluating good-enough? to false), but I can't figure out exactly why this is occurring. I see nothing wrong in the structure of the code or the concept of using cond instead of if (and if instead of using the new-if procedure I actually type in the cond statement, it seems to work).
So, if anyone knows Scheme well, please help me. I suspect that maybe you aren't allowed to use functions as parameters in other function definitions (which would be the case in passing the function (good-enough? guess x) to new-if). However, I want to be sure about this.
TIA. If you need more details to help me out, let me know.
EDIT: I'm still experimenting so here's more of what I found. If I define new-if as follows:
(define (new-if predicate then-clause else-clause)
(if predicate then-clause else-clause))
It still does not work within the sqrt-iter function (however, it does work, as does the original version with cond, by itself). Furthermore, I've tried defining a junk function that does a very simple test like this:
(define (m x y) (< x y))
Then I tried this with new-if--(new-if (m 1 2) 3 4) returns 3, (new-if (m 2 1) 3 4) returns 4)--and it works just fine, meaning that you are allowed to pass a function to another function (I originally thought this to be true).