How do you represent truth tables for variables of type a
?
type TruthTable a = [(a, Bool)]
truthTables :: [a] -> [TruthTable a]
What is your truth table for no variables? There is only one: the one that contains no variable assignments.
truthTables [] = [[]]
How do you construct all possible truth tables for variables v:vs
, given the truth tables for vs
? You take every possible truth table for vs
, and every possible assignment for v
, and combine them:
truthTables (v:vs) = [(v, b):others | b <- [True, False], others <- truthTables vs]
We could have also written it using the Monad
instance of []
:
truthTables [] = return []
truthTables (v:vs) = do
this <- [True, False]
them <- truthTables vs
return (this:them)