1

Make "if" with "cond"? Why not?

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

And next I tried to use it in some function:

(define (sqrt x)
    (define (sqrt-iter guess x)
      (new-if 
        (< 
          (abs (- (* guess guess) x)) 
          0.0001 )
        guess
        (sqrt-iter
          (/ (+ guess (/ x guess)) 2)
          x )))
    (sqrt-iter 1. x) )

(sqrt 3)

Why I get "maximum recursion depth exceeded"?

Daniel
  • 11
  • 2
  • I'm pretty sure this is a duplicate. IF and COND don't unconditionally evaluate their arguments, whereas function calls do. `(if #t (display "true") (display "false"))` only prints "true" or "false", but not both. However, with `(define (my-if condition then else) ...)`, you will always print two things if you do `(my-if #t (display "true") (display "false"))`. – Joshua Taylor Jun 05 '15 at 19:55

0 Answers0