-
Notifications
You must be signed in to change notification settings - Fork 0
/
Charpter14.rkt
139 lines (118 loc) · 3.53 KB
/
Charpter14.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
#|【Scheme Common Functions by Kevin&poi~ on Github】|#
;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
(define (reverse_order wd)
(accumulate (lambda (x y)(word y x))wd))
#|
14.1
> (remove-once 'morning '(good morning good morning))
(GOOD GOOD MORNING)
(It's okay if your solution removes the other MORNING instead,
as long as it removes only one of them.) |#
;keep patern
(define (remove-once wd sent)
(if (equal? wd (first sent))
(bf sent)
(se (first sent)(remove-once wd (bf sent)))))
#|
14.2
(up 'town)
(T TO TOW TOWN)|#
;accumulate
(define (up wd)
(if (= (count wd) 0) '()
(se (up (bl wd))wd)))
;14.3
(define (remove-once wd sent)
(cond ((empty? sent) sent)
((equal? (first sent) wd) (bf sent))
(else (se (first sent) (remove-once (bf sent))))))
;14.4
(define (odds-helper n sent)
(cond ((empty? sent) sent)
((= (remainder n 2) 0) (odds-helper (+ n 1) (bf sent)))
(else (se (first wd) (odds-helper (+ n 1) (bf sent))))))
(define (odds sent)
(odds-helper 1 sent))
;14.5
(define (letter-count sent)
(if (empty? sent)
0
(+ (count (first sent))
(letter-count (bf sent)))))
;14.6
(define (member? wd sent)
(cond ((empty? sent) #f)
((equal? wd (first sent)) #t)
(else (member? wd (bf sent)))))
;14.7
(define (differences sent)
(if (<= (count sent) 1)
'()
(let ((f (first sent))
(s (bf sent)))
(se (- (first s) f) (differences s)))))
#|14.9 Write a procedure called location that takes two arguments, a word and a sentence. It should return a
number indicating where in the sentence that word can be found. If the word isn't in the sentence, return #f.
If the word appears more than once, return the location of the first appearance.
> (location 'me '(you never give me your money))
4|#
(define (location n wd sent)
(if (empty? sent)
#f
(if (equal? wd (first sent))n
(location ((+ n 1) wd (bf sent))))))
;14.10
(define (cad-helper n sent)
(cond ((<= (count sent) 1) n)
((equal? (first sent) (first (bf sent)))
(cad-helper (+ n 1) (bf sent)))
(else
(cad-helper n (bf sent)))))
(define (count-adjacent-duplicates sent)
(cad-helper 0 sent))
;14.11
(define (remove-adjacent-duplicates sent)
(if (<= (count sent) 1)
sent
(se
(if (equal? (first sent) (first (bf sent)))
'()
(first sent))
(remove-adjacent-duplicates (bf sent)))))
;14.15
(define (mergesort sent)
(if (<= (count sent) 1)
sent
(merge (mergesort (one-half sent))
(mergesort (other-half sent)))))
(define (merge left right)
(cond ((empty? left)right)
((empty? right)left)
((before? (first left)(first right))
(se (first left)(merge (bf left)right)))
(else (se (first right)(merge left(bf right))))))
(define (one-half sent)
(if (<= (count sent)1)
sent
(se(first sent)(one-half (bf(bl sent))))))
(define (other-half sent)
(if (<= (count sent)1)
'()
(se (last (bf sent))(other-half (bf (bl sent))))))