-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy path1106-parsing-a-boolean-expression.rkt
37 lines (33 loc) · 1.15 KB
/
1106-parsing-a-boolean-expression.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
#lang racket
(define (eat-single expr)
(let ((c (car expr))
(expr-r (cdr expr)))
(cond ((eq? c #\t) (cons true expr-r))
((eq? c #\f) (cons false expr-r))
((eq? c #\|)
(let ((list-out (eat-list expr-r)))
(cons (ormap identity (car list-out))
(cdr list-out))))
((eq? c #\&)
(let ((list-out (eat-list expr-r)))
(cons (andmap identity (car list-out))
(cdr list-out))))
((eq? c #\!)
(let ((list-out (eat-list expr-r)))
(cons (not (caar list-out))
(cdr list-out)))))))
(define (eat-list expr)
(define (iter result expr-r)
(if (eq? (car expr-r) #\))
(cons result (cdr expr-r))
(let ((single-out (eat-single (cdr expr-r))))
(iter (cons (car single-out) result)
(cdr single-out)))))
(iter null expr))
(define/contract (parse-bool-expr expression)
(-> string? boolean?)
(car (eat-single (string->list expression))))
(parse-bool-expr "|(t,f)")
(parse-bool-expr "|(&(t,f,t),t)")
(parse-bool-expr "!(&(t,f))")
(parse-bool-expr "|(&(t,f,t),!(t))")