|
134 | 134 | (defun princ-n (string n)
|
135 | 135 | "Just print the STRING by N times"
|
136 | 136 | (dotimes (_ n) (princ string)))
|
137 |
| - |
| 137 | +(defun print-bar (spaces) |
| 138 | + (princ "+") |
| 139 | + (princ-n "-" (1- (reduce #'+ spaces))) |
| 140 | + (princ "+") |
| 141 | + (princ #\newline)) |
138 | 142 |
|
139 | 143 | (defun print-truth-table (exp)
|
140 | 144 | "Given a EXP with prefixed notation generate
|
|
147 | 151 | for p = (princ-to-string x)
|
148 | 152 | collect (concatenate 'string " " p " |")))
|
149 | 153 | (spaces (mapcar #'length printable-header)))
|
150 |
| - (princ-n "-" (reduce #'+ spaces)) |
151 |
| - (princ #\newline) |
| 154 | + (print-bar spaces) |
| 155 | + (princ "|") |
152 | 156 | (loop for exp in printable-header do (princ exp))
|
153 | 157 | (princ #\newline)
|
154 |
| - (princ-n "-" (reduce #'+ spaces)) |
155 |
| - (princ #\newline) |
| 158 | + (print-bar spaces) |
156 | 159 | (loop for n-value from 1 below n-values
|
157 |
| - do (progn (loop for n-exp from 0 below (length header) |
158 |
| - do (let* ((space (nth n-exp spaces)) |
159 |
| - (half-space (floor (- space 2) 2)) |
160 |
| - (val (nth n-value (nth n-exp truth-table)))) |
161 |
| - (princ-n " " half-space) |
162 |
| - (princ val) |
163 |
| - (princ-n " " half-space) |
164 |
| - (if (oddp space) |
165 |
| - (princ " |") |
166 |
| - (princ "|")))) |
167 |
| - (princ #\newline))) |
168 |
| - (princ-n "-" (reduce #'+ spaces)) |
169 |
| - (princ #\newline))) |
| 160 | + do (progn |
| 161 | + (princ "|") |
| 162 | + (loop for n-exp from 0 below (length header) |
| 163 | + do (let* ((space (nth n-exp spaces)) |
| 164 | + (half-space (floor (- space 2) 2)) |
| 165 | + (val (nth n-value (nth n-exp truth-table)))) |
| 166 | + (princ-n " " half-space) |
| 167 | + (princ val) |
| 168 | + (princ-n " " half-space) |
| 169 | + (if (oddp space) |
| 170 | + (princ " |") |
| 171 | + (princ "|")))) |
| 172 | + (princ #\newline))) |
| 173 | + (print-bar spaces))) |
170 | 174 |
|
171 | 175 | (defmacro truth (exp)
|
172 | 176 | "A easy way to generate a truth table"
|
173 | 177 | `(print-truth-table (quote ,exp)))
|
174 | 178 |
|
| 179 | +(defmacro truth-infix (exp) |
| 180 | + "A easy and infix way of EXP generate a truth table. |
| 181 | + Ex.: (truth-infix (p ^ q)) " |
| 182 | + `(print-truth-table (infix-to-prefix (quote , exp)))) |
| 183 | + |
175 | 184 | (defun main ()
|
176 | 185 | (truth (=> (v p (~ q)) (=> p q)))
|
177 | 186 | (truth (^ p q))
|
|
0 commit comments