• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

Need Scheme/LISP help

esun

Platinum Member
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).
 
OMG! SCHEME!!! I assume you are doing this for school? I promptly purged ALL my scheme knowledge when I finished those horrible classes... I'm lucky I passed because I blocked most of the Scheme knowledge from entering my brain before I could learn it in the first place... I hope you figure out your code though!

Try finding the smart guy in your class, and ask him for help. There's always some smart dude who throws off the curve. 😉

Or maybe there is a study group? Any TA's you can talk to?

I'm suggesting other alternatives to asking here because there are so few people who know Scheme. I don't even know why anyone uses it for teaching anymore...
 
Yeah, it's for the introductory programming class at UC Berkeley (MIT uses the same book, so they probably have the same course as well). I've done programming in C++, Java, and Perl, and I've read a lot of Scheme already, but this little problem is just becoming an annoyance. I think I have a lab section today, so maybe I can ask someone there. Thanks for the advice, though.
 
Do you have a scheme debugger? I think that would be the easiest way to determine what's wrong. To be honest, I don't see why it wouldn't work, but I'll poke around a bit to see if something comes to mind.
 
Actually, I do (at least I think there's one in emacs or something). I've just never used it before with Scheme. I'll play around with it to see what I can come up with. The funny thing is I've already completed the portion of the homework that requires actually writing programs. This conceptual problem is the only part I can't answer.
 
Back
Top