[Haskell-cafe] Newbie: generating a truth table
rherrmann at gmail.com
Wed Feb 21 16:11:37 EST 2007
One possible way to generate the values would be using a generic function
for permutation with repetition, such as:
permuteRep :: [a] -> [b] -> [[(a,b)]]
permuteRep  _ = 
permuteRep (a:) bs = [ [ (a,b) ] | b <- bs ]
permuteRep (a:as) bs = concat [ [ (a,b):p | p <- permuteRep as bs ] | b <-
and then use:
lines = permuteRep ["x","y","z"] [False,True]
In case the variable names can be discarded (or, in this case, not generated
... lazy evaluation rox ;-), then:
map (map snd) lines
This avoids having to provide a "domain" for each variable in the list
comprehension, which could be problematic when dealing with many variables
On 2/21/07, Joe Thornber <joe.thornber at gmail.com> wrote:
> > On 2/10/07, Peter Berry <pwberry at gmail.com> wrote:
> > Prelude> putStrLn $ concatMap (flip (++)"\n") $ map show $ [(x,y,(&&) x
> > |x <- [True,False],y <- [True,False]]
> This can be simplified slightly to:
> Prelude > putStrLn . unlines . map show $ [(x, y, x && y) | x <-
> [True, False], y <- [True, False]]
> - Joe
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
Ricardo Guimarães Herrmann
"Those who do not understand Lisp are doomed to reinvent it, poorly"
"Curried food and curried functions are both acquired tastes"
"If you think good architecture is expensive, try bad architecture"
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Haskell-Cafe