Handling all possible results in prolog -
i have clause is_adjacent([x1,y1], [x2,y2])
true, when [x2,y2]
neighbor of [x1,y1]
. possible results (four) if ask is_adjacent([2,2], [x,y])
, in case:
is_adjacent([2,2],[x,y]). x = 2, y = 3 ; x = 3, y = 2 ; x = 2, y = 1 ; x = 1, y = 2.
i want use behavior in clause:
anotherclause :- is_adjacent([2,2],[x,y]), [to continued...].
now, how can work automatically every possible result? example if want check if every single result member of specific list.
thanks answers!
there forall/2, seems fit requirements:
% fails because not results in list anotherclause :- forall(is_adjacent([2, 2], [x, y]), memberchk([x, y], [[2, 3], [3, 2]])). % succeed, results in list anotherclause :- forall(is_adjacent([2, 2], [x, y]), memberchk([x, y], [[2, 3], [3, 2], [1, 2], [2, 1]])).
for test purpose, coded is_adjacent way:
is_adjacent([a, b], [x, y]) :- member(u / v, [1 / 0, 0 / 1, -1 / 0, 0 / -1]), x + u, y b + v.
Comments
Post a Comment