lambda - Removing duplicates in list results with unneeded tail -
lambda - Removing duplicates in list results with unneeded tail -
i want remove duplicates given list .
consider next code :
% check if given element in given list member(element,[element|_]). member(element,[_|list]):-member(element, list). % append element if it's not in input list appending([],x,x). appending([h|t1],elem,[h|t2]):- appending(t1,elem,t2). appendhlp(listorg,res,addme):- not(member(addme,listorg)), appending(listorg,[addme],res). appendhlp(listorg,res,addme):- member(addme,listorg), res=listorg. % remove duplicates setify([h|t],set):-appendhlp(set,output,h), setify(t,output). setify([],_). and when run code :
1 ?- setify([1,2,3,3,2],x). the output :
x = [1, 2, 3|_g2725] how can remove tail ?
thanks
i quite new prolog. solution came remove duplicate elements. hope helps.
% define negation of "member" notmember(x,l) :- not( member(x,l) ). % remove duplicates terminal case removedups( [], r, r). % case head element fellow member of tail. drop it. removedups( [h|t], r, ) :- member(h,t) , removedups( t, r, ). % case head element unique removedups([h|t], r, ) :- notmember(h,t), append(a,[h],n), removedups(t,r,n). % main predicate remove duplicates original order maintained. uniq(x,y) :- removedups(x,y,[]). lambda prolog
Comments
Post a Comment