prolog无限循环未知

时间:2016-03-22 18:49:33

标签: prolog infinite-loop

这是一个程序,它接收一个坐标列表ex:[e(1,2,3),e(4,5,6)]。制定动态,划分和征服,将列表分成两半 半递归, 当你有一个转换值的元组中的坐标。 问题是它处于无限循环中。

resuelve([X],D):-tuples([X],D).   
resuelve(Xs,D):-
     length(Xs,L), %length of the list
     divideIzq(Xs,L,Xa), %divide the left half
     divideDer(Xs,L,Xb), %divide the rigth half
     resuelve(Xa,D1), %left recursively
     resuelve(Xb,D2), %rigth recursively
     append_(D1,D2,D).    %concatenate tuples result

%function that transforms the coordinates
tuples([e(X,Y,Z)],D) :-  D = [c(X,Z),c(Y,0)].

divideIzq(Xs,L1,D):-
    L is div(L1, 2),
    take(L,Xs,D).


divideDer(Xs,L1,D):-  
    L is div(L1, 2),
    drop(L,Xs,D).

drop(0,LastElements,LastElements) :- !.
drop(N,[_|Tail],LastElements) :-
  N > 0,
  N1 is N  - 1,
  drop(N1,Tail,LastElements).


take(0, _, []) :- !.
take(N, [H|TA], [H|TB]) :-
  N > 0,
  N2 is N - 1,
  take(N2, TA, TB).

append_([], Cs, Cs).
append_([A|As],Bs,[A|Cs]):-
          append_(As, Bs, Cs).

示例:

% resuelve([e(3,6,5),e(4,9,3),e(8,11,2),e(10,12,4)], D).
% D = [c(3, 5), c(6, 0), c(4, 3), c(9, 0), c(8, 2), c(11, 0), c(10, 4), c(12, 0)] 

由于

1 个答案:

答案 0 :(得分:0)

就绪!

问题出在第一行:

resuelve([X],D):-tuples([X],D) , !. 

是必要的砍树。

感谢您的帮助!