-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsicp1.scm
115 lines (101 loc) · 2.73 KB
/
sicp1.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
#|
1.1 - The Elements of Programming
|#
; 1.1.2 - Naming and the Environment
(define pi 3.14159)
(define radius 6)
(define circumference (* 2 pi radius))
circumference
; 1.1.4 - Compound Procedures
#|
General form of a procedure definition:
(define (<name> <formal parameters>) <body>)
|#
(define (square x) (* x x))
(square 13)
; 1.1.5 - The Substitution Model for Procedure Application
(define (sum-of-squares x y)
(+ (square x) (square y))
)
; 1.1.6 - Conditional Expressions and Predicates
#|
General form of a case analysis:
(cond (<p1> <e1>)
(<p2> <e2>)
...
(<pn> <en>))
|#
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))
)
)
;OR
(define (abs x)
(cond ((< x 0) (- x))
(else x)
)
)
#|
Exercise 1.3
Define a procedure that takes three numbers as arguments
and returns the sum of the squares of the two larger numbers.
|#
(define (greater-num x y) (if (> x y) x y))
(define (sum-of-larger-squares x y z)
(sum-of-squares
(greater-num x y) ;returns the larger x or y
(if (= (greater-num x y) x) ;if that was x
(greater-num y z) ;then compare y and z
(greater-num x z) ;else compare x and z
)
)
)
; test some cases
(sum-of-larger-squares 6 12 11)
(sum-of-larger-squares 12 11 6)
(sum-of-larger-squares 11 6 12)
(sum-of-larger-squares 6 5 5)
(sum-of-larger-squares 5 5 5)
#|
Exercise 1.5
With applicative-order evaluation, which is what the
interpreter actually uses - "evaluate the arguments
and then apply", we see the power of a special form
(like "and" or "or") that does not evaluate all
subexpressions if they won't change the outcome of
the predicate. In this case, the predicate of the
special form "if" evaluates to True, returning the
consequent without bothering with evaluating the
alternative.
With normal-order evaluation, the interpreter performs
a "fully expand and then reduce" evaluation method.
When the expression is evaluated, the interpreter
will attempt to resolve the definition of p so that
test can be evaluated, but it will be stuck in an
infinite loop.
|#
; 1.1.7 - Example: Square Roots by Newton's Method
(define (average x y)
(/ (+ x y) 2))
(define (improve guess x)
(average guess (/ x guess)))
(define (is-sufficient guess x)
(< (abs (- (square guess) x)) 0.001))
(define (sqrt-iter guess x)
(if (is-sufficient guess x)
guess
(sqrt-iter (improve guess x)
x)))
(define (sqrt x)
(sqrt-iter 1.0 x))
(sqrt 9)
; Exercise 1.6
#|
When Alyssa uses the new-if to compute square roots,
since it doesn't use a special form, then the alternative
will be evaluated, even if the predicate is true.
Need to check this answer - may not be true.
|#
; 1.1.8 - Procedures as Black-Box Abstractions