Remove duplicates in list (Prolog)

后端 未结 9 2225
鱼传尺愫
鱼传尺愫 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:25

    Adding my answer to this old thread:

    notmember(_,[]).
    notmember(X,[H|T]):-X\=H,notmember(X,T).
    
    set([],[]).
    set([H|T],S):-set(T,S),member(H,S).
    set([H|T],[H|S]):-set(T,S),not(member(H,S)).
    

    The only virtue of this solution is that it uses only those predicates that have been introduced by the point where this exercise appears in the original text.

提交回复
热议问题