-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutil.rkt
30 lines (25 loc) · 1.11 KB
/
util.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
#lang racket
(require racket/contract)
(require txexpr)
(module+ test
(require rackunit))
(define/contract (merge-successive-strings elements)
(txexpr-elements? . -> . txexpr-elements?)
(define (conditional-merge element current-list)
(if (and (not (empty? current-list))
(string? (first current-list))
(string? element))
(cons (string-append (first current-list) element) (drop current-list 1))
(cons element current-list)))
(reverse (foldl conditional-merge '() elements)))
(module+ test
(check-equal? (merge-successive-strings '()) '())
(check-equal? (merge-successive-strings '("a")) '("a"))
(check-equal? (merge-successive-strings '(a)) '(a))
(check-equal? (merge-successive-strings '(a "b")) '(a "b"))
(check-equal? (merge-successive-strings '("a" "b")) '("ab"))
(check-equal? (merge-successive-strings '("a" a "b")) '("a" a "b"))
(check-equal? (merge-successive-strings '("a" "b" " " "c")) '("ab c"))
(check-equal? (merge-successive-strings '("a" "b" a " " b "c" c d "e" " f" g)) '("ab" a " " b "c" c d "e f" g))
)
(provide merge-successive-strings)