solve(Start, ActionList) :- depthfirst([Start], [], RevActionList), reverse(RevActionList, ActionList). depthfirst([State | States], ActionList, ActionList) :- goal(State). depthfirst([State | States], ActionList, FinalActions) :- successor(State, Action, NewState), \+ visitedBefore(NewState, [State | States]), depthfirst([NewState, State | States], [Action | ActionList], FinalActions). reverse([], []). reverse([Head|Tail], Reversed) :- reverse(Tail, Revtail), append(Revtail, [Head], Reversed). append([], Xs, Xs). append([X|Xs], Ys, [X|Zs]) :- append(Xs, Ys, Zs). visitedBefore(X, [Y|Ys]) :- sameState(X, Y). visitedBefore(X, [Y|Ys]) :- visitedBefore(X, Ys).