-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex1.6
40 lines (28 loc) · 969 Bytes
/
ex1.6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
(define (sqr x)
(* x x))
(define (abs x)
(if (< x 0) (- x) x))
(define (avg x y)
(/ (+ x y) 2))
(define (improve guess x)
(avg guess (/ x guess)))
(define (good-enough? guess x)
(< (abs (- (sqr guess) x)) 0.001))
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(define (sqrt-iter guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
;; OUTPUT ;;
;> (load "ex1.6")
;Loading "ex1.6.clj"... done
;Value: sqrt
;> (sqrt 36)
;Aborting!: maximum recursion depth exceeded
; It means, The default if statement is a special form which means that even when an interpreter follows applicative substitution,
; it only evaluates one of it's parameters- not both. However, the newly created new-if doesn't has this property and hence,
; it never stops calling itself due to the third parameter passed to it in sqrt-iter.