remove items appearing in both lists
  Home FAQ Contact Sign in
comp.lang.functional only
 
Advanced search
POPULAR GROUPS

more...

comp.lang.functional Profile…
 Up
remove items appearing in both lists         


Author: xahlee
Date: Jul 20, 2008 07:14

Given 2 lists Olist and Clist. I want to remove items that appears in
both list. How best to do this?

Say,

(setq Olist (list "a" "b" "c" "d" "e"))
(setq Clist (list "ee" "a" "d" "c" "bb"))

i want the result to be like

(setq Olist (list "b" "e"))
(setq Clist (list "ee" "bb"))

order doesn't matter.

i think i can go thru each element and build OlistNew, and removing
Clist to build a ClistNew. But the checking existance and building
item by item would be difficult or very inefficient i think but am not
sure about lisp here.

should i turn them both into hash table first for easy checking
existance?

Thanks.

Xah
http://xahlee.org/

10 Comments
Re: remove items appearing in both lists         


Author: xahlee
Date: Jul 20, 2008 07:16

Posted to too many groups by mistake. Sorry. Fixed follow up to just
lisp and emacs.
Show full article (0.86Kb)
no comments
Re: remove items appearing in both lists         


Author: Jim Burton
Date: Jul 21, 2008 08:37

On Jul 20, 3:16 pm, "xah...@gmail.com" gmail.com> wrote:
> Posted to too many groups by mistake. Sorry.

That's going on a t-shirt :-)
> Fixed follow up to just
> lisp and emacs.
>
> On Jul 20, 7:14 am, "xah...@gmail.com" gmail.com> wrote:
>
>> Given 2 lists Olist and Clist. I want to remove items that appears in
>> both list...
Show full article (1.02Kb)
no comments
Re: remove items appearing in both lists         


Author: Jon Harrop
Date: Jul 22, 2008 04:29

xahlee@gmail.com wrote:
> Given 2 lists Olist and Clist. I want to remove items that appears in
> both list. How best to do this?
>
> Say,
>
> (setq Olist (list "a" "b" "c" "d" "e"))
> (setq Clist (list "ee" "a" "d" "c" "bb"))
>
> i want the result to be like
>
> (setq Olist (list "b" "e"))
> (setq Clist (list "ee" "bb"))
>
> order doesn't matter.

In F#, convert the lists to sets and use set theoretic operations:

let Olist, Clist = set Olist, set Clist
let union = Olist + Clist
let Olist, Clist = Olist - union, Clist - union
Show full article (0.64Kb)
no comments
Re: remove items appearing in both lists         


Author: xahlee
Date: Jul 22, 2008 14:33

Rainer Joswig and Kent Pitman suggested the Common Lisp solution:

(psetq Olist (set-difference Olist Clist :test #'string=)
Clist (set-difference Clist Olist :test #'string=))

Thanks to Rainer Joswig and Kent Pitman.

Jon Harrop wrote:
> In F#, convert the lists to sets and use set theoretic operations:
>
> let Olist, Clist = set Olist, set Clist
> let union = Olist + Clist
> let Olist, Clist = Olist - union, Clist - union

O, great. In Mathematica.

olist = {"a", "b", "c", "d", "e"};
clist = {"ee", "a", "d", "c", "bb"};

olist = Complement[olist, clist]
clist = Complement[clist, olist]

btw, if i'm curious and want to have a peek of docs of Caml or F# now
and then, is there a easy to use website?

--------------------------------------
David Kastrup offered a pure elisp solution:
Show full article (2.32Kb)
no comments
Re: remove items appearing in both lists         


Author: Kaz Kylheku
Date: Jul 22, 2008 15:36

On 2008-07-20, xahlee@gmail.com gmail.com> wrote:
> Given 2 lists Olist and Clist. I want to remove items that appears in
> both list. How best to do this?
>
> Say,
>
> (setq Olist (list "a" "b" "c" "d" "e"))
> (setq Clist (list "ee" "a" "d" "c" "bb"))
>
> i want the result to be like
>
> (setq Olist (list "b" "e"))
> (setq Clist (list "ee" "bb"))

Since you're assigning to variables, wouldn't this be off-topic in
comp.lang.functional? :)
> order doesn't matter.

;; items common to both sets

(intersection '("a" "b" "c") '("b" "c" "d") :test #'equal)

-> ("b" "c")
Show full article (0.83Kb)
no comments
Re: remove items appearing in both lists         


Author: David Kastrup
Date: Jul 22, 2008 23:49

"xahlee@gmail.com" gmail.com> writes:
> Rainer Joswig and Kent Pitman suggested the Common Lisp solution:
>
> (psetq Olist (set-difference Olist Clist :test #'string=)
> Clist (set-difference Clist Olist :test #'string=))
>
> Thanks...
Show full article (2.07Kb)
no comments
Re: remove items appearing in both lists         


Author: John Thingstad
Date: Jul 23, 2008 03:04

På Wed, 23 Jul 2008 08:49:03 +0200, skrev David Kastrup gnu.org>:
>
> The Common Lisp solution that you are clamoring for most certainly
> _isn't_ O(n lg n) but rather O(n^2). The same likely holds for the
> other "elegant" solutions based on a set abstraction. You can at best
> have O(n^2) behavior without making use of an order relation, because
> then you are reduced to comparing everything with everything.
>
> If we are not talking about similarly sized sets, the "elegant"
> solutions are O(m n), and the one using sorting is O(n lg n + m lg m).
>

Not entirely true.
You could store one set in a hash table without making assumptions about
order.
Then you have k * n complexity. I believe SBCL does this for large lists.
K here is a high number so for small lists the approach above is still
better.
Show full article (1.08Kb)
no comments
Re: remove items appearing in both lists         


Author: Dr Jon D Harrop
Date: Jul 23, 2008 03:15

On Jul 22, 10:33 pm, "xah...@gmail.com" gmail.com> wrote:
> O, great. In Mathematica.
>
> olist = {"a", "b", "c", "d", "e"};
> clist = {"ee", "a", "d", "c", "bb"};
>
> olist = Complement[olist, clist]
> clist = Complement[clist, olist]

Sorry, my F# code was wrong. It should have been:

let olist = set ["a"; "b"; "c"; "d"; "e"]
let clist = set ["ee"; "a"; "d"; "c"; "bb"]
let olist, clist = olist - clist, clist - olist
> btw, if i'm curious and want to have a peek of docs of Caml or F# now
> and then, is there a easy to use website?

There is a site running OCamlJava somewhere but it is not the real
OCaml implementation (e.g. no tail calls).

Easiest to "apt-get install ocaml" under Linux or install Visual
Studio Shell and the F# distribution under Windows.
Show full article (0.81Kb)
no comments
Re: remove items appearing in both lists         


Author: David Kastrup
Date: Jul 23, 2008 03:17

"John Thingstad" writes:
> På Wed, 23 Jul 2008 08:49:03 +0200, skrev David Kastrup gnu.org>:
>
>>
>> The Common Lisp solution that you are clamoring for most certainly
>> _isn't_ O(n lg n) but rather O(n^2). The same likely holds for the
>> other "elegant" solutions based on a set abstraction. You can at best
>> have O(n^2) behavior without making use of an order relation, because
>> then you are reduced to comparing everything with everything.
>>
>> If we are not talking about similarly sized sets, the "elegant"
>> solutions are O(m n), and the one using sorting is O(n lg n + m lg m).
>>
>
> Not entirely true.
> You could store one set in a hash table without making assumptions
> about order.

Hash codes _provide_ an order relation.
Show full article (1.36Kb)
no comments
1 2