Reverse the output of the Depth-First-Search Algorithm

I want to reverse the output of the code.

goal(hall).
move(gateA,gateB).
move(gateA,gateG).
move(gateB,gateC).
move(gateB,gateH).
move(gateG,gateL).
move(gateG,gateF).
move(gateL,gateS).
move(gateC,gateD).
move(gateH,gateO).
move(gateD,gateI).
move(gateD,gateJ).
move(gateI,gateP).
move(gateP,gateQ).
move(gateJ,gateR).
move(gateR,hall).




goSolveTheMaze( Start, Goal)  :-
  depthfirst( [], Start, Goal).



depthfirst( Path, Start, [Start | Path] )  :-
   goal( Start).

depthfirst( Path,Start, Hall)  :-
  move( Start, Node1),
  depthfirst( [Start | Path], Node1, Hall).

The output is :

?- goSolveTheMaze(gateA,Hall).
Hall = [hall, gateR, gateJ, gateD, gateC, gateB, gateA] .

I want it to be :
Hall =[gateA ,gateB ,gateC ,gateD ,gateJ ,gateR ,hall].

I tried to use the reverse function like this :

    reverse([])     --> [].
    reverse([Goal|Ls]) --> reverse(Ls), [Goal].

 
But it didn't work.

>Solution :

goSolveTheMaze(Start, Path) :-
    depthfirst(Start, Path).

depthfirst(Goal, [Goal]) :-
   goal(Goal).

depthfirst(Start, [Start|Path])  :-
    move(Start, Node1),
    depthfirst(Node1, Path).

Result in swi-prolog:

?- goSolveTheMaze(gateA, Path).
Path = [gateA,gateB,gateC,gateD,gateJ,gateR,hall] ;
false.

Leave a Reply