Convert statements to prolog code

Hey. I'm trying to convert the following statements into a prolog, but I'm not sure if I did this correctly.

1 - everyone who respects himself is respected by others:

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

2-john respects himself:

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

thank

+3
source share
2 answers

In prologs, variables should start with a capital letter, so pay attention to this.

Everyone who respects himself is respected by others. I think you need some basic facts, for example, who respects them. Then you can declare a rule that says that X is respected by others, implied by 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).
+1
source

Optimization:% (A, B) means A respected B respects (John, John). respects (X, _): - respects (X, X).

Don't you just love prologue :)

0
source

All Articles