Predicate to unzip a list

前端 未结 3 1819
失恋的感觉
失恋的感觉 2021-01-21 01:53
List1=[(x,1),(y,1),(z,1)]

I\'m attempting to split this list:

into two lists:

List3=[x,y,z]
List4=[1,1,1]

So

3条回答
  •  渐次进展
    2021-01-21 02:17

    First, the term you have chosen. This: (a, b), is most definitely not how you would usually represent a "tuple" in Prolog. You almost always use a-b for a "pair", and pairs are used throughout the standard libraries.

    So your initial list would look like this: [x-1, y-1, z-1].

    This should also explain why you are having your problem. You write (a, b), but your predicate says a, b, and you consume two elements when you expect to get one ,(a,b) term. So, to fix your current predicate you would write:

    split([], [], []).
    split([X|Xs], [Y|Ys], [(X,Y)|XYs]) :-
        split(Xs, Ys, XYs).
    
    ?- split(Xs, Ys, [(x,1), (y,1), (z,1)]).
    Xs = [x, y, z],
    Ys = [1, 1, 1].
    

    But instead, using a more conventional name, term order, and Prolog pairs:

    zip([], [], []).
    zip([X-Y|XYs], [X|Xs], [Y|Ys]) :-
        zip(XYs, Xs, Ys).
    
    ?- zip([x-1, y-1, z-1], Xs, Ys).
    Xs = [x, y, z],
    Ys = [1, 1, 1].
    

    And of course, SWI-Prolog at least has a library(pairs), and it comes with a pairs_keys_values/3:

    ?- pairs_keys_values([x-1, y-1, z-1], Xs, Ys).
    Xs = [x, y, z],
    Ys = [1, 1, 1].
    

提交回复
热议问题