I am trying to solve the puzzle 15 game in OCaml. I almost did everything.. except for the core function of the program, the depth first search function. This is what my function looks like now:
let df_search_path (Graph next) start =
let rec from_node visited a =
print(a);
if List.mem a visited then raise Fail
else if goal(a) then [a]
else a::from_list (a::visited) (next a)
and from_list visited nextA = match visited with
[] -> raise Fail
| nextA::rest -> try from_node visited nextA
with Fail -> from_list visited rest
in from_node [] start;;
Graph
is a graph, next
is a function that searches for the 2,3 or 4 next moves (I move the empty space), goal
is a function to check if current a
state is the goal and Fail
is a normal exception.
I tried a lot all the other functions and they works well.
This function endlessy loops always in the starting state, and I can't understand why.
print
is a custom function to print the state a
in a friendly form.
What am I doing wrong? I'm making my test using a state "1 move" far from the solution, just a "go down" will solve this.. but it is stuck in the first state, always printing the start configuration!