distinct elements in a list



On 6/17/07, Robert Dodier <robert.dodier at gmail.com> wrote:
>
> On 6/17/07, sen1 at math.msu.edu <sen1 at math.msu.edu> wrote:
>
> >   Given a list 'list_1', one way to make a list of the distinct
> >   elements of 'list_1' is;
> >
> > listify(setify(list_1))$
> >
> > Is there a better (i.e., faster) way ?
>
> Glancing at src/nset.lisp, there doesn't seem to be a single
> function to do that, and from what I can tell, the formulation
> above should work OK.
>
> I think there should be a function specifically for the purpose
> of returning the distinct elements of a list; it could be called "unique".
> e.g.
>
> (defun $unique (x)
>   (if ($listp x)
>     `((mlist) ,@(sorted-remove-duplicates (sort (cdr x) '$orderlessp)))
>     x))


Yes, this might be convenient, though it's no faster than

       unique(x):= if listp(x) or setp(x) then listify(setify(x)) else
error("Unique applies to lists or sets only", x)$

           -s