-
Notifications
You must be signed in to change notification settings - Fork 0
/
01.scm
88 lines (67 loc) · 1.54 KB
/
01.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
#lang planet neil/sicp
#| 1.1
10
12
8
3
6
nothing
nothing
19
#f
4
16
6
16
=> correct
|#
#|
(/ (+ 5
4
(- 2
(- 3
(+ 6
(/ 4 5)))))
(* 3
(- 6 2)
(- 2 7)))
|#
; 1.3
(define (sum_squares a b)
(+ (* a a)
(* b b)))
(define (sum_bigger_squares a b c)
(cond ((and (<= a b) (<= a c)) (sum_squares b c))
((and (<= b a) (<= b c)) (sum_squares a c))
((and (<= c a) (<= c b)) (sum_squares a b))
(else 'undefined_value)))
;1.4
#| based on the evaluation of the predicate, the if expression
evaluates to the + or the - operator. As the position of this expression
is at the 'beginning' of the parentheses i.e. the first item,
the resulting operator primitive (+ or -) is applied to each of
the remaining arguments
|#
#| 1.5
With applicative-order ('inside-out'), the program will give an
error or enter a permanent loop, because it will attempt to evaluate p
, which evaluates to p
With normal-order ('outside-in'), the expression will return 0, and the
expression for y ( '(p)) will never be evaluated
in drracket, the interpreter gets stuck in a loop, indicating that applicative-order
is being used
|#
(define (square x)
(* x x))
(define (good-enough? guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (good-enough? guess x)
guess
(sqrt-iter (improve guess x) x)))
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (sqrt x)
(sqrt-iter 1.0 x))