[Haskell-cafe] Newbie: generating a truth table
Philippe de Rochambeau
phiroc at free.fr
Tue Feb 6 17:08:23 EST 2007
Hello,
the following code
------------------------------------------------------------------------
-------
import Stdm -- From "Discrete Mathematics Using a Computer", by
Cordelia Hall and John O'Donnell.
pImpliesQAndR :: [Bool] -> Bool
pImpliesQAndR (p:q:r:[]) = p ==> ( q /\ r )
pImpliesQAndQImpliesR :: [Bool] -> Bool
pImpliesQAndQImpliesR (p:q:r:[]) = ( p ==> q ) /\ ( q ==> r )
pImpliesQAndRLoop = [ ( (p, q, r), pImpliesQAndR [p, q, r] ) | p <-
[True, False], q <- [True, False], r <- [True,False] ]
pImpliesQAndQImpliesRLoop = [ ( (p, q, r), pImpliesQAndQImpliesR [p,
q, r] ) | p <- [True, False], q <- [True, False], r <- [True,False] ]
pImpliesQAndRLoopShow = [ "pImpliesQAndR" ++ show x ++ " = " ++ show
xs | (x,xs) <- pImpliesQAndRLoop ]
pImpliesQAndQImpliesRLoopShow = [ "pImpliesQAndQImpliesR" ++ show x +
+ " = " ++ show xs | (x,xs) <- pImpliesQAndQImpliesRLoop ]
------------------------------------------------------------------------
-------
returns
------------------------------------------------------------------------
-------
["pImpliesQAndR(True,True,True) = True","pImpliesQAndR
(True,True,False) = False","pImpliesQAndR(True,False,True) =
False","pImpliesQAndR(True,False,False) = False","pImpliesQAndR
(False,True,True) = True","pImpliesQAndR(False,True,False) =
True","pImpliesQAndR(False,False,True) = True","pImpliesQAndR
(False,False,False) = True"]
------------------------------------------------------------------------
-------
when I type
------------------------------------------------------------------------
-------
pImpliesQAndRLoopShow
------------------------------------------------------------------------
-------
I still can't figure out how to write a function that loops through
the string items in the list returned by pImpliesQAndRLoopShow and
pImpliesQAndQImpliesRLoopShow, and print them separated by carriage
returns, thus
pImpliesQAndR(True,True,True) = True
pImpliesQAndR(True,True,False) = False
....
I have tried
------------------------------------------------------------------------
-------
map (putStrLn) pImpliesQAndRLoopShow
------------------------------------------------------------------------
-------
but that results in the following error message:
------------------------------------------------------------------------
-------
<interactive>:1:0:
No instance for (Show (IO ()))
arising from use of `print' at <interactive>:1:0-33
Possible fix: add an instance declaration for (Show (IO ()))
In the expression: print it
In a 'do' expression: print it
*Main>
------------------------------------------------------------------------
-------
Any help would be appreciated.
Cheers,
phiroc
On 6 févr. 07, at 10:32, Ketil Malde wrote:
> 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>
>>
> Well, if you keep x and y in the table as well, either as a triple
> (x, y, and2 x y) or a nested tuple ((x,y), and2 x y) (which would
> let you use the lookup function), you can define a function that
> converts such a tuple to the approprate string. Keep in mind
> that Bool has a Show instance, so that e.g. show True => "True",
> and use ++ to join two strings.
>
> Then, you can 'map' this function on the table to get a list of
> strings
> corresponding to table rows, and use 'concat' or 'unlines'. When you
> have a result you're happy with, you feed it to putStrLn.
>
> -k
>
More information about the Haskell-Cafe
mailing list