Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 23, 2018 11:22
Show Gist options
  • Save DonaldKellett/9d4b43b682044173f392aac3722a68a8 to your computer and use it in GitHub Desktop.
Save DonaldKellett/9d4b43b682044173f392aac3722a68a8 to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 6 - Exercise 6.6 - Who owns the zebra? (Simplified version of Einstein's Riddle)
% Learn Prolog Now! - Chapter 6 - Exercise 6.6 - Who owns the zebra?
% A simplified version of Einstein's Riddle
% Problem Statement:
% There is a street with three neighboring houses that all have a different color, 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? zebra/1 should tell us who this person is.
% Formulation of the problem statement via the predicate zebra/1
% Heavily inspired by this answer from StackOverflow (to the actual Einstein's Riddle): https://stackoverflow.com/questions/318888/solving-who-owns-the-zebra-programmatically/8270393#8270393
% Correctness of solution not guaranteed
zebra(X) :- % X owns the zebra if ...
Houses = [_, _, _], % There are 3 neighboring houses, where each house will be represented by house(Color, Nationality, Pet) in the following statements
member(house(red, english, _), Houses), % The Englishman lives in the red house
member(house(_, spanish, jaguar), Houses), % The jaguar is the pet of the Spanish family
nextto(house(_, _, snail), house(_, japanese, _), Houses), % The Japanese lives to the right of the snail keeper
nextto(house(_, _, snail), house(blue, _, _), Houses), % The snail keeper lives to the left of the blue house
member(house(_, X, zebra), Houses). % Who (X) keeps the zebra?
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment