<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE message PUBLIC
  "-//MLarc//DTD MLarc output files//EN"
  "../../mlarc.dtd"[
  <!ATTLIST message
    listname CDATA #REQUIRED
    title CDATA #REQUIRED
  >
]>

  <?xml-stylesheet href="../../mlarc.xsl" type="text/xsl"?>


<message 
  url="2003/12/15aed9cdaa87e3077ca1de4ee1e768ca"
  from="Damien &lt;Damien.Pous@e...&gt;"
  author="Damien"
  date="2003-12-05T19:11:04"
  subject="[Caml-list] how to calculate a &quot;xor&quot;"
  prev="2003/12/9c9fa03f1342454a210a05b63a6447b6"
  next="2003/12/31e143b28f1df16344904a64cd335b6a"
  next-in-thread="2003/12/5f70e25ab8bd63188579edc825ec4041"
  prev-thread="2003/12/89c86bc0dc75f477b32fd1c1d1f8d712"
  next-thread="2003/12/6645b76fdaaac896907eba3358f17de9"
  root="../../"
  period="month"
  listname="caml-list"
  title="Archives of the Caml mailing list">

<thread subject="[Caml-list] how to calculate a &quot;xor&quot;">
<msg 
  url="2003/12/15aed9cdaa87e3077ca1de4ee1e768ca"
  from="Damien &lt;Damien.Pous@e...&gt;"
  author="Damien"
  date="2003-12-05T19:11:04"
  subject="[Caml-list] how to calculate a &quot;xor&quot;">
<msg 
  url="2003/12/5f70e25ab8bd63188579edc825ec4041"
  from="Alan Post &lt;apost@r...&gt;"
  author="Alan Post"
  date="2003-12-06T05:08:08"
  subject="[Caml-list] Re: how to calculate a &quot;xor&quot;">
</msg>
<msg 
  url="2003/12/644d57efdd9d2acc90b0e089df817412"
  from="Jean-Baptiste Rouquier &lt;jean-baptiste.rouquier@e...&gt;"
  author="Jean-Baptiste Rouquier"
  date="2003-12-10T21:47:02"
  subject="Re: [Caml-list] how to calculate a &quot;xor&quot;">
</msg>
</msg>
</thread>

<contents>
Hi algorithmers,

Given two sets A and B, I want to calculate A\B _and_ B\A.
The sets are represented by lists.

without using an order to sort the lists, 
is there something better than the following (O(n^2)) ?

&lt;&lt;
let omem a = 
  let rec aux acc = function
    | [] -&gt; None
    | a'::q when a=a' -&gt; Some (List.rev_append acc q)
    | a'::q -&gt; aux (a'::acc) q
  in aux []

let xor x = 
  let rec aux (a',b') = function
    | la, [] -&gt; List.rev_append a' la, b'
    | [], lb -&gt; a', List.rev_append b' lb
    | a::qa, b::qb when a=b -&gt; aux (a',b') (qa,qb)
    | a::qa, b::qb -&gt; match omem a qb, omem b qa with
	| None,     None     -&gt; aux (a::a', b::b') (qa, qb )
	| Some qb', None     -&gt; aux (   a', b::b') (qa, qb')
	| None,     Some qa' -&gt; aux (a::a',    b') (qa',qb )
	| Some qb', Some qa' -&gt; aux (   a',    b') (qa',qb')
  in aux ([],[]) x
&gt;&gt;

# xor ([4;1;6;2;8],[3;9;5;2;8;1;7]);;
- : int list * int list = ([6; 4], [3; 9; 5; 7])



with an order, is there something better than (O(n*ln n)) :
 * sort the two lists,
 * "merge" them to extract the result
?


what's the complexity of &lt;Set.Make(M).diff&gt;, from the standard library ?


thanks,
damien

-------------------
To unsubscribe, mail caml-list-request@inria.fr Archives: http://caml.inria.fr
Bug reports: http://caml.inria.fr/bin/caml-bugs FAQ: http://caml.inria.fr/FAQ/
Beginner's list: http://groups.yahoo.com/group/ocaml_beginners

</contents>

</message>

