-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path1-2_3_5-basic.scm
68 lines (54 loc) · 1.22 KB
/
1-2_3_5-basic.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
;; SICP 1.1.4
(define (square x) (* x x))
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (f a)
(sum-of-squares (+ a 1) (* a 2)))
;; Print the result.
(display (f 5)) (newline)
;; SICP 1.1.6
(define (abs x)
(cond ((> x 0) x)
((= x 0) 0)
((< x 0) (- x))))
(display (abs -5)) (newline)
(define (abs2 x)
(cond ((< x 0) (- x))
(else x)))
(display (abs2 -5)) (newline)
(define (abs3 x)
(if (< x 0)
(- x)
x))
(display (abs3 -5)) (newline)
;; Exercise 1.2
(define r (/
(+ 5 4 (- 2 (- 3 (+ 6 (/ 4 5)))))
(* 3 (- 6 2) (- 2 7))))
(display r) (newline)
;; Exercise 1.3
(define (foo a b c)
(if (> a b)
;; Do we have swap in Scheme?
(if (> b c)
(+ (square a) (square b))
(+ (square a) (square c)))
(if (> a c)
(+ (square b) (square a))
(+ (square b) (square c)))))
;; All should yield 13.
(display (foo 1 2 3)) (newline)
(display (foo 1 3 2)) (newline)
(display (foo 2 1 3)) (newline)
(display (foo 2 3 1)) (newline)
(display (foo 3 2 1)) (newline)
(display (foo 3 1 2)) (newline)
;; Exercise 1.5
(define (p) (p))
(define (test x y)
(if (= x 0)
0
y))
;; Infinite loop in applicative-order evaluation.
;;(test 0 (p))
;; (p) -> (p) -> ...