Predicate to unzip a list

前端 未结 3 1813
失恋的感觉
失恋的感觉 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:13

    You're matching the tuple as a whole, rather than it's component parts.

    You should match on [(X1,Y1)|XS], instead of [X|XS] and [Y|Ys].

    splt([],[],[]).
    splt([(X1,Y1)|Xs],[X1|T1],[Y1|T2]):-
      splt(Xs,T1,T2).
    

    Here the first term is used as input, the second and third as output.

    Ideone example, using SWI-Prolog, here.

提交回复
热议问题