28 July 2007

Golfer-Riddle (Prolog-Version)

In my ongoing quest for improved awareness for rule-based systems, I solved the golfer-riddle in Prolog and published the solution together with an explanation here.

It's a pretty straightforward translation of the riddle itself - Prolog rocks!
(I'm open to improvisation suggestions...)


gcolor(blue).
gcolor(plaid).
gcolor(orange).
gcolor(red).

gpos(1).
gpos(2).
gpos(3).
gpos(4).

golfer( [_, C, P] ) :- gcolor(C), gpos(P).

golferRiddle( G1, G2, G3, G4) :- golfer(G1),golfer(G2),golfer(G3), golfer( G4),
G1 = [fred, FredCol, FredPos ],
G2 = [joe, JoeCol, 2],
G3 = [tom, TomCol, TomPos],
TomPos =\= 1, TomPos =\= 4,
TomCol \= orange,
G4 = [bob, plaid, BobPos],

/* unique colors */
is_set([FredCol, JoeCol, TomCol, plaid]),

/* unique positions */
is_set([FredPos, TomPos, BobPos, 2]),

/* Fred's right neighbour wears blue */
plus( FredPos, 1, FP1),
member( [_, blue, FP1], [G1, G2, G3, G4]).