-
Notifications
You must be signed in to change notification settings - Fork 0
/
ss_procedures.rkt
99 lines (61 loc) · 1.34 KB
/
ss_procedures.rkt
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
#lang simply-scheme
(define (square x)
(* x x))
(square 7)
(+ 10 (square 2))
(square (square 3))
;;;;;;;;;;;;;;;;;;;;;;;
(define (square x)
(* x x))
(square 7)
(+ 10 (square 2))
(square (square 3))
(define (f x)
(+ (* 3 x) 12))
(define (g x)
(* 3 (+ x 4)))
;;;;;;;;;;;;;;;;;;;;;;;
(define (f a b)
(+ (* 3 a) b))
(f 5 8)
(f 8 5)
(/ (+ 17 25) 2)
(/ (+ 14 68) 2)
(define (average a b)
(/ (+ a b) 2))
(average 27 4)
;;;;;;;;;;;;;;;;;;;;;;;
; composability
(define (average a b)
(/ (+ a b) 2))
(average (+ 10 8) (* 3 5))
(average (average 2 3) (average 4 5))
(sqrt (average 143 145))
; the substitution model
(define (square x)
(* x x))
(define (hypotenuse a b)
(sqrt (+ (square a) (square b))))
(hypotenuse 5 12)
; spelled out:
(sqrt (+ (square 5) (square 12)))
(hypotenuse 5 12) ; substitute into HYPOTENUSE body
(sqrt (+ (square 5) (square 12))) ; substitute for (SQUARE 5)
(* 5 5)
25
(sqrt (+ 25 (square 12))) ; substitute for (SQUARE 12)
(* 12 12)
144
(sqrt (+ 25 144))
(+ 25 144) ; combine the results as before
169
(sqrt 169)
13
;;;;;;;;;;;;;;;;;;;;;;;
; pitfalls
(define (sum-of-squares x y)
(+ (square x)
(square y)))
(define (f x)
(+ (* x 3) 10))
;;;;;;;;;;;;;;;;;;;;;;;