Prolog union for A U B U C

前端 未结 3 1511
情歌与酒
情歌与酒 2020-11-21 05:23

I\'ve started to learn Prolog recently and I can\'t solve how to make union of three lists.

I was able to make union of 2 lists :

%element
element(X,         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-21 05:58

    You can make the union of the first two lists and then the union between that result and the third:

    union(L1, L2, L3, U):-union(L1, L2, U12), union(U12, L3, U).
    

    You can improve union/3 with a cut operator:

    union([],M,M).
    union([X|Y],L,S) :- element(X,L), !, union(Y,L,S).
    union([X|Y],L,[X|S]) :- union(Y,L,S).
    

提交回复
热议问题