Convert statements into prolog code
Hey I 'm trying to convert the following statements into prolog code, but I'm not sure if I did it correctly.
1-everybody who respects himself is respected by others:
开发者_StackOverflow中文版 respects(x,respects(x)) :- respects(y,x)).
2-john respects herself:
respectsherself(john).
respects(john,respectsherself(john)).
Thanks
In prolog variables must start with a capital letter so look out for that.
Everybody who respects himself is respected by others. I think you need some basic facts such as who respects who. Then you can declare a rule that says X is respected by others is implied by X respecting Himself.
respects(john, mary). %john respects mary
respects(john, john). %john respects himself
respects(X, Y) :- respectedbyothers(Y). %X respects Y if Y is respected by others
respectedbyothers(X):-respects(X, X).
An optimization: %respects(A,B) means A is respected by B respects(john,john). respects(X,_):-respects(X,X). ?
Don't you just love prolog :)
精彩评论