Prolog. If Prolog found first solution then it stops finding -


task is: determine numbers a,b,c divider of n. example: (a=2,b=3,c=4,n=2 answer a,c).

so, wrote following code:

predicates     a(integer,integer)     m(integer,integer,integer,integer) clauses     a(x,n):-         0 = x mod n,         write(x).      m(a,b,c,n):-         a(a,n).     m(a,b,c,n):-         a(b,n).     m(a,b,c,n):-         a(c,n). goal     m(2,3,4,2). 

but if prolog found first solution (in case a=2) stops , displays it. but, in case answer a=2, c=4. question "how prolog can check predicates"?

prolog begins backtracking when call fails. in situations, may necessary initialize performance of backtracking find other solutions. prolog has special predicate fail, calls unsuccessful finish, and, therefore, initializes refund.

so, rewrote code:

predicates     a(integer,integer)     m(integer,integer,integer,integer) clauses     a(x,n):-         0 = x mod n,         write(x),nl.      m(a,b,c,n):-         a(a,n),fail.     m(a,b,c,n):-         a(b,n),fail.     m(a,b,c,n):-         a(c,n). goal     m(2,3,4,2). 

Comments

Popular posts from this blog

node.js - Mongoose: Cast to ObjectId failed for value on newly created object after setting the value -

gradle error "Cannot convert the provided notation to a File or URI" -

python - NameError: name 'subprocess' is not defined -