[Haskell-cafe] Haskell function help
Carsten Schultz
carsten at codimi.de
Tue Feb 1 20:26:16 CET 2011
Am 01.02.11 14:05, schrieb Houdini:
>
> that is really.....complicated...hmm
Maybe you find
assign :: (Atom,Bool)->Formula->Formula
assign _ [] = []
assign (a,b) (c:cs)
| (b,a) `elem` c = cs'
| otherwise = filter ((/= a).snd) c : cs'
where cs' = assign (a,b) cs
easier to read. With
example :: Formula
example = [[p,q,r],[n p,q,n r],[p,n q]]
where p = l "P"
q = l "Q"
r = l "R"
l a = (True, a)
n (b,a) = (not b, a)
we get
*Algorithm> example
[[(True,"P"),(True,"Q"),(True,"R")],[(False,"P"),(True,"Q"),(False,"R")],[(True,"P"),(False,"Q")]]
*Algorithm> assign ("P", True) example
[[(True,"Q"),(False,"R")]]
*Algorithm> assign ("P", False) example
[[(True,"Q"),(True,"R")],[(False,"Q")]]
But note that we also get
*Algorithm> assign ("P", False)
[[(True,"P"),(True,"Q"),(True,"R")],[(False,"P"),(True,"Q"),(False,"R")],[(True,"P")]]
[[(True,"Q"),(True,"R")],[]]
So this is not reduced to [[]].
hth C.
More information about the Haskell-Cafe
mailing list