Jump to content

Symmetric difference: Difference between revisions

Rename Perl 6 -> Raku, alphabetize, minor clean-up
No edit summary
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 20:
# In the mathematical notation above <code>A \ B</code> gives the set of items in A that are not in B; <code>A ∪ B</code> gives the set of items in both A and B, (their ''union''); and <code>A ∩ B</code> gives the set of items that are in both A and B (their ''intersection'').
<br><br>
 
=={{header|Ada}}==
Ada has the lattice operation '''xor''' predefined on Boolean, modular types, 1D arrays, set implementations from the standard library. The provided solution uses arrays:
Line 129 ⟶ 130:
Symmetric Difference: {Jim, Serena}
Symmetric Difference 2: {Jim, Serena}</pre>
 
 
=={{header|AppleScript}}==
Line 258:
 
<pre>#("Jim" "Serena")</pre>
 
 
=={{header|AutoHotkey}}==
Line 742 ⟶ 741:
? symmDiff(["John", "Bob", "Mary", "Serena"].asSet(), ["Jim", "Mary", "John", "Bob"].asSet())
# value: ["Jim", "Serena"].asSet()</lang>
 
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<lang elixir>iex(1)> a = ~w[John Bob Mary Serena] |> MapSet.new
#MapSet<["Bob", "John", "Mary", "Serena"]>
iex(2)> b = ~w[Jim Mary John Bob] |> MapSet.new
#MapSet<["Bob", "Jim", "John", "Mary"]>
iex(3)> sym_dif = fn(a,b) -> MapSet.difference(MapSet.union(a,b), MapSet.intersection(a,b)) end
#Function<12.54118792/2 in :erl_eval.expr/5>
iex(4)> sym_dif.(a,b)
#MapSet<["Jim", "Serena"]></lang>
 
=={{header|Erlang}}==
<lang erlang>%% Implemented by Arjun Sunel
-module(symdiff).
-export([main/0]).
 
main() ->
SetA = sets:from_list(["John","Bob","Mary","Serena"]),
SetB = sets:from_list(["Jim","Mary","John","Bob"]),
AUnionB = sets:union(SetA,SetB),
AIntersectionB = sets:intersection(SetA,SetB),
SymmDiffAB = sets:subtract(AUnionB,AIntersectionB),
sets:to_list(SymmDiffAB).
</lang>
 
{{out}}
<pre>["Serena","Jim"]
</pre>
 
=={{header|F Sharp|F#}}==
<lang fsharp>> let a = set ["John"; "Bob"; "Mary"; "Serena"]
let b = set ["Jim"; "Mary"; "John"; "Bob"];;
 
val a : Set<string> = set ["Bob"; "John"; "Mary"; "Serena"]
val b : Set<string> = set ["Bob"; "Jim"; "John"; "Mary"]
 
> (a-b) + (b-a);;
val it : Set<string> = set ["Jim"; "Serena"]</lang>
Or, if you don't like the infix operators:
<lang fsharp>> Set.union (Set.difference a b) (Set.difference b a);;
val it : Set<string> = set ["Jim"; "Serena"]</lang>
 
=={{header|Eiffel}}==
Line 831 ⟶ 788:
 
end</lang>
 
=={{header|Elixir}}==
{{works with|Elixir|1.2}}
<lang elixir>iex(1)> a = ~w[John Bob Mary Serena] |> MapSet.new
#MapSet<["Bob", "John", "Mary", "Serena"]>
iex(2)> b = ~w[Jim Mary John Bob] |> MapSet.new
#MapSet<["Bob", "Jim", "John", "Mary"]>
iex(3)> sym_dif = fn(a,b) -> MapSet.difference(MapSet.union(a,b), MapSet.intersection(a,b)) end
#Function<12.54118792/2 in :erl_eval.expr/5>
iex(4)> sym_dif.(a,b)
#MapSet<["Jim", "Serena"]></lang>
 
=={{header|Erlang}}==
<lang erlang>%% Implemented by Arjun Sunel
-module(symdiff).
-export([main/0]).
 
main() ->
SetA = sets:from_list(["John","Bob","Mary","Serena"]),
SetB = sets:from_list(["Jim","Mary","John","Bob"]),
AUnionB = sets:union(SetA,SetB),
AIntersectionB = sets:intersection(SetA,SetB),
SymmDiffAB = sets:subtract(AUnionB,AIntersectionB),
sets:to_list(SymmDiffAB).
</lang>
 
{{out}}
<pre>["Serena","Jim"]
</pre>
 
=={{header|F Sharp|F#}}==
<lang fsharp>> let a = set ["John"; "Bob"; "Mary"; "Serena"]
let b = set ["Jim"; "Mary"; "John"; "Bob"];;
 
val a : Set<string> = set ["Bob"; "John"; "Mary"; "Serena"]
val b : Set<string> = set ["Bob"; "Jim"; "John"; "Mary"]
 
> (a-b) + (b-a);;
val it : Set<string> = set ["Jim"; "Serena"]</lang>
Or, if you don't like the infix operators:
<lang fsharp>> Set.union (Set.difference a b) (Set.difference b a);;
val it : Set<string> = set ["Jim"; "Serena"]</lang>
 
=={{header|Factor}}==
Line 1,897 ⟶ 1,896:
in
{Show {SymDiff A B}}</lang>
 
=={{header|PARI/GP}}==
<lang parigp>sd(u,v)={
my(r=List());
u=vecsort(u,,8);
v=vecsort(v,,8);
for(i=1,#u,if(!setsearch(v,u[i]),listput(r,u[i])));
for(i=1,#v,if(!setsearch(u,v[i]),listput(r,v[i])));
Vec(r)
};
sd(["John", "Serena", "Bob", "Mary", "Serena"],["Jim", "Mary", "John", "Jim", "Bob"])</lang>
 
=={{header|Pascal}}==
Line 1,934 ⟶ 1,944:
ListA - ListB -> Serena
ListB - ListA -> Jim</pre>
 
=={{header|PARI/GP}}==
<lang parigp>sd(u,v)={
my(r=List());
u=vecsort(u,,8);
v=vecsort(v,,8);
for(i=1,#u,if(!setsearch(v,u[i]),listput(r,u[i])));
for(i=1,#v,if(!setsearch(u,v[i]),listput(r,v[i])));
Vec(r)
};
sd(["John", "Serena", "Bob", "Mary", "Serena"],["Jim", "Mary", "John", "Jim", "Bob"])</lang>
 
=={{header|Perl}}==
Line 1,968 ⟶ 1,967:
B\A: Jim
Symm: Serena Jim</pre>
 
=={{header|Perl 6}}==
<lang perl6>my \A = set <John Serena Bob Mary Serena>;
my \B = set <Jim Mary John Jim Bob>;
 
say A ∖ B; # Set subtraction
say B ∖ A; # Set subtraction
say (A ∪ B) ∖ (A ∩ B); # Symmetric difference, via basic set operations
say A ⊖ B; # Symmetric difference, via dedicated operator</lang>
{{out}}
<pre>set(Serena)
set(Jim)
set(Jim, Serena)
set(Jim, Serena)</pre>
 
=={{header|Phix}}==
Line 2,349 ⟶ 2,334:
(set-subtract B A)
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>my \A = set <John Serena Bob Mary Serena>;
my \B = set <Jim Mary John Jim Bob>;
 
say A ∖ B; # Set subtraction
say B ∖ A; # Set subtraction
say (A ∪ B) ∖ (A ∩ B); # Symmetric difference, via basic set operations
say A ⊖ B; # Symmetric difference, via dedicated operator</lang>
{{out}}
<pre>set(Serena)
set(Jim)
set(Jim, Serena)
set(Jim, Serena)</pre>
 
=={{header|REBOL}}==
Line 2,624:
'(Jim Mary John Jim Bob))) (newline)
</lang>
 
 
=={{header|Seed7}}==
10,339

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.