-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathex_1_6.scm
50 lines (38 loc) · 1.22 KB
/
ex_1_6.scm
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
41
42
43
44
45
46
47
48
49
50
; (define (sqrt-iter guess x)
; (if (good-enough? guess x)
; guess
; (sqrt-iter (improve guess x) x)
; )
; )
(define (square x)
(* x x)
)
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001)
)
(define (improve guess x)
(average guess (/ x guess))
)
(define (average x y)
(/ (+ x y) 2)
)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(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)
)
)
; The problem is that the new-if is now a procedure, thus the interpreter first evaluates
; the operator, and the arguments, and then tries to apply the combination, but the second
; argument is a call to itself, generating an infinite chain of calls.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(define (sqrt x)
(sqrt-iter 1.0 x)
)