Remove duplicates in list (Prolog)

后端 未结 9 2227
鱼传尺愫
鱼传尺愫 2020-11-27 08:02

I am completely new to Prolog and trying some exercises. One of them is:

Write a predicate set(InList,OutList) which takes as input an arbitrary l

9条回答
  •  鱼传尺愫
    2020-11-27 08:19

    I think that a better way to do this would be:

    set([], []).
    set([H|T], [H|T1]) :- subtract(T, [H], T2), set(T2, T1).
    

    So, for example ?- set([1,4,1,1,3,4],S) give you as output:

    S = [1, 4, 3]
    

提交回复
热议问题