Search code examples
prolog

How to transform a list of lists into a single list in prolog


I am wondering how to transform a list that looks like this: [[a,b,c], [[b,c],[a,c],[a,b]] ,which indicates the vertices adjacent to every vertex in the first sub list, to a list that looks like this: [*adj*(a,[b,c]), *adj*(b,[a,c]), *adj*(c,[a,b])]

I am wondering if using maplist would help in this situation or if there is a list operation I can use to get the result.


Solution

  • It seems to me that this works:

    map([X,Y],L) :- map(X,Y,L).
    
    map([],[],[]).
    map([H1|T1],[H2|T2],[adj(H1,H2)|T3]) :- map(T1,T2,T3).
    
    ?- map([[a,b,c], [[b,c],[a,c],[a,b]]],X).
    

    That gives:

    X = [foo(a, [b, c]), foo(b, [a, c]), foo(c, [a, b])].