2
votes

Don't need answer anymore. its done. Thanks !

a(S,E,[S|R]) :- S\=E,(c(S, N);c(N,S)),a(N,E,R),\+member(S,R).
a(X,X,P):- P=[X].

The above code cause out of local stack problem, Could someone tell me how to fix? Thanks.

1
c(1,2). c(1,3).c(2,4).c(3,4). - SwordW
A(S,E,[S|R]) :- ... isn't valid Prolog syntax, so I doubt the code you are showing even executed. It would have thrown an error. Please show the code you actually tried. - lurker
I am so sorry, I mean a instead of A. Since Im asking a homework problem, to avoid showing answer online, I changed all the names and I made a horrible mistake. Thank you guys for pointing out - SwordW
Work harder on improving the quality of your question! Use proper code layout. Choose proper names. Write the queries you did, the answer(s) you expected and the answer(s) you got. - repeat
You should accept the answer given by @CapelliC if you found it acceptable. There's a checkmark that you click when viewing his answer. It's very unorthodox to edit your question to say it's answered. That should be evident from accepting the appropriate answer. Also, others may still want to offer answers just as useful alternatives or more information, if not for you, then for others reading the question and answers. - lurker

1 Answers

1
votes

To complete a visit of a graph with loops (introduced in your DAG from c(S, N);c(N,S)), we must check the path seen so far (just to say), but you attempt to inspect after it has been built. This 'logical loop' reflects in non termination of your code. The easier solution is to add an argument that holds the path seen so far:

a(S,E,P) :- visit(S,E,[S],P).

visit(X,X,P,R) :- reverse(P,R).
visit(S,E,P,R) :- (c(S,N);c(N,S)), \+memberchk(N,P), visit(N,E,[N|P],R).

yields

?- a(1,4,L).
L = [1, 2, 4] ;
L = [1, 3, 4] ;
false.

see this thread about some high level thoughts on this theme