我正在使用BFS解决8难题,但对Prolog来说是新手,因此很难递归工作。我们需要将其设置为类似于所示的内容:
breadthFirstSearch(Initial, Solution) :- retractall(closed(_)), breadthFirstSearch_aux(Initial, Solution).
breadthFirstSearch_aux(Initial, [Initial]) :- goal8(Initial), !.
breadthFirstSearch_aux(Initial, Solution) :- (not(closed(Initial)) -> succ8(Initial, Neighbours), extractSuccessors(Neighbours, L),
assert(closed(Initial)), append([Initial], Solution2, Solution), nth0(0, L, Queuehead), breadthFirstSearch_aux(Queuehead, Solution2);
append([Initial], Solution2, Solution), nth0(0, L, Queuehead), breadthFirstSearch_aux(Queuehead, Solution2)).
extractSuccessors(X, Y) :- X = [].
extractSuccessors([(_,First)|Rest], X) :- append([X], [First], NewQ), extractSuccessors(Rest, NewQ).
我遇到的第一个问题是我的extractSuccessors函数,我希望它返回状态列表,但它只返回true。例如:
?- extractSuccessors( [(1, [1, 2, 3, 4, 5, 6, 7|...]), (1, [1, 2, 3, 0, 5,
6|...])], Y).
true.
[trace] ?- extractSuccessors( [(1, [1, 2, 3, 4, 5, 6, 7|...]), (1, [1, 2,
3, 0, 5, 6|...])], Y).
Call: (8) extractSuccessors([(1, [1, 2, 3, 4, 5, 6|...]), (1, [1, 2, 3,
0, 5|...])], _3088) ? creep
Call: (9) lists:append([_3088], [[1, 2, 3, 4, 5, 6|...]], _3440) ? creep
Exit: (9) lists:append([_3088], [[1, 2, 3, 4, 5, 6|...]], [_3088, [1, 2,
3, 4, 5|...]]) ? creep
Call: (9) extractSuccessors([(1, [1, 2, 3, 0, 5, 6|...])], [_3088, [1, 2,
3, 4, 5, 6|...]]) ? creep
Call: (10) lists:append([[_3088, [1, 2, 3, 4|...]]], [[1, 2, 3, 0, 5,
6|...]], _3458) ? creep
Exit: (10) lists:append([[_3088, [1, 2, 3, 4|...]]], [[1, 2, 3, 0, 5,
6|...]], [[_3088, [1, 2, 3, 4|...]], [1, 2, 3, 0, 5|...]]) ? creep
Call: (10) extractSuccessors([], [[_3088, [1, 2, 3, 4, 5|...]], [1, 2, 3,
0, 5, 6|...]]) ? creep
Exit: (10) extractSuccessors([], [[_3088, [1, 2, 3, 4, 5|...]], [1, 2, 3,
0, 5, 6|...]]) ? creep
Exit: (9) extractSuccessors([(1, [1, 2, 3, 0, 5, 6|...])], [_3088, [1, 2,
3, 4, 5, 6|...]]) ? creep
Exit: (8) extractSuccessors([(1, [1, 2, 3, 4, 5, 6|...]), (1, [1, 2, 3,
0, 5|...])], _3088) ? creep
true.
为什么它不返回列表Y?我意识到其余的实施尚未完全完成。