这个prolog程序的意外行为

时间:2015-04-15 02:39:58

标签: prolog

所以对于hw我需要创建一个在整数列表中找到一个洞的程序。整数总是正数,没有重复。以下代码完美地运行并始终返回包含孔的列表。但它的主要缺陷是它只处理最多9个整数列表。我试图用增量修复这个问题,但没有运气。我总是得到堆栈溢出。我找不到原因。我以为我的所有终端谓词都是正确的。我一直在使用跟踪,但输出只是让我困惑,它继续增加。

  game(Input, Hole) :-
     sort(Input, Sortedinput),
     compareheads(Sortedinput, [1, 2, 3, 4, 5, 6, 7, 8, 9], Hole). 

  compareheads([], IntegerList, []).
  compareheads([], Anotherlist, Output).
  compareheads(Compinput, [N|Restofintegers], [N|Output]) :-
     compareheads(Compinput, Restofintegers, Output).
  compareheads([H|Restofinput], [H|Restofintegers], Output) :-
     compareheads(Restofinput, Restofintegers, Output).

这是不起作用的程序......

 game(Input, Hole) :-
    sort(Input, Sortedinput),
    compareheads(Sortedinput, 1, Hole).

 compareheads([], IntegerList, []).
 compareheads([], Anotherlist, Hole).
 compareheads(List, 100, []).
 compareheads(Compinput, Incr, [Incr|Hole]) :-
    incr(Incr, Next),
    compareheads(Compinput, Next, Hole).
 compareheads([Incre|Restofinput], Incre, Hole) :-
    incr(Incre, Another),
    compareheads(Restofinput, Another, Hole).

 incr(X, Y) :-
    Y is X + 1.

0 个答案:

没有答案