;;; 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)])) ; create a list just like l ; except to replace the nth element of l with e (define (subst l n e) (let ((this (if (null? l) 'L (car l))) (next (if (null? l) '() (cdr l)))) (if (= 0 n) (cons e next) (cons this (subst next (- n 1) e))))) ;;; An implementation of abstract syntax using define-struct in DrScheme ; This line and (make-inspector) below tells DrScheme to ; show a visual representation of the contents of structures (print-struct #t) ; The line below defines: make-variable variable? variable-num (define-struct variable (num) (make-inspector)) ; The line below defines: make-binary binary? binary-left binary-op binary-right (define-struct binary (left op right) (make-inspector)) ; The line below defines: make-negate negate? negate-sub (define-struct negate (sub) (make-inspector)) ; The line below defines: make-split split? split-num split-val split-expr (define-struct split (num val expr) (make-inspector)) ;;; 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)] ;;; YOUR CODE HERE )) ; an implementation of exclusive-or ; using split (define testex (make-split 2 (make-negate (make-binary (make-variable 0) 'and (make-variable 1))) (make-binary (make-binary (make-variable 0) 'and (make-variable 2)) 'or (make-binary (make-variable 1) 'and (make-variable 2)))))