The criminal is one of A, B, C and D.
A says: "It's not me"
B says: "It's D"
C says: "It's B"
D says: "It's not me"
And we know that only one of them tells the truth.
Who is the one? I want to solve it by using Prolog.
It's an interview question.
Disclaimer: This is Xonix' solution. If you like it vote him up. But as it took me some head-scratching to figure out what was going on, I thought I might just as well offer my comments so others could benefit.
At first, here is his solution as a proper clause:
And it goes like this:
At first, he runs through the list of individuals (have to be lower case, so they're not variables).
K
is instantiated to each of them in turn.With each possible value of
K
he runs through the rest of the clause.K
can be interpreted as the hypothesis who the criminal is. The next 4 lines are to provide bindings to each of the variables A, B, C and D. You can read them like this: Under the assumption thata
is not the criminal, a is truthful otherwise not. Under the assumption thatd
is the criminal, b is truthful otherwise not. Asf. That is, the variables A, B, ... capture the truthfulness of the corrsponding individual, given a specific criminal.As a known constraint is the fact that only one of them is truthful, the sum of their truth values must be 1. On backtracking, Prolog makes the next binding for K, and runs through it again. Turns out the constraint is only satisfied if
a
is the criminal (andd
is telling the truth, if I'm not mistaken). Cute.I ran across this problem and wanted to give it a shot :
Here is another solution which I find a bit less cryptic than Xonix's. Tested in SWI-Prolog.
Usage example:
One-liner solution
A similar problem and corresponding solution can also be found here:
https://github.com/LogtalkDotOrg/logtalk3/blob/master/examples/puzzles/jam_thief.lgt
Like the solution posted by Kaarel, is possible to request a justification/explanation for the solution found.