forked from schwers-zz/RedRacket
-
Notifications
You must be signed in to change notification settings - Fork 0
/
a-test.rkt
121 lines (105 loc) · 4.87 KB
/
a-test.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
(module a-test racket
(require racket/unsafe/ops)
(define a*-only
(lambda (input)
(letrec ((length (unsafe-string-length input))
(loop
(lambda (i n)
(if (unsafe-fx= i length) #t
(let* ((a (unsafe-fx+ i 1))
(b (char->integer (unsafe-string-ref input a))))
(if (unsafe-fx= n 97) (loop a b) #f))))))
(loop 0 (char->integer (unsafe-string-ref input 0))))))
(define a*-only-2
(lambda (input)
(letrec ((length (unsafe-string-length input))
(loop
(lambda (i)
(if (unsafe-fx= i length) #t
(let ((n (char->integer (unsafe-string-ref input i))))
(if (unsafe-fx= n 97) (loop (unsafe-fx+ i 1)) #f))))))
(loop 0))))
(define a*-only-3
(lambda (input)
(let ((length (unsafe-string-length input)))
(if (unsafe-fx= length 0)
#f
(letrec ((accept (lambda (i) #t))) (accept 0))))))
(define a+paren-edited
(lambda (input)
(let ((length (unsafe-string-length input)))
(if (unsafe-fx= length 0)
#f
(letrec ((state1
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(if (unsafe-fx= n 40) (state2 (unsafe-fx+ i 1)) #f)))))
(state2
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(if (unsafe-fx= n 97) (state3 (unsafe-fx+ i 1)) #f)))))
(state3
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(if (unsafe-fx= n 41)
(state4 (unsafe-fx+ i 1))
(and (unsafe-fx= n 97) (state3 (unsafe-fx+ i 1))))))))
(state4 (lambda (i) #t)))
(state1 0))))))
(define a+paren-compiled
(lambda (input)
(let ((length (unsafe-string-length input)))
(if (unsafe-fx= length 0)
#f
(letrec ((state1
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(and (unsafe-fx= n 40) (state2 (unsafe-fx+ i 1)))))))
(state2
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(and (unsafe-fx= n 97) (state3 (unsafe-fx+ i 1)))))))
(state3
(lambda (i)
(if (unsafe-fx= i length)
#f
(let ((n (char->integer (unsafe-string-ref input i))))
(if (unsafe-fx< n 97)
(and (unsafe-fx= n 41) (state4 (unsafe-fx+ i 1)))
(and (unsafe-fx= n 97) (state3 (unsafe-fx+ i 1))))))))
(state4 (lambda (i) #t)))
(state1 0))))))
(define a+paren-cased
(lambda (input)
(let ((length (unsafe-string-length input)))
(if (unsafe-fx= length 0) #f
(let loop ([state 1][pos 0])
(case state
[(1) (if (unsafe-fx= pos length) #f
(let ((n (char->integer (unsafe-string-ref input pos))))
(case n
[(40) (loop 2 (unsafe-fx+ pos 1))]
[else #f])))]
[(2) (if (unsafe-fx= pos length) #f
(let ((n (char->integer (unsafe-string-ref input pos))))
(case n
[(97) (loop 3 (unsafe-fx+ pos 1))]
[else #f])))]
[(3) (if (unsafe-fx= pos length) #f
(let ((n (char->integer (unsafe-string-ref input pos))))
(case n
[(97) (loop 3 (unsafe-fx+ pos 1))]
[(41) (loop 4 (unsafe-fx+ pos 1))]
[else #f])))]
[(4) #t]))))))
)