November 23, 2025
My solution is the simplified Zebra problem from this SWI-Prolog tutorial.
Here’s the problem statement:
There is a street with three neighbouring houses that all have a different colour, namely red, blue, and green. People of different nationalities live in the different houses and they all have a different pet. Here are some more facts about them:
- The Englishman lives in the red house.
- The jaguar is the pet of the Spanish family.
- The Japanese lives to the right of the snail keeper.
- The snail keeper lives to the left of the blue house.
Who keeps the zebra? Don’t work it out for yourself: define a predicate zebra/1 that tells you the nationality of the owner of the zebra!
% Define a toLeft predicate
% Succeeds if the list contains the sublist H1,H2 at any location
toLeft(H1,H2,[H1,H2|_]).
toLeft(H1,H2,[_|T]):-
toLeft(H1,H2,T).
zebra(Who):-
% We have a street of length 3
length(Street, 3),
% The street will have an Englishman's red house and a Spaniard with a pet Jaguar
member(house(english, red, _), Street),
member(house(spanish, _, jaguar), Street),
% The Japanese house is to the left of the snail house
% The blue house is to the left of the snail house
toLeft(
house(_,_,snail),
house(japanese,_,_),
Street
),
toLeft(
house(_,_,snail),
house(_,blue,_),
Street
),
% We're looking for who owns the Zebra, so create that rule here
member(house(Who,_,zebra), Street).
« “your system has run out of application memory” on macOS Tahoe | Home | Flatten Nested Lists in Prolog »