[Haskell-cafe] Problem with question 3 about knights and knaves on
wikipedia
Peter Verswyvelen
bf3 at telenet.be
Thu Aug 9 14:07:02 EDT 2007
I was writing some haskell code for fun to solve some "knights and knaves"
problems, and I have troubles with
http://en.wikipedia.org/wiki/Knights_and_knaves#Question_3
So knights always tell the truth and knaves always lie. John and Bill are
two persons, but you don't know what they are, and you have to find out.
Question 3
You: Are you both knights?
John: answers either Yes or No, but you don't have enough
information to solve the problem.
You: Are you both knaves?
John: answers either Yes or No, and you can now solve the problem.
My haskell code gave the following result:
--(what is john, what is bill, first answer from john, second
answer from john)
("John is a knight","Bill is a knight","Yes","No ")
("John is a knight","Bill is a knave ","No ","No ")
("John is a knave ","Bill is a knight","Yes","Yes")
("John is a knave ","Bill is a knave ","Yes","No ")
Anyone has an idea what I missed here?
Thanks,
Peter
PS:
The (quick and dirty) haskell code I wrote for this is:
infixl 1 <=>
infixl 1 ==>
-- Equivalence
x <=> y = (x == y)
-- Implication
True ==> False = False
_ ==> _ = True
-- Knights tell the truth
name `isa` True = name ++ " is a knight"
-- Knaves always lie
name `isa` False = name ++ " is a knave "
answer True = "Yes"
answer False = "No "
--Logician: Are you both knights?
--John answers either Yes or No, but the Logician does not have enough
information to solve the problem.
--Logician: Are you both knaves?
--John answers either Yes or No, and the Logician can now solve the problem.
condition j b answer1 answer2 =
(j <=> ((b && j) == answer1)) &&
(j <=> ((not b && not j) == answer2))
solution = [("John" `isa` j, "Bill" `isa` b, answer answer1, answer answer2)
|
j <- [True,False],
b <- [True, False],
answer1 <- [True, False],
answer2 <- [True, False],
condition j b answer1 answer2 ]
main = mapM_ putStrLn (map show solution)
More information about the Haskell-Cafe
mailing list