-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCharpter12.bak
72 lines (50 loc) · 2.07 KB
/
Charpter12.bak
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
#|【Scheme Basic Functions by Kevin&poi~】|#
;function: ture if input is a vowel, false if not.
;argument: 1 character
(define (vowel? char)
(member? char '(a e i o u)))
;square the number
;argument: 1 number
(define (square num)
(* num num))
;cube the number
;argument: 1 number
(define (cube num)
(* num num num))
;find the second word/character in a string/word
;argument: word/string
(define (second x)
(first (bf x)))
;reverse the order of the word
;argument:word
;——————————————————————————————————————————practice problems————————————————————————————————————————————————————————————————
(define (factorial x)
(if (<= x 1)
1
(* x(factorial (- x 1)))))
(define (fib x)
(if (or (= x 1)(= x 2))
1
(+ (fib(- x 1)) (fib(- x 2)))))
;———————————————————————————————————————————exercise problems———————————————————————————————————————————————————————————————
#|12.1 Here is a definition of a procedure that returns the sum of the numbers in its argument sentence:
(define (addup nums)
(if (empty? (bf nums))
(first nums)
(+ (first nums) (addup (bf nums)))))
Although this works, it could be simplified by changing the base case. Do that.|#
(define (addup nums)
(if (empty? nums)
0
(+ (first nums) (addup (bf nums)))))
#|12.2 Fix the bug in the following definition:
(define (acronym sent) ;; wrong
(if (= (count sent) 1)
(first sent)
(word (first (first sent))
(acronym (bf sent)))))|#
(define (acronym sent) ;; wrong
(if (= (count sent) 1)
(first (first (sent)))
(word (first (first sent))
(acronym (bf sent)))))