-
Notifications
You must be signed in to change notification settings - Fork 2
/
p46.lisp
49 lines (42 loc) · 1.17 KB
/
p46.lisp
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
38
39
40
41
42
43
44
45
46
47
48
49
;;;; (**) 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 true
;;;; true nil true
;;;; nil true nil
;;;; nil nil nil
(in-package :99-problems)
(defmacro nand (&rest x)
`(not (and ,@x)))
(defmacro nor (&rest x)
`(not (or ,@x)))
(defun xor (a b)
(or (and a (not b)) (and (not a) b)))
(defun impl (a b)
(or a (not b)))
(defun equ (a b)
(not (xor a b)))
(defun table (sym1 sym2 expr)
(loop for a in '(t nil)
do (loop for b in '(t nil)
for bound-expr = (subst a sym1 (subst b sym2 expr))
do (format t "~:[F~;T~] ~:[F~;T~] ~:[F~;T~]~%" a b (eval bound-expr)))))
(define-test table-test
(assert-prints
"T T T
T F T
F T F
F F F"
(table 'A 'B '(and A (or A B)))))