GNU Prolog - Recursion Problem (Easy?)

So I have this

edu_less(hs,college).
edu_less(college,masters).
edu_less(masters,phd).

I need to write a function to say that something is less than the other. Predicate

edu_le.

So, if I bet edu_le(hs,phd)., it should return yes. I came up with this.

edu_le(A,B) :- A = B.
edu_le(A,B) :- edu_less(A,B).
edu_le(A,B) :- edu_less(A,C), edu_le(C,B).

I really do not want him to go through everything and return a few answers.

Is it possible to return yes or no if it detects that it is actually less than or equal to the second argument?

So basically, if I use the example again edu_le(hs,phd), because hs is less than college, and college is less than masters, and masters are less than phd, then hs should be less than phd, and he would say yes.

Sorry, really new to the prologue, still trying to figure it out.

+3
source share
4

(!). , . :

edu_le(A,B) :- A = B, !.
edu_le(A,B) :- edu_less(A,B), !.
edu_le(A,B) :- edu_less(A,C), edu_le(C,B).

, . , , .

, , , .

+3

edu_le(A,B) :- A = B.
edu_le(A,B) :- edu_less(A,B).
edu_le(A,B) :- edu_less(A,C), edu_le(C,B).

.

edu_le(A,B) :- A = B.
edu_le(A,B) :- edu_less(A,C), edu_le(C,B).

true, (false) . , .

?- edu_le(hs,X).
X = hs ;
X = college ;
X = masters ;
X = phd ;
false.

:

?- edu_le(hs,X).
X = hs.

, once/1. Prolog , , , .

+3

!/0 , , , :

?- edu_le(X, Y).

/1, :

?- once(edu_le(hs, phd)).
+2

, ร–stman, - , Prolog ? , . , , .

-, edu_le(A,B) :- edu_less(A,B)., larsmans. :

edu_le1(A, A).
edu_le1(A, B) :- edu_less(A, C), edu_le1(C, B).

edu_le, : , (edu_le1 ). , , ; , SWI:

?- edu_le1(hs, hs)
true ;
false.

, , - false, ( ):

edu_le2(A, A) :- !.
edu_le2(A, B) :- edu_less(A, C), edu_le2(C, B).

, false:

?- edu_le2(hs, hs)
true.

?-

, : , , , :

?- edu_le1(hs, B) %same, with more copies, for edu_le
B = hs ;
B = college ;
B = masters ;
B = phd ;
false.

?- edu_le2(hs, B)
B = hs.           %bad!

?-
, : edu_le edu_le1 edu_le(?A, ?B), edu_le2 edu_le2(+A, +B) (. [1] ). , edu_le2 , . , edu_le2 , .. , , . , , . , .

, edu_le1 , , A B . , A B . =, = , . ==:

edu_le3(A, B) :- (A == B -> ! ; true), A = B.
edu_le3(A, B) :- edu_less(A, C), edu_le3(C, B).

, , A B . , , :

?- edu_le3(A, A).
true.

?- edu_le3(A, B).  %note that A and B are not the same term
A = B ;
A = hs,
B = college ;
A = hs,
B = masters ;
A = hs,
B = phd ;
A = college,
B = masters ;
A = college,
B = phd ;
A = masters,
B = phd ;
false.

?-

Prolog .

I do not think that there is a way to eliminate the latter falsewithout introducing too much dependence on edu_lt. This is because we must open up the possibility that there is one more edu_ltto study, in case you decide to enrich it with more fundamental facts later. So, in my opinion, this is the best you can have.

[1] SWI Prolog Reference Manual, Section 4.1.

+1
source

Source: https://habr.com/ru/post/1770040/


All Articles