Personal tools

99 questions/Solutions/46

From HaskellWiki

< 99 questions | Solutions
Revision as of 21:25, 13 July 2010 by Wapcaplet (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

(**) Define predicates and/2, or/2, nand/2, nor/2, xor/2, impl/2 and equ/2 (for logical equivalence) which succeed or fail according to the result of their respective operations; e.g. and(A,B) will succeed, if and only if both A and B succeed.

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

Now, write a predicate table/3 which prints the truth table of a given logical expression in two variables.

not' :: Bool -> Bool
not' True  = False
not' False = True
 
and',or',nor',nand',xor',impl',equ' :: Bool -> Bool -> Bool
and' True True = True
and' _    _    = False
 
or' False False = False
or' _     _     = True
 
nor'  a b = not' $ or'  a b
nand' a b = not' $ and' a b
 
xor' True  False = True
xor' False True  = True
xor' _     _     = False
 
impl' a b = (not' a) `or'` b
 
equ' True  True  = True
equ' False False = True
equ' _     _     = False
 
table2 :: (Bool -> Bool -> Bool) -> IO ()
table2 f = mapM_ putStrLn [show a ++ " " ++ show b ++ " " ++ show (f a b)
                                | a <- [True, False], b <- [True, False]]

The implementations of the logic functions are quite verbose and can be shortened in places (like "equ' = (==)").

The table function in Lisp supposedly uses Lisp's symbol handling to substitute variables on the fly in the expression. I chose passing a binary function instead because parsing an expression would be more verbose in haskell than it is in Lisp. Template Haskell could also be used :)