[Haskell-cafe] Newbie: generating a truth table
phiroc at free.fr
phiroc at free.fr
Tue Feb 6 05:18:21 EST 2007
Ketil,
thanks for you help.
Here's the code:
and2 :: Bool -> Bool -> Bool
and2 a b = a && b
loop = [ and2 x y | x <- [True,False], y <- [True,False] ]
Now, how do I have Haskell print
printStrLn("True and True = ") + <result of calling and2 True True>
printStrLn("True and False = ") + <result of calling and2 True False>
...
Thanks.
phiroc
Quoting Ketil Malde <Ketil.Malde at bccs.uib.no>:
> 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.
>
> -k
>
More information about the Haskell-Cafe
mailing list