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
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)
(define (sqrt x)
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
(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.