Symmetric difference: Difference between revisions

m
First example shows symmetric difference w/o using `><` operator
m (→‎{{header|AppleScript}}: Added some alternatives.)
m (First example shows symmetric difference w/o using `><` operator)
 
(15 intermediate revisions by 8 users not shown)
Line 377:
<pre>
Serena Jim
</pre>
 
=={{header|Amazing Hopper}}==
<syntaxhighlight lang="c">
#include <basico.h>
 
algoritmo
matrices (A, B)
"John", "Bob", "Mary", "Serena", enlistar en 'A'
"Jim", "Mary", "John", "Bob", enlistar en 'B'
" A XOR B: {", diferencia simétrica(A,B), "}\n"
" A \ B: {", diferencia(A,B), "}\n"
" B \ A: {", diferencia(B,A), "}\n"
imprimir
terminar
</syntaxhighlight>
{{out}}
<pre>
A XOR B: {Jim,Serena}
A \ B: {Serena}
B \ A: {Jim}
</pre>
 
Line 1,156 ⟶ 1,180:
ReadLn;
END.</syntaxhighlight>
'''Delphi/Object Pascal actually has a 'Symmetric Difference' operator `> <`: '''
<syntaxhighlight lang="pascal">
program SymmetricDifference;
 
type
charSet = set of Char;
 
var
s1, s2, s3: charSet;
ch: char;
 
begin
s1 := ['a', 'b', 'c', 'd'];
s2 := ['c', 'd', 'e', 'f'];
s3 := s1 >< s2;
 
for ch in s3 do
write(ch, ' ');
writeLn;
end.
</syntaxhighlight>
Output:
<pre>
a b e f
</pre>
 
=={{header|Déjà Vu}}==
Line 1,182 ⟶ 1,231:
? symmDiff(["John", "Bob", "Mary", "Serena"].asSet(), ["Jim", "Mary", "John", "Bob"].asSet())
# value: ["Jim", "Serena"].asSet()</syntaxhighlight>
 
=={{header|EasyLang}}==
<syntaxhighlight>
a$[] = [ "John" "Bob" "Mary" "Serena" ]
b$[] = [ "Jim" "Mary" "John" "Bob" ]
#
for i to 2
for a$ in a$[]
for b$ in b$[]
if a$ = b$
a$ = ""
break 1
.
.
if a$ <> ""
c$[] &= a$
.
.
swap a$[] b$[]
.
print c$[]
</syntaxhighlight>
 
=={{header|Eiffel}}==
Line 1,404 ⟶ 1,475:
A - B : [Serena]
B - A : [Jim]
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
include "NSLog.incl"
 
local fn SymmetricDifferenceOfSets( setA as CFSetRef, setB as CFSetRef ) as CFSetRef
CFMutableSetRef notInSetA = fn MutableSetWithSet( setB )
MutableSetMinusSet( notInSetA, setA )
CFMutableSetRef notInSetB = fn MutableSetWithSet( setA )
MutableSetMinusSet( notInSetB, setB )
CFMutableSetRef symmetricDifference = fn MutableSetWithSet( notInSetA )
MutableSetUnionSet( symmetricDifference, notInSetB )
end fn = fn SetWithSet( symmetricDifference )
 
CFSetRef set1, set2
 
set1 = fn SetWithObjects( @"John", @"Serena", @"Bob", @"Mary", @"Serena", NULL )
set2 = fn SetWithObjects( @"Jim", @"Mary", @"John", @"Jim", @"Bob", NULL )
 
NSLog( @"Symmetric difference:\n%@", fn SymmetricDifferenceOfSets( set1, set2 ) )
 
HandleEvents
</syntaxhighlight>
{{output}}
<pre>
Symmetric difference:
{(
Jim,
Serena
)}
</pre>
 
Line 2,323 ⟶ 2,425:
to_sorted_list(Set, Elems),
io.format("%11s: %s\n", [s(Desc), s(string(Elems))], !IO).</syntaxhighlight>
 
=={{header|MiniScript}}==
<syntaxhighlight lang="miniscript">
Set = new map
Set["set"] = {}
 
Set.init = function(items)
set = new Set
set.set = {}
for item in items
set.add(item)
end for
return set
end function
 
Set.contains = function(item)
return self.set.hasIndex(item)
end function
 
Set.items = function
return self.set.indexes
end function
 
Set.add = function(item)
self.set[item] = true
end function
 
Set.union = function(other)
result = Set.init
result.set = self.set + other.set
return result
end function
 
Set.difference = function(other)
result = Set.init
for item in self.items
if not other.contains(item) then result.add(item)
end for
return result
end function
 
Set.symmetricDifference = function(other)
diff1 = self.difference(other)
diff2 = other.difference(self)
return diff1.union(diff2)
end function
 
a = ["John", "Serena", "Bob", "Mary", "Serena"]
b = ["Jim", "Mary", "John", "Jim", "Bob"]
 
A1 = Set.init(a)
B1 = Set.init(b)
 
print "A XOR B " + A1.symmetricDifference(B1).items
print "A - B " + A1.difference(B1).items
print "B - A " + B1.difference(A1).items
</syntaxhighlight>
{{out}}
<pre>A XOR B ["Serena", "Jim"]
A - B ["Serena"]
B - A ["Jim"]
</pre>
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda>main :: [sys_message]
main = [Stdout (show (symdiff a b) ++ "\n")]
where a = ["John", "Serena", "Bob", "Mary", "Serena"]
b = ["Jim", "Mary", "John", "Jim", "Bob"]
 
symdiff :: [*]->[*]->[*]
symdiff a b = (a' -- b') ++ (b' -- a')
where a' = nub a
b' = nub b
 
nub :: [*]->[*]
nub = f []
where f acc [] = acc
f acc (a:as) = f acc as, if a $in acc
= f (a:acc) as, otherwise
 
in :: *->[*]->bool
in i [] = False
in i (a:as) = a == i \/ i $in as</syntaxhighlight>
{{out}}
<pre>["Serena","Jim"]</pre>
 
=={{header|Nim}}==
Line 2,488 ⟶ 2,675:
Put('ListA -> ', ListA);
Put('ListB -> ', ListB);
Put('ListA >< ListB -> ', (ListA ><- ListB) + (ListB - ListA));
Put('ListA - ListB -> ', ListA - ListB);
Put('ListB - ListA -> ', ListB - ListA);
Line 2,499 ⟶ 2,686:
ListA - ListB -> Serena
ListB - ListA -> Jim</pre>
'''Object Pascal actually has a 'Symmetric Difference' operator `> <`: '''
<syntaxhighlight lang="pascal">
program SymmetricDifference;
 
type
charSet = set of Char;
 
var
s1, s2, s3: charSet;
ch: char;
 
begin
s1 := ['a', 'b', 'c', 'd'];
s2 := ['c', 'd', 'e', 'f'];
s3 := s1 >< s2;
 
for ch in s3 do
write(ch, ' ');
writeLn;
end.
</syntaxhighlight>
Output:
<pre>
a b e f
</pre>
 
=={{header|Perl}}==
Line 2,990 ⟶ 3,202:
</pre>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, John Bob Mary Serena: e.A
, Jim Mary John Bob: e.B
= <Prout <Symdiff (e.A) (e.B)>>;
};
 
Symdiff {
(e.1) (e.2), <Diff (<Set e.1>) (<Set e.2>)>: e.3
, <Diff (<Set e.2>) (<Set e.1>)>: e.4
= <Union (e.3) (e.4)>;
};
 
Set {
= ;
s.1 e.1 s.1 e.2 = <Set e.1 s.1 e.2>;
s.1 e.1 = s.1 <Set e.1>;
};
 
Union {
(e.1) (e.2) = <Set e.1 e.2>;
};
 
Diff {
() (e.1) = ;
(e.1) () = e.1;
(s.1 e.1) (e.2 s.1 e.3) = <Diff (e.1) (e.2 e.3)>;
(s.1 e.1) (e.2) = s.1 <Diff (e.1) (e.2)>;
};</syntaxhighlight>
{{out}}
<pre>Serena Jim</pre>
=={{header|REXX}}==
===version 1===
Line 3,124 ⟶ 3,367:
see blist2 see nl
</syntaxhighlight>
 
=={{header|RPL}}==
<code>DIFFL</code> is defined at [[Set#RPL|Set]]
{{works with|Halcyon Calc|4.2.9}}
≪ DUP2 <span style="color:blue>DIFFL</span>
ROT ROT SWAP <span style="color:blue>DIFFL</span> +
≫ ≫ '<span style="color:blue>SYMDIFF</span>' STO
 
{"John" "Bob" "Mary" "Serena"} {"Jim" "Mary" "John" "Bob"} <span style="color:blue>SYMDIFF</span>
{{out}}
<pre>
1: {"Jim" "Serena"}
</pre>
 
=={{header|Ruby}}==
Line 3,299 ⟶ 3,555:
{Jim, Serena}
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program symmetric_difference;
A := {"John", "Bob", "Mary", "Serena"};
B := {"Jim", "Mary", "John", "Bob"};
 
print("A - B:", A - B);
print("B - A:", B - A);
print("Symmetric difference:", (A-B) + (B-A));
end program;</syntaxhighlight>
{{out}}
<pre>A - B: {Serena}
B - A: {Jim}
Symmetric difference: {Jim Serena}</pre>
 
=={{header|Sidef}}==
Line 3,529 ⟶ 3,799:
=={{header|Wren}}==
{{libheader|Wren-set}}
<syntaxhighlight lang="ecmascriptwren">import "./set" for Set
 
var symmetricDifference = Fn.new { |a, b| a.except(b).union(b.except(a)) }
57

edits