-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path65-valid-number.rkt
65 lines (45 loc) · 1.16 KB
/
65-valid-number.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
#lang racket
(define ((then f g) input cont)
(f input (λ (rst) (g rst cont))))
(define ((alt f g) input cont)
(or (f input cont) (g input cont)))
(define ((opt f) input cont)
(or (f input cont)
(cont input)))
(define ((many f) input cont)
(or ((then f (many f)) input cont)
(cont input)))
(define (many1 f)
(then f (many f)))
(define (end input [cont #t])
(null? input))
(define ((satisfy f) input cont)
(and (pair? input)
(f (car input))
(cont (cdr input))))
(define (is c)
(satisfy (λ (v) (char=? v c))))
(define (is/ci c)
(satisfy (λ (v) (char-ci=? v c))))
; ------------------------------------
(define digit (satisfy char-numeric?))
(define dot (is #\.))
(define sign (alt (is #\+) (is #\-)))
(define integer
(then (opt sign)
(many1 digit)))
(define lead-dot
(then dot (many1 digit)))
(define lead-digit
(then (many1 digit)
(then dot (many digit))))
(define decimal
(then (opt sign)
(alt lead-dot lead-digit)))
(define exponent
(then (is/ci #\e) integer))
(define number
(then (alt integer decimal)
(opt exponent)))
(define (is-number s)
(number (string->list s) end))