Skip to content

Instantly share code, notes, and snippets.

@DonaldKellett
Created December 22, 2018 05:16
Show Gist options
  • Save DonaldKellett/ff6344d57bd66596d3d2769b4a91e711 to your computer and use it in GitHub Desktop.
Save DonaldKellett/ff6344d57bd66596d3d2769b4a91e711 to your computer and use it in GitHub Desktop.
Learn Prolog Now! - Chapter 3 - Exercise 3.2 - Matryoshka Dolls
% The doll katarina contains the doll olga, which contains the doll natasha, which contains the doll irina
directlyIn(irina, natasha).
directlyIn(natasha, olga).
directlyIn(olga, katarina).
% in(X, Y) represents the statement "Doll X is contained within doll Y"
% Note that this order is the reverse of the exercise description in Learn Prolog now! (where in(X, Y) states "Doll X contains doll Y" instead) but I thought it made more sense this way.
% Doll X is in doll Y if X is directly in Y
in(X, Y) :- directlyIn(X, Y).
% Doll X is in doll Y if X is directly in some doll Z such that Z is in Y
in(X, Y) :- directlyIn(X, Z), in(Z, Y).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment