简体   繁体   中英

map two lists together in prolog

I have two facts which are x(a, b, c) and g(x, y, z).

I want to create a rule to print all possible answers such that if I pick one member from the first fact and match it with a member from the second fact then I can't match another member from the first fact to the same member in the second fact that was matched before, for ex. [a,x], [b, x] are not acceptable.

... X = [[a,x],[b,y],[c,z]]

... X = [[a,y],[b,z],[c,x]]

... X = [[a,z],[b,x],[c,y]]

.
.
.

you get the point I hope, there should be 9 I think.

Why do you think that there should be 9 in your sample? There should be exactly 6:

:- use_module(library(lambda)).

?- maplist(\K^V^KV^(KV = K-V), [a,b,c], Rs, LRs), permutation([x,y,z], Rs).
   Rs = [x,y,z], LRs = [a-x,b-y,c-z]
;  Rs = [x,z,y], LRs = [a-x,b-z,c-y]
;  Rs = [y,x,z], LRs = [a-y,b-x,c-z]
;  Rs = [y,z,x], LRs = [a-y,b-z,c-x]
;  Rs = [z,x,y], LRs = [a-z,b-x,c-y]
;  Rs = [z,y,x], LRs = [a-z,b-y,c-x]
;  false.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM