-
Notifications
You must be signed in to change notification settings - Fork 6
/
ex-177.rkt
58 lines (43 loc) · 1.1 KB
/
ex-177.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
#lang htdp/bsl
(define good (explode "good"))
(define all (explode "all"))
(define lla (explode "lla"))
(define-struct editor [pre post])
; An Editor is a structure (make-editor Lo1S Lo1S)
; An Lo1S is one of:
; - '()
; - (cons 1String Lo1S)
; Lo1S -> Lo1S
; Reverses a list of 1String
(check-expect (rev '()) '())
(check-expect (rev good) (explode "doog"))
(define (rev lo1s)
(cond
[(empty? lo1s) '()]
[else
(add-at-end
(rev (rest lo1s))
(first lo1s)
)]))
; Lo1S 1String -> Lo1S
; Adds s to the end of lo1s
(check-expect (add-at-end '() "s") (cons "s" '()))
(check-expect (add-at-end (explode "hi") "s") (explode "his"))
(define (add-at-end lo1s s)
(cond
[(empty? lo1s) (cons s '())]
[else
(cons
(first lo1s)
(add-at-end (rest lo1s) s)
)]))
; String String -> Editor
; Creates an editor from two strings
(check-expect (create-editor "abc" "dfg") (make-editor (explode "cba") (explode "dfg")))
(define (create-editor s1 s2)
(make-editor
(rev (explode s1))
(explode s2)
))
(require test-engine/racket-tests)
(test)