Skip to content

Commit 1100e8f

Browse files
committed
SICP: 1.1 through 1.5
1 parent 1da8ddb commit 1100e8f

File tree

1 file changed

+69
-0
lines changed

1 file changed

+69
-0
lines changed

sicp/chap1.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
1.1
2+
```scheme
3+
10
4+
> 10
5+
(+ 5 3 4)
6+
> 12
7+
(- 9 1)
8+
> 8
9+
(/ 6 2)
10+
> 3
11+
(+ (* 2 4) (- 4 6))
12+
> 6
13+
(define a 3)
14+
>
15+
(define b (+ a 1))
16+
>
17+
(+ a b (* a b))
18+
> 19
19+
(= a b)
20+
> #f
21+
(if (and (> b a) (< b (* a b)))
22+
b
23+
a)
24+
> 4
25+
(cond ((= a 4) 6)
26+
((= b 4) (+ 6 7 a))
27+
(else 25))
28+
> 16
29+
(+ 2 (if (> b a) b a))
30+
> 6
31+
(* (cond ((> a b) a)
32+
((< a b) b)
33+
(else -1))
34+
(+ a 1))
35+
> 16
36+
```
37+
38+
1.2
39+
```scheme
40+
(/ (+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
41+
(* 3 (- 6 2) (- 2 7)))
42+
```
43+
44+
1.3
45+
```scheme
46+
(define (sum-squares x y z)
47+
(define (squares p q) (+ (* p p) (* q q)))
48+
(cond ((> x y z) (squares x y))
49+
((> x z y) (squares x z))
50+
((> y z x) (squares y z))
51+
((> y x z) (squares y x))
52+
((> z x y) (squares z x))
53+
(else (squares z y))))
54+
```
55+
56+
1.4
57+
```
58+
This function will change from a+b to a-b if b is less than 0, ensuring b as absolute.
59+
```
60+
61+
1.5
62+
```
63+
In normal order the interpreter will "fully expand and then reduce", using values as needed. so:
64+
(test 0 (p)) fully expands to:
65+
(if (= 0 0) 0 (p))
66+
Since its true, 0 is returned.
67+
68+
On applicative order, however, the interpreter will evaluate the arguments then apply. So it will evaluate (p) before applying test, which will make it loop forever.
69+
```

0 commit comments

Comments
 (0)