-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path04_ch04.scm
94 lines (61 loc) · 1.59 KB
/
04_ch04.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
;4.4
(define (sphere-volume r)
(* (/ 4 3) 3.141592654)
(* r r r))
; the first compound expression is ignored, only the second is returned. should be
(define (sphere-volume r)
(* (/ 4 3) 3.141592654
r r r))
(define (next x)
(x + 1)) ; error: (x + 1) should be (+ x 1)
(define (next x)
(+ x 1))
(define (square)
(* x x)) ; error is that no formal argument is listed , should be
(define (square x)
(* x x))
(define (triangle-area triangle)
(* 0.5 base height)) ; error is that the listed formal argument is not used in the body
; also, the body contains 'base' and 'height' that are not listed formal arguments and
; so are undefined
#|(define (sum-of-squares (square x) (square y))
(+ (square x) (square y))) ; error is that square is being used in place of a a formal
; parameter as well as to evaluate an expression
|#
(define (sum-of-squares x y)
(+ (square x) (square y)))
(define (square x)
(* x x))
;4.5
(define (f2c f)
(/ (* 5 (- f 32))
9))
(define (c2f c)
(+ (/ (* 9 c)
5)
32))
(define (fourth x)
(* x x x x))
(define (fourth x)
(square (square x)))
(define (abs-val x)
(sqrt (square x)))
(define (scientific coeff exp)
(* coeff (expt 10 exp)))
#|
log(base a) b = log (base k) b / log (base k) a
so log(base 10) x = log (base e) x / log (base e) 10
|#
(define (sci-exponent x)
(floor (/ (log x)
(log 10))))
(define (sci-coefficient x)
(/ x
(expt 10 (sci-exponent x))))
;4.9
(define (discount price saving)
(* price (- 1 (/ saving 100))))
;4.10
(define (tip price)
(- (ceiling (* price 1.15))
price))