This repository has been archived by the owner on Jun 27, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
excersises_1_7.scm
124 lines (90 loc) · 2.08 KB
/
excersises_1_7.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
(define (abs x)
(cond ((< x 0) (- x))
((> x 0) x)
((= x 0) 0)))
(define (abs1 x)
(cond ((< x 0) (- x))
(else x)))
(define (abs2 x)
(if (< x 0)
(- x)
x))
(define (>= x y)
(or (> x y) (= x y)))
(define a 3)
(define b (+ a 1))
(+ a b (* a b))
(= a b)
(if (and (> b a) (< b (* a b)))
b
a)
(cond ((= a 4) 6)
((= b 4) (+ 6 7 a))
(else 25))
(/ (+ 5 4
(- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3
(- 6 2)
(- 2 7)))
(define (max x y)
(if (> x y) x y))
(define (sqr x)
(* x x))
(define (sum-squares a b c)
(+ (sqr (max a b)) (sqr (max b c))))
(sum-squares 1 2 3)
(define (a-plus-abs-b a b)
((if (> b 0) + -) a b))
(a-plus-abs-b 1 -2)
;; endless recursion
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;; don't try this at home
;; if you r using applicative eval order
;; (test 0 (p))
(define (average x y)
(/ (+ x y) 2))
(define (good-enough? guess x)
(< (abs (- (sqr guess) x)) 0.001))
(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)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt (+ 100 37))
(sqrt (+ (sqrt 2) (sqrt 3)))
(sqr (sqrt 1000))
(define (new-if predicate then-clause else-clause)
(cond (predicate then-clause)
(else else-clause)))
(new-if (= 5 0) 2 3)
(new-if (= 1 1) 4 5)
(define (sqrt-iter1 guess x)
(new-if (good-enough? guess x)
guess
(sqrt-iter1 (improve guess x) x )))
(define (sqrt1 x)
(sqrt-iter1 1.0 x))
(sqrt 0.00001)
;; endless recursion cause of applicative eval order
;; if is a special form, that doesn't use appl. eval order
;; (sqrt1 9)
(define (good-enough1? prev-guess curr-guess)
(< (abs (- prev-guess curr-guess)) 0.00000000001))
(define (sqrt-iter2 prev-guess curr-guess x)
(if (good-enough1? prev-guess curr-guess)
curr-guess
(sqrt-iter2 curr-guess (improve curr-guess x) x)))
(define (sqrt2 x)
(sqrt-iter2 0.9 1.0 x))
(sqrt2 2)
(sqrt2 9)
(sqrt2 9000)
(sqrt2 10050010050010050100)
(sqrt2 0.00000001)