NOTE: **use Perl;** is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

Ovid

(email not shown publicly)

http://publius-ovidius.livejournal.com/

**AOL IM:** ovidperl (**Add Buddy, Send Message**)

(email not shown publicly)

http://publius-ovidius.livejournal.com/

Stuff with the Perl Foundation. A couple of patches in the Perl core. A few CPAN modules. That about sums it up.

Monday March 22, 2004

11:52 AM

OK, I know this place is about Perl, but I suspect a few people here are familiar with Scheme. Can anyone tell me why I get the following problem with mit-scheme?

1 ]=> (load "test.scm")

;Loading "test.scm" -- done

;Value: sqrt

1 ]=> (sqrt 4)

;Aborting!: maximum recursion depth exceeded

And `test.scm` looks like this:

(define (new-if predicate then-clause else-clause)

(cond (predicate then-clause)

(else else-clause)))

(define (sqrt x)

(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.001))

(sqrt-iter 1.0 x))

That works find if the `sqrt` definition has `new-if` changed to `if`. If you're curious, I got the problem from Section 1.1.7 of the Wizard book.

**Update:** problem solved. Corion on Perlmonks pointed out that `new-if` will always evaluate both arguments, thereby causing infinite recursion.

Stories, comments, journals, and other submissions on use Perl; are Copyright 1998-2006, their respective owners.

The Fine Print:The following comments are owned by whoever posted them. We are not responsible for them in any way.## Scheme Recursion Problem 0 Comments More | Login | Reply /

Loading... please wait.