Skip to content

Instantly share code, notes, and snippets.

@mjg123
Created September 11, 2011 21:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mjg123/1210177 to your computer and use it in GitHub Desktop.
Save mjg123/1210177 to your computer and use it in GitHub Desktop.
(defn bool-combinations [vs]
(loop [vs vs
a '({})]
(if (empty? vs)
a
(recur
(rest vs)
(flatten
(map
#(list
(assoc % (first vs) true)
(assoc % (first vs) false))
a))))))
(defmacro pr-truth-table* [vs pred]
`(for [{:syms ~vs} (bool-combinations '~vs)]
(println (apply str (interpose "\t" ~vs)) "\t" ~pred)))
(defn variables-in [exp]
(-> (filter #(= (name %) (.toUpperCase (name %))) (flatten exp)) distinct sort))
(defmacro pr-truth-table [pred]
`(let [vs# (variables-in '~pred)]
(println (apply str (interpose "\t" vs#)) "\t" '~pred)
(pr-truth-table* vs# ~pred)))
(pr-truth-table (and A (not B)))
@thaiviet1994
Copy link

Creating a “truth table” is not hard, you can use an useful tool (CKod, at http://ckod.sourceforge.net/_/) to make a “truth table”.

  1. CKod homepage: http://ckod.sourceforge.net/
  2. CKod online: http://ckod.sourceforge.net/_/
  3. CKod forum: http://ckod.sourceforge.net/~/

Good luck to you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment