Prolog findall / 3: more than one bag

I am writing an AI for the game Fox and Geese. One of my predicates looks like this:

moveFox(+PrevState, -NextState, -PegList, +VisitedStates, -NewVisitedStates)

He accepts the state of the game and makes a move with the fox. The resulting state is unified with NextState, and the actual move is unified with PegList. Everything works as expected.

I calculate the utility score for all moves NextState. To find the state with the highest utility value, I use findall/3to get all the states in the list before comparing their utility ratings.

findall(NextState, moveFox(...), NextStatesList)

Having found the maximum indicator of utility, I know NextState(and also its position in the list) with the highest indicator of utility. There is only one problem, at the moment I have not written any predicate to conclude what step was taken for NextState, for example:

getMove(+PrevState, +NextState, -PegList)

Instead of writing such a predicate, I would rather use findall/3either the equivalent. My question is, is there a way to get two different variables in two different lists. I think so (if it worked):

findall([NextState, PegList], moveFox(...), [NextStatesList, MoveList])

Can I implement such functionality without having to run twice findall/3(ugly overhead) or write this predicate getMove(+PrevState, +NextState, -PegList)?

+5
source share
1 answer

, , , (pairs)

...
findall(NextState-PegList, moveFox(...), Pairs),
pairs_keys_values(Pairs, NextStates, Pegs),
...

_keys_values ​​/3, maplist, . maplist:

pkv(K-V, K, V).
pairs_keys_values(Pairs, Keys, Vals) :-
    maplist(pkv, Pairs, Keys, Vals).
+3

All Articles