;;; Helper function: "apply binary operator" (define (abo truth-table x y) (cond [(and (eq? x 'L) (eq? y 'L)) (list-ref truth-table 0)] [(and (eq? x 'L) (eq? y 'H)) (list-ref truth-table 1)] [(and (eq? x 'H) (eq? y 'L)) (list-ref truth-table 2)] [(and (eq? x 'H) (eq? y 'H)) (list-ref truth-table 3)])) ;;; An implementation of abstract syntax in terms of concrete syntax (define (make-variable n) n) (define variable? number?) (define (variable-num a) a) (define (make-binary left op right) (list left op right)) (define (binary? a) (and (list? a) (= (length a) 3))) (define (binary-left a) (list-ref a 0)) (define (binary-op a) (list-ref a 1)) (define (binary-right a) (list-ref a 2)) (define (make-negate a) (list 'not a)) (define (negate? a) (and (list? a) (= (length a) 2) (eq? (list-ref a 0) 'not))) (define (negate-sub a) (list-ref a 1)) ;;; An interpreter of logical formulas that operates through abstract syntax (define (interp-op op) (cond [(eq? op 'and) '(L L L H)] [(eq? op 'or ) '(L H H H)])) (define (interp a b) (cond [(variable? a) (list-ref b (variable-num a))] [(binary? a) (abo (interp-op (binary-op a)) (interp (binary-left a) b) (interp (binary-right a) b))] [(negate? a) (if (eq? (interp (negate-sub a) b) 'H) 'L 'H)]))