# Difference between revisions of "99 questions/Solutions/48"

From HaskellWiki

< 99 questions | Solutions

(to make output look better) |
|||

Line 17: | Line 17: | ||

tablen n f = mapM_ putStrLn [toStr a ++ " => " ++ show (f a) | a <- args n] | tablen n f = mapM_ putStrLn [toStr a ++ " => " ++ show (f a) | a <- args n] | ||

where args n = replicateM n [True, False] | where args n = replicateM n [True, False] | ||

− | toStr = unwords . map show | + | toStr = unwords . map (\x -> show x ++ space x) |

+ | space True = " " | ||

+ | space False = " " | ||

</haskell> | </haskell> |

## Revision as of 13:21, 14 January 2012

(**) Truth tables for logical expressions (3).

Generalize problem P47 in such a way that the logical expression may contain any number of logical variables. Define table/2 in a way that table(List,Expr) prints the truth table for the expression Expr, which contains the logical variables enumerated in List.

```
import Control.Monad (replicateM)
-- functions as in solution 46
infixl 4 `or'`
infixl 4 `nor'`
infixl 5 `xor'`
infixl 6 `and'`
infixl 6 `nand'`
infixl 3 `equ'` -- was 7, changing it to 3 got me the same results as in the original question :(
tablen :: Int -> ([Bool] -> Bool) -> IO ()
tablen n f = mapM_ putStrLn [toStr a ++ " => " ++ show (f a) | a <- args n]
where args n = replicateM n [True, False]
toStr = unwords . map (\x -> show x ++ space x)
space True = " "
space False = " "
```