help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: remove sublist


From: Yuri Khan
Subject: Re: remove sublist
Date: Mon, 2 Apr 2018 21:05:17 +0700

On Mon, Apr 2, 2018 at 8:28 PM, Emanuel Berg <moasen@zoho.com> wrote:

>>     (cl-set-difference '("0" "a" "b" "c" "d")
>>                        '("a" "b")
>>                        :test 'string=)
>
> Excellent, thanks!
>
> You got the right idea, "sublist" as in the
> math unordered set sense - and not "set" as in
> the serpent god of the paleo-pharaohs, here!

Wasn’t that Seth? (oh, Wikipedia says both variants are used.)

> Should the test function be hash-quoted?

Probably.

> Only strange thing is the order is reversed,
> except for the empty list as sublist!

For me it isn’t, but I’m on 25.1. It’s not strange though — it’s more
efficient to traverse the original list first-to-last and build the
result list last-to-first; and it’s even more efficient to detect that
the second argument is empty and return the first argument as result.

If you need to preserve the original list’s order, you should probably
use remove-if, as nothing in the name or documentation of
set-difference will or should guarantee order.

    (cl-remove-if (lambda (s) (member s '("a" "b")))
                  '("0" "a" "b" "c" "d"))

(For different types of elements, take your choice of member, memq or memql.)



reply via email to

[Prev in Thread] Current Thread [Next in Thread]