-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathex-165.rkt
58 lines (46 loc) · 1.18 KB
/
ex-165.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
#lang htdp/bsl
; ### Constants
; ### Data Definitions
; ### Functions
; List-of-toys -> List-of-toys
; given a list of one-word strings, it replaces the occurrences
; of "robot" with "r2d2"
(check-member-of (subst-robot '()) '())
(check-member-of
(subst-robot (cons "lol" (cons "troll" (cons "robot" '()))))
(cons "lol" (cons "troll" (cons "r2d2" '())))
)
(define (subst-robot lot)
(cond
[(empty? lot) '()]
[else
(cons
(if
(string=? (first lot) "robot")
"r2d2"
(first lot)
)
(subst-robot (rest lot))
)]))
; List-of-strings String String -> List-of-strings
; given a list of strings los, it replaces the occurrences
; of the string s with r
(check-member-of (substitute '() "lol" "troll") '())
(check-member-of
(substitute (cons "lol" (cons "troll" (cons "robot" '()))) "lol" "troll")
(cons "troll" (cons "troll" (cons "robot" '())))
)
(define (substitute lot s r)
(cond
[(empty? lot) '()]
[else
(cons
(if
(string=? (first lot) s)
r
(first lot)
)
(substitute (rest lot) s r)
)]))
(require test-engine/racket-tests)
(test)