Skip to content

Instantly share code, notes, and snippets.

@wlyecn
Created December 2, 2013 13:39
Show Gist options
  • Save wlyecn/7749574 to your computer and use it in GitHub Desktop.
Save wlyecn/7749574 to your computer and use it in GitHub Desktop.
// prolog hanoi
move(1,X,Y,\_):-write('Move top disk from '),write(X),write(' to '), write(Y), nl.
move(N,X,Y,Z):-N>1,M is N-1,move(M,X,Z,Y),move(1,X,Y,\_),move(M,Z,Y,X).
/* 事实 */
male(baba).
male(yeye).
female(nainai).
female(mama).
male(wo).
father(yeye,baba).
father(baba,wo).
mother(nainai,baba).
mother(mama,wo).
/* 规则 */
grandfather(X,Y):-father(X,Z),father(Z,Y).
grandmother(X,Y):-mother(X,Z),father(Z,Y).
daughter(X,Y):-parent(X,Y),female(Y).
son(X,Y):-parent(Y,X),male(X).
parent(X,Y):-father(X,Y);mother(X,Y).
ancestor(X,Y):-parent(X,Y).
ancestor(X,Y):-parent(X,Z),ancestor(Z,Y).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment