[Haskell-cafe] Newbie: generating a truth table

phiroc at free.fr phiroc at free.fr
Tue Feb 6 05:03:35 EST 2007


Hello,

how would you convert Boolean triples to strings, in the IO function?

printStrings :: (Bool,Bool,Bool) -> IO ()


phiroc


Quoting Lennart Augustsson <lennart at augustsson.net>:

> It would be better to produce a triple (as was suggested)
>
> loop = [(x, y, x&&y) | x <- [True, False], y <- [True, False]]
>
> The you can map a function that prints over that.  Or even better
> map a function that generates the strings, and the use one big
> putStr at the end.  Always try to separate IO from computation.
>
> 	-- Lennart
>
> On Feb 6, 2007, at 10:18 , phiroc at free.fr wrote:
>
> > 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
> >>
> >
> >
> > _______________________________________________
> > Haskell-Cafe mailing list
> > Haskell-Cafe at haskell.org
> > http://www.haskell.org/mailman/listinfo/haskell-cafe
>
>




More information about the Haskell-Cafe mailing list