August 19, 2024
P47 - Truth tables for logical expressions (2)
Modify problem P46 by accepting expressions written in infix notation, with all parenthesis present. This allows us to write logical expression in a more natural way, as in the example: (A and (A or (not B))).
Example:* (table 'A 'B '(A and (A or (not B)))).true true truetrue nil truenil true nilnil nil nil
lisp
;;; Sets the logical functions that are not in the clisp:
;;; impl and equ
(defun impl (a b)
(or a (not b))
)
(defun equ (a b)
(not (xor to b))
)
;;; now table, which builds table of Boolean functions
;;; parameters A and A are the variables, and E is the Boolean expression
;;; this one is for infix expressions.
(defun table-in (a b e)
(dolist (va (list t nil))
(dolist (vb (list t nil))
(format t "~S ~S ~S~%"
va vb
(bool-infix-eval a va b vb e)
)
)
)
)
;;; This function evaluates an infix expression E in A and B
;;; giving them VA and VB values
(defun bool-infix-eval (a va b vb e)
(cond
((eql and a) va)
((eql and b) vb)
((not (consp e)) 'invalidates)
((eql (car e) 'not) (not (bool-infix-eval a va b vb (cadr e)))
((eql (cadr e) 'and) (and (bool-infix-eval a va b vb (car e))
(bool-infix-eval a va b vb (caddr e))
))
((eql (cadr e) 'or) (or (bool-infix-eval a va b vb (car e))
(bool-infix-eval a va b vb (caddr e))
))
((eql (cadr e) 'xor) (xor (bool-infix-eval a va b vb (car e))
(bool-infix-eval a va b vb (caddr e))
))
((eql (cadr e) 'impl) (impl (bool-infix-eval a va b vb (car e))
(bool-infix-eval a va b vb (caddr e))
))
((eql (cadr e) 'equ) (equ (bool-infix-eval a va b vb (car e))
(bool-infix-eval a va b vb (caddr e))
))
(t 'invalidates)
)
)