john
john

Reputation: 41

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:

   respects(x,respects(x)) :- respects(y,x)).

2-john respects herself:

respectsherself(john).
respects(john,respectsherself(john)).

Thanks

Upvotes: 3

Views: 1101

Answers (2)

user741944
user741944

Reputation: 41

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 :)

Upvotes: 0

Vincent Ramdhanie
Vincent Ramdhanie

Reputation: 103135

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).

Upvotes: 1

Related Questions