在 Prolog 中使用增量器跟踪每个递归调用

Keeping track of each recursive call with an incrementer in Prolog

我试图在每次递归调用时将变量 M 递增 1

我一直在努力解决这个问题,但在查询时得到 'arguments not sufficiently instantiated'。我有一个基本情况,我自反地使用两个变量递增以存储旧值并统一新值。

point(a,b).
point(a,c).
point(b,d).
point(c,d).
point(d,e).
point(f,g).

linkup(Point,Point,0).
linkup(Point1,Point2,Count) :-
   Counthelp is Count+1,
   point(Point1,Link),  
   linkup(Link,Point2,Counthelp).

我想这就是你需要的:

linkup(Point1,Point2,0) :- point(Point1,Point2).
linkup(Point1,Point2,Counthelp) :-
    point(Point1,Link),
    linkup(Link,Point2,Count),
    Counthelp is Count+1.

你必须确保你想 "Prologishly"。 Counthelp变量是一个输出,所以你必须把它传出去,不能传进去。它也需要最后计算,这样Count就已经统一了。