August 19, 2024

P46 - Truth tables for logical expressions

Define functions and, or, nand, nor, xor, impl and equ (for logical equivalence) which return the result of the respective operation on boolean values.

A logical expression in two variables can then be written in prefix notation, as in the following example: (and (or A B) (nand A B)).

Write a function table which prints the truth table of a given logical expression in two variables.

Example:* (table 'A 'B '(and A (or A B))).true true truetrue nil truenil true nilnil nil nil

lisp

;;; Define the logical functions that are not in clisp:
;;; impl e equ

(defun impl (a b)
(or a (not b))
)

(defun equ (a b)
(not (xor a b))
)

;;; Now table, which builds a table of boolean functions
;;; the parameters A and A are the variables, and E is the boolean expression

(defun table (a b e)
(dolist (va (list t nil))
(dolist (vb (list t nil))
(format t "~S ~S ~S~%"
va vb
(eval (subst va a (subst vb b e)))
)
)
)
)
Be first to comment
Leave a reply