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

All the Perl that's Practical to Extract and Report

Ovid (2709)

Ovid
(email not shown publicly)
http://publius-ovidius.livejournal.com/
AOL IM: ovidperl (Add Buddy, Send Message)

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

Journal of Ovid (2709)

Monday March 22, 2004
11:52 AM

Scheme Recursion Problem

[ #18011 ]

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?

;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.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.