File tree Expand file tree Collapse file tree 8 files changed +157
-0
lines changed
1.1-TheElementsOfProgramming Expand file tree Collapse file tree 8 files changed +157
-0
lines changed Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+
3
+ 10
4
+ ; 10
5
+
6
+ (+ 5 3 4 )
7
+ ; 12
8
+
9
+ (- 9 1 )
10
+ ; 8
11
+
12
+ (+ (* 2 4 ) (- 4 6 ))
13
+ ; 6
14
+
15
+ (define a 3)
16
+ ;
17
+
18
+ (define b (+ a 1 ))
19
+ ;
20
+
21
+ (+ a b (* a b))
22
+ ; 19
23
+
24
+ (= a b)
25
+ ; false
26
+
27
+ (if (and (> b a) (< b (* a b)))
28
+ b
29
+ a)
30
+ ; 4
31
+
32
+ (cond ((= a 4 ) 6 )
33
+ ((= b 4 ) (+ 6 7 a))
34
+ (else 25 ))
35
+ ; 16
36
+
37
+ (+ 2 (if (> b a) b a))
38
+ ; 6
39
+
40
+ ( * (cond ((> a b) a)
41
+ ((< a b) b)
42
+ (else -1))
43
+ (+ a 1 ))
44
+ ; 16
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+ (/ (+ 5
3
+ 4
4
+ (- 2
5
+ (- 3
6
+ (+ 6
7
+ (/ 4
8
+ 5 )))))
9
+ (* 3
10
+ (- 6 2 )
11
+ (- 2 7 )))
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+ (define (square x ) (* x x))
3
+
4
+ (define (square-sum x y )
5
+ (+ (square x) (square y)))
6
+
7
+ (define (square-2lg3 x y z )
8
+ (cond ((and (< x y) (< x z)) (square-sum y z))
9
+ ((< y z) (square-sum x z))
10
+ (else (square-sum x y))))
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+ (define (a-plus-abs-b a b )
3
+ ((if (> b 0 ) + -) a b))
4
+
5
+ ; The application will invert the signal of the operation based if the b
6
+ ; argument is positive of negative, so the returned number will always be absolute.
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+ (define (p ) (p))
3
+ (define (test x y )
4
+ (if (> x 0 ) 0 y))
5
+
6
+ (test 0 (p))
7
+
8
+ ; applicative-order
9
+ ; (if (= 0 0) 0 (p (p (p (p (... and so on))))))
10
+ ; infinite recursion
11
+ ;
12
+ ; normal-order
13
+ ; (if (= 0 0) 0 (p))
14
+ ; (if (true) 0 (p))
15
+ ; 0
16
+
17
+ ; This environment uses applicative-order evaluation, trying to expand
18
+ ; everything and resulting in a infinite recursion. In normal-order evaluation
19
+ ; the expression evaluates first then expand.
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+
3
+ ; if uses a normal order evaluation to check values, the Evan's new if
4
+ ; uses applicative order witch infinitely expands sqrt-iter.
5
+
6
+ (define (new-if predicate then-clause else-clause )
7
+ (cond (predicate then-clause)
8
+ (else else-clause)))
9
+
10
+ (define (sqrt-iter guess x )
11
+ (new-if (good-enough? guess x)
12
+ guess
13
+ (sqrt-iter (improve guess x) x)))
14
+
15
+ (define (improve guess x )
16
+ (average guess (/ x guess)))
17
+
18
+ (define (average x y )
19
+ (/ (+ x y) 2 ))
20
+
21
+ (define (good-enough? guess x )
22
+ (< (abs (- (square guess) x)) 0.001 ))
23
+
24
+ (define (sqrt x )
25
+ (sqrt-iter 1.0 x))
26
+
27
+ (define (square x ) (* x x))
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+
3
+ (define (sqrt-iter guess oldguess x )
4
+ (if (good-enough? guess oldguess x)
5
+ guess
6
+ (sqrt-iter (improve guess x) guess x)))
7
+
8
+ (define (improve guess x )
9
+ (average guess (/ x guess)))
10
+
11
+ (define (average x y )
12
+ (/ (+ x y) 2 ))
13
+
14
+ (define (good-enough? guess oldguess x )
15
+ (< (abs (- oldguess guess)) 0.001 ))
16
+
17
+ (define (sqrt x )
18
+ (sqrt-iter 1.0 2.0 x))
19
+
20
+ (define (square x ) (* x x))
Original file line number Diff line number Diff line change
1
+ #lang sicp
2
+
3
+ (define (cbrt-iter guess x )
4
+ (if (good-enough? guess x)
5
+ guess
6
+ (cbrt-iter (improve guess x) x)))
7
+
8
+ (define (improve guess x )
9
+ (/ (+ (/ x (square guess))
10
+ (* 2 guess) )
11
+ 3 ))
12
+
13
+ (define (good-enough? guess x )
14
+ (< (abs (- (cube guess) x)) 0.001 ))
15
+
16
+ (define (cbrt x )
17
+ (cbrt-iter 1.0 x))
18
+
19
+ (define (square x ) (* x x))
20
+ (define (cube x ) (* x x x))
You can’t perform that action at this time.
0 commit comments