-
Notifications
You must be signed in to change notification settings - Fork 0
/
newton_nested.rkt
42 lines (40 loc) · 1.16 KB
/
newton_nested.rkt
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
#lang scheme
; the sqrt proceedure with locally defined functions,
; so that they don't collide with other free variables/fns.
; Such nesting of definitions is called BLOCK STRUCTURE.
(define (sqrt x)
(define (good-enough? guess x)
(< (abs (- (square guess) x))
0.001))
(define (square x)
(* x x))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess
(/ x guess)))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(sqrt-iter 1.0 x))
; But there's a better idea lurking here.
; Since x is in the scope of sqrt 2, we don't have to explicitely
; pass it to other internal functions!
; We allow x to be a free variable inside the internal definitions.
; This is called LEXICAL SCOPING.
(define (sqrt2 x)
(define (good-enough? guess)
(< (abs (- (square guess) x))
0.001))
(define (square a)
(* a a))
(define (average a b)
(/ (+ a b) 2))
(define (improve guess)
(average guess (/ x guess)))
(define (sqrt-iter guess)
(if (good-enough? guess)
guess
(sqrt-iter (improve guess))))
(sqrt-iter 1.0))