 |
 |
Here is a solution for the "Who owns the zebra?" puzzle in Allegro Prolog:
(<-- (nextto ?x ?y ?list) (iright ?x ?y ?list))
(<- (nextto ?x ?y ?list) (iright ?y ?x ?list))
(<-- (iright ?left ?right (?left ?right . ?rest)))
(<- (iright ?left ?right (?x . ?rest))
(iright ?left ?right ?rest))
(<-- (zebra ?h ?w ?z)
;; Each house is of the form:
;; (house nationality pet cigarette drink house-color)
(= ?h ((house norwegian ? ? ? ?)
?
(house ? ? ? milk ?) ? ?))
(member (house englishman ? ? ? red) ?h)
(member (house spaniard dog ? ? ?) ?h)
(member (house ? ? ? coffee green) ?h)
(member (house ukrainian ? ? tea ?) ?h)
(iright (house ? ? ? ? ivory)
(house ? ? ? ? green) ?h)
(member (house ? snails winston ? ?) ?h)
(member (house ? ? kools ? yellow) ?h)
(nextto (house ? ? chesterfield ? ?)
(house ? fox ? ? ?) ?h)
(nextto (house ? ? kools ? ?)
(house ? horse ? ? ?) ?h)
(member (house ? ? luckystrike oj ?) ?h)
(member (house japanese ? parliaments ? ?) ?h)
(nextto (house norwegian ? ? ? ?)
(house ? ? ? ? blue) ?h)
(member (house ?w ? ? water ?) ?h)
(member (house ?z zebra ? ? ?) ?h)
)
;; This runs the query:
(?- (zebra ?houses ?water-drinker ?zebra-owner))
For comparison, here is how the first few rules above would be expressed in Edinburgh syntax:
nextto(X, Y, List) :- iright(X, Y, List).
nextto(X, Y, List) :- iright(Y, X, List).
iright(Left, Right, [Left, Right | _]).
iright(Left, Right, [_ | Rest]) :- iright(Left, Right, Rest).
|  |
 |