[Haskell-cafe] Newbie: generating a truth table

Dougal Stanton ithika at gmail.com
Tue Feb 6 04:20:14 EST 2007


Quoth phiroc at free.fr, nevermore,
> 
> loop = do
> 	and True True
> 	and True False
> 	and False True
> 	and False False

For this construct to work you'd have to have 'and' as a monadic
function - let's say, for argument's sake, in IO. And because there are
no results being carried around we can surmise that the type signature
is:

> and :: Bool -> Bool -> IO ()

This will help you do the putStr statements too, although I think it can
be done better. For example, you want to create a truth table, and you
want to print the values of this table. These should really be done
separately, so you don't mix IO with pure computation.

You might want to write a function of the type:

> and :: Bool -> Bool -> (Bool, Bool, Bool)

instead, where the two arguments passed in are stored alongside the
result.

> 
> Is there a better way to repeatedly call "and"?
> 
> Furthermore, is there a way in Haskell to loop through the Boolean values (True
> and False) and call "and" each time?
> 

These can be answered in one sweep with list comprehensions. An
expression of the form

    [ f x y | x <- xs, y <- ys ]

will form a list where every value is 'f x y' for all values of x and y
in the two source lists xs and ys. I hope this is of some help. I've
been purposefully vague in case this is a homework question. If not, let
us know and I'm sure people will be more than happy to provide fuller
answers.

Cheers,

D.


More information about the Haskell-Cafe mailing list