[Haskell-cafe] Newbie: generating a truth table

Ketil Malde Ketil.Malde at bccs.uib.no
Tue Feb 6 04:05:52 EST 2007

phiroc at free.fr wrote:
> I would like to create a Haskell function that generates a truth table, for all
> Boolean values, say, using the following "and" function :
> and :: Bool -> Bool -> Bool
> and a b = a && b
What is the type of the resulting table?
> I have tried creating a second function called "loop", which repeatedly calls
> "and", but it did not work, because, for some reason unknown to me, "do" does
> not like repeated function calls
> loop = do
> 	and True True
> 	and True False
I'm not sure I understand what you expected here.  The 'do' syntax
is for monadic code.
> Is there a better way to repeatedly call "and"?
If you want your table in list for, I'd suggest using a list comprehension.

Here's how you'd calculate squares of numbers, for instance:

   squares = [ x^2 | x <- [1..5] ]
> Furthermore, is there a way in Haskell to loop through the Boolean values (True
> and False)
Since there are only two values, you can just feed a list comprehension
with [True,False].
> Last but not least, in the "loop" function above, assuming that there is a way
> to repeatedly call the "and" function, how could you intersperse "printStr"s
> between the "and" calls?
I would't - keep the the calculation and the output separate instead.


More information about the Haskell-Cafe mailing list