0

I am checking if the graph is connected and for some reason getting false when it should be true.

allConnected([]).
allConnected(A|L) :- checkConnect(A,L), allConnected(L).

checkConnect([],[]).
checkConnect(X, Head|Y) :- isConnected(X,Head), checkConnect(X,Y).
isConnected(X,Y) :- edge(X,Y); edge(Y,X).

edge(a,b).
edge(b,c).
edge(c,a).

What I'm doing for my predicate is checking if every node in allConnected([a,b,c]) is connected. I should be getting true, but can't pinpoint my error, I have tried using trace, but it does not help.

false
  • 10,264
  • 13
  • 101
  • 209
John Smith
  • 45
  • 1
  • 5
  • change to `allConnected([A|L]) :- ...`, otherwise it will never match. After that, then it will loop forever :) – CapelliC Mar 18 '16 at 05:18
  • @CapelliC: This problem is specific to SWI. Only SWI permits operators above 1000 in arguments. – false Mar 18 '16 at 10:56

1 Answers1

0

As CapelliC stated above, you will want to add square brackets around your lists: [A|L] instead of A|L and [Head|Y] instead of Head|Y.

The second problem is the termination condition in checkConnect:

checkConnect([],[]).

You specify it being true whenever both arguments can be matched with empty lists. However, checkConnect's first param is used to call isConnected, and will thus imply being a character, and not a list as you specify in your termination condition. In other words, checkConnect will never match its stop condition.

What you are looking for is:

checkConnect(_,[]).

(we use the wildcard _ since we do not care about accessing the variable at this point)

SND
  • 1,552
  • 2
  • 16
  • 29