0
votes

I have this code and when i try to run it with SWI, nothing is shown. Infinite Loop, maybe. I won't explain what i do with this because i'd have to say many things. just wanna know what's wrong with it. i'm running it this way:

mesmaLinhaEste_58((0,4),(4,4),[(1,3),(1,2),(0,0),(2,0),(3,2),(3,3),(4,0)],[]).

mesmaLinhaEste_58((X,_),(Xx,_),[],Lista):-
    N is Xx - X,
    length(Lista,Nn),
    Nn =:= N.

mesmaLinhaEste_58(Atual,Objetivo,[P|R],Lista):-
    estaEste58(Atual,P),
    append([P],Lista,NList),
    mesmaLinhaEste_58(Atual,Objetivo,R,NList).

mesmaLinhaEste_58(Atual,Objetivo,[P|R],Lista):-
     \+estaEste58(Atual,P),
     mesmaLinhaEste_58(Atual,Objetivo,[R],Lista).

estaEste58((X,Y),(Xx,Yy)):-
    Xx > X,
    Yy =:= Y.
1
When you say you try to run it what exactly did you query?lurker
Please translate the names.repeat

1 Answers

1
votes

When \+estaEste58(Atual,P), the fourth clause of mesmaLinhaEste_58/4 keep building a nested tail structure, like [[[[...R...]]]].

It's a kind of undue left recursion... I think you should rewrite the recursive call like

...
mesmaLinhaEste_58(Atual,Objetivo,R,Lista).

I would also merge third and fourth clauses, since there is no point in repeating the test estaEste58(Atual,P), and would 'cons' P to Lista without calling append:

mesmaLinhaEste_58(Atual,Objetivo,[P|R],Lista):-
    (  estaEste58(Atual,P)
    -> mesmaLinhaEste_58(Atual,Objetivo,R,[P|Lista])
    ;  mesmaLinhaEste_58(Atual,Objetivo,R,Lista)
    ).