2 (require racket/control)
4 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
5 ;; solutions to the "abSdS" etc task ;;
6 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
10 (define (saved z) (car z))
11 (define (nextchar z) (cadr z))
12 (define (rest z) (cddr z))
15 [(null? (cdr z)) (reverse (saved z))]
16 [(eqv? #\S (nextchar z)) (tz1 (pair (append (saved z) (saved z)) (rest z)))]
17 [else (tz1 (pair (cons (nextchar z) (saved z)) (rest z)))]))
19 ; using explicit continuations
22 [(null? l) (reverse (k '()))]
23 [(eqv? #\S (car l)) (tc1 (cdr l) (compose k k))]
24 [else (tc1 (cdr l) (lambda (tail) (cons (car l) (k tail))))]))
26 ; using implicit continuations (reset/shift)
30 [(null? l) (reverse (k '()))]
31 [(eqv? #\S (car l)) ((compose k k) (tr1 (cdr l)))]
32 [else ((lambda (tail) (cons (car l) (k tail))) (tr1 (cdr l)))])))
34 ; wrapper functions, there's a (test) function at the end
37 (list->string (tz1 (cons '() (string->list s)))))
40 (list->string (tc1 (string->list s) identity)))
43 (list->string (reset (tr1 (string->list s)))))
45 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46 ;; here are variants that only repeat from S back to the most recent # ;;
47 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
51 (define (saved z) (car z))
52 (define (nextchar z) (cadr z))
53 (define (rest z) (cddr z))
54 (define (pair x y) (cons x y))
56 [(null? (cdr z)) (reverse (saved z))]
57 [(eqv? #\# (nextchar z)) (append (reverse (saved z)) (tz3 (pair '() (rest z))))]
58 [(eqv? #\S (nextchar z)) (tz3 (pair (append (saved z) (saved z)) (rest z)))]
59 [else (tz3 (pair (cons (nextchar z) (saved z)) (rest z)))]))
61 ; using explicit continuations
62 ; there are several working solutions
63 ; but it's a bit tricky to get the reverses in the right place, and the order of appending right
66 [(null? l) (reverse (k '()))]
67 [(eqv? #\# (car l)) (append (reverse (k '())) (tc3 (cdr l) identity))]
68 [(eqv? #\S (car l)) (tc3 (cdr l) (compose k k))]
69 [else (tc3 (cdr l) (lambda (tail) (cons (car l) (k tail))))]))
71 ; using implicit continuations (reset/shift)
75 [(null? l) (reverse (k '()))]
76 [(eqv? #\# (car l)) (append (reverse (k '())) (reset (tr3 (cdr l))))]
77 [(eqv? #\S (car l)) ((compose k k) (tr3 (cdr l)))]
78 [else ((lambda (tail) (cons (car l) (k tail))) (tr3 (cdr l)))])))
81 (list->string (tz3 (cons '() (string->list s)))))
84 (list->string (tc3 (string->list s) identity)))
87 (list->string (reset (tr3 (string->list s)))))
89 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
92 (define (cmp t1 t2 inp)
93 (equal? (t1 inp) (t2 inp)))
95 (equal? (tz2 "abSd") "ababd")
98 (equal? (tz2 "aSbS") "aabaab")
101 (equal? (tz4 "ab#ceSfSd") "abcecefcecefd")
102 (cmp tc4 tz4 "ab#ceSfSd")
103 (cmp tr4 tz4 "ab#ceSfSd")
104 (equal? (tz4 "ab#ceS#fSd") "abceceffd")
105 (cmp tc4 tz4 "ab#ceS#fSd")
106 (cmp tr4 tz4 "ab#ceS#fSd")