Search code examples
prologpredicateswi-prolog

Why `depthfirst2` is always marked as red?


Got the following program for depth search, however the depthsearch predicate is always marked as red, so I am unable to run the program. What is the issue?

node(a).
node(b).
node(c).
node(d).
node(e).
node(f).
node(g).
node(h).
node(i).
node(j).

s(a,b). s(a,c).
s(b,d). s(b,e).
s(c,f). s(c,g).
s(d,h).
s(e,i). s(e,j).
goal(j).
goal(f).

depthfirst2( Node, [Node], _) :-
    goal( Node).

depthfirst2( Node, [Node | Sol], Maxdepth) :-
    Maxdepth > 0,
    s( Node, Node1),
    Max1 is Maxdepth - 1,
    depthfirst2( Node1, Sol, Max1).

Solution

  • Your code is working correctly. Try calling the predicate as:

    ?-depthfirst2(a,L,4).
    
    OUTPUT:
    L = [a, b, e, j]
    L = [a, c, f]
    false