Set: Difference between revisions

48,518 bytes added ,  3 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(14 intermediate revisions by 10 users not shown)
Line 35:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">V s1 = Set([1, 2, 3, 4])
V s2 = Set([3, 4, 5, 6])
print(s1.union(s2))
Line 56:
print(s1)
s1.discard(99)
print(s1)</langsyntaxhighlight>
 
{{out}}
Line 83:
The user must type in the monitor the following command after compilation and before running the program!<pre>SET EndProg=*</pre>
{{libheader|Action! Tool Kit}}
<langsyntaxhighlight Actionlang="action!">CARD EndProg ;required for ALLOCATE.ACT
 
INCLUDE "D2:ALLOCATE.ACT" ;from the Action! Tool Kit. You must type 'SET EndProg=*' from the monitor after compiling, but before running this program!
Line 364:
Clear(s3)
Clear(s4)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Set.png Screenshot from Atari 8-bit computer]
Line 394:
An alternative hash-based solution could use the Hashed_Maps package from Ada.Containers.
 
<langsyntaxhighlight Adalang="ada">with ada.containers.ordered_sets, ada.text_io;
use ada.text_io;
 
Line 423:
end loop;
end set_demo;
</syntaxhighlight>
</lang>
 
{{out}}
Line 438:
 
=={{header|Aime}}==
<langsyntaxhighlight lang="aime">record
union(record a, record b)
{
Line 518:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre> banana is not an element of A
Line 531:
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
Reuses a lot of code from the Symetric difference task.
<langsyntaxhighlight lang="algol68"># sets using associative arrays #
# include the associative array code for string keys and values #
PR read "aArray.a68" PR
Line 683:
print( ( "... after restoration of Pluto: " ) );
all planets // "Pluto";
print set( all planets )</langsyntaxhighlight>
{{out}}
<pre>
Line 708:
In Apex, Sets are unordered collections of elements. Although elements can be anything including primitives, Ids, Apex classes, or sObjects, typically they are used with primitives and Ids.
 
<langsyntaxhighlight lang="apex">
public class MySetController{
public Set<String> strSet {get; private set; }
Line 738:
}
}
</syntaxhighlight>
</lang>
</pre>
 
Line 745:
macOS's Foundation framework offers a few classes of set which can be used in AppleScript by means of AppleScriptObjectiveC code. The basic types are <tt>NSSet</tt> and its mutable equivalent <tt>NSMutableSet</tt>.
 
<langsyntaxhighlight lang="applescript">use AppleScript version "2.4" -- OS X 10.10 (Yosemite) or later
use framework "Foundation"
--use scripting additions
Line 794:
end doSetTask
 
doSetTask()</langsyntaxhighlight>
 
{{out}}
 
<langsyntaxhighlight lang="applescript">"Set S: 0, 9, 1, 6, 2, 7, 3, 8
\"aardvark\" is a member of S: false
3 is a member of S: true
Line 810:
A is a subset of S: true
A is equal to B: false
A is equal to S: true"</langsyntaxhighlight>
 
=={{header|Arturo}}==
<langsyntaxhighlight lang="rebol">a: [1 2 3 4]
b: [3 4 5 6]
 
Line 834:
print superset?.proper a [1 3]
print superset? [1 3] [1 3]
print superset?.proper [1 3] [1 3]</langsyntaxhighlight>
 
{{out}}
Line 859:
 
You might notice the operator overloading.
 
<lang ATS>(*------------------------------------------------------------------*)
(Aside: If one were going to write an ATS implementation of sets as linked lists, it might be well to base the implementation on the reference code for Scheme SRFI-1: [https://srfi.schemers.org/srfi-1/srfi-1-reference.scm]. This is particularly so if an '''eq?'''-like function is possible for set members.)
 
<syntaxhighlight lang="ats">(*------------------------------------------------------------------*)
 
#define ATS_DYNLOADFLAG 0
Line 1,595 ⟶ 1,598:
println! ("set2 = set1: ", set2 = set1);
println! ("(set1 ^ set2) = (set2 ^ set1): ",
(set1 ^ set2) = (set2 ^ set1));
println! ("(set1 ^ set2) = set1: ", (set1 ^ set2) = set1);
println! ("(set1 ^ set2) = set2: ", (set1 ^ set2) = set2)
end
 
(*------------------------------------------------------------------*)</langsyntaxhighlight>
 
{{out}}
Line 1,638 ⟶ 1,643:
set1 = set2: false
set2 = set1: false
(set1 ^ set2) = (set2 ^ set1): true</pre>
(set1 ^ set2) = set1: false
 
(set1 ^ set2) = set2: false</pre>
 
 
=={{header|AutoHotkey}}==
<langsyntaxhighlight AutoHotkeylang="autohotkey">test(Set,element){
for i, val in Set
if (val=element)
Line 1,693 ⟶ 1,698:
equal(SetA,SetB){
return (SetA.MaxIndex() = SetB.MaxIndex() && subset(SetA,SetB)) ? 1: 0
}</langsyntaxhighlight>
Examples:<langsyntaxhighlight AutoHotkeylang="autohotkey">A:= ["apple", "cherry", "elderberry", "grape"]
B:= ["banana", "cherry", "date", "elderberry", "fig"]
C:= ["apple", "cherry", "elderberry", "grape", "orange"]
Line 1,735 ⟶ 1,740:
Res .= "`nA is " (equal(A,E)?"":"not ") "a equal to Set E"
 
MsgBox % Res</langsyntaxhighlight>
{{out}}
<pre>A:= ["apple", "cherry", "elderberry", "grape"]
Line 1,758 ⟶ 1,763:
A is not a equal to Set E</pre>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
The sets are represented as 32-bit integers, which means that the maximum number of elements is 32.
<langsyntaxhighlight lang="bbcbasic"> DIM list$(6)
list$() = "apple", "banana", "cherry", "date", "elderberry", "fig", "grape"
Line 1,802 ⟶ 1,808:
IF set% AND 1 << i% o$ += list$(i%) + ", "
NEXT
= LEFT$(LEFT$(o$))</langsyntaxhighlight>
{{out}}
<pre>
Line 1,824 ⟶ 1,830:
BQN can use lists with only unique elements to represent sets. The following implement all the basic set relations.
 
<langsyntaxhighlight lang="bqn">Union ← ⍷∾
Inter ← ∊/⊣
Diff ← ¬∘∊/⊣
Line 1,836 ⟶ 1,842:
•Show 2‿4‿6‿8 Subset 2‿3‿5‿7
•Show 2‿4‿6‿8 Eq 2‿3‿5‿7
•Show CreateSet 2‿2‿3‿5‿7‿2</langsyntaxhighlight><syntaxhighlight lang="text">⟨ 2 4 6 8 3 5 7 ⟩
⟨ 2 ⟩
⟨ 4 6 8 ⟩
0
0
⟨ 2 3 5 7 ⟩</langsyntaxhighlight>
 
=={{header|C}}==
Line 1,847 ⟶ 1,853:
 
A frequent use of set is that of small, non-negative integers, implemented as a bit field as shown below.
<langsyntaxhighlight lang="c">#include <stdio.h>
 
typedef unsigned int set_t; /* probably 32 bits; change according to need */
Line 1,891 ⟶ 1,897:
 
return 0;
}</langsyntaxhighlight>
 
=={{header|C sharp|C#}}==
<langsyntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Linq;
Line 1,949 ⟶ 1,955:
Console.ReadKey();
}
}</langsyntaxhighlight>
{{out}}
<pre>Set creation
Line 1,979 ⟶ 1,985:
C++11 standard library also contains unordered_set based on a hash table. However, algorithms like std::set_intersection etc take sorted ranges, so set-specific functions should be hand-rolled.
 
<langsyntaxhighlight lang="cpp">
#include <set>
#include <iostream>
Line 2,059 ⟶ 2,065:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|Ceylon}}==
<langsyntaxhighlight lang="ceylon">shared void run() {
value a = set {1, 2, 3};
value b = set {3, 4, 5};
Line 2,079 ⟶ 2,085:
a subset of b? ``subset``
a == b? ``equality``");
}</langsyntaxhighlight>
 
=={{header|Clojure}}==
 
<langsyntaxhighlight lang="clojure">(require 'clojure.set)
 
; sets can be created using the set method or set literal syntax
Line 2,097 ⟶ 2,103:
(clojure.set/difference a b)
 
(clojure.set/subset? a b)</langsyntaxhighlight>
 
=={{header|CoffeeScript}}==
This implements functions from the task, along with an iteration helper called "each".
<langsyntaxhighlight lang="coffeescript">
# For ad-hoc set features, it sometimes makes sense to use hashes directly,
# rather than abstract to this level, but I'm showing a somewhat heavy
Line 2,187 ⟶ 2,193:
 
run_tests()
</syntaxhighlight>
</lang>
 
=={{header|Common Lisp}}==
Common Lisp provides some set operations on lists.
<langsyntaxhighlight lang="lisp">(setf a '(1 2 3 4))
(setf b '(2 3 4 5))
 
Line 2,212 ⟶ 2,218:
(subsetp b a))
"~a = ~a~%"
"~a ≠ ~a~%") a b)</langsyntaxhighlight>
 
=={{header|D}}==
<langsyntaxhighlight lang="d">void main() {
import std.stdio, std.algorithm, std.range;
 
Line 2,235 ⟶ 2,241:
const s5 = [1, 1, 1, 4, 4, 7, 7, 7, 7, 8, 8];
assert(s4.nWayUnion.equal(s5));
}</langsyntaxhighlight> =={{header|D}}==
<syntaxhighlight lang="d">
<lang d>
module set;
import std.typecons : Tuple, tuple;
Line 2,389 ⟶ 2,395:
return true;
}
</syntaxhighlight>
</lang>
 
=={{header|Dart}}==
<langsyntaxhighlight lang="d">void main(){
//Set Creation
Set A = new Set.from([1,2,3]);
Line 2,429 ⟶ 2,435:
print('A is equal to B = ${B.containsAll(A) && A.containsAll(B)}');
print('B is equal to AC = ${B.containsAll(AC) && AC.containsAll(B)}');
}</langsyntaxhighlight>
{{out}}
<pre>Set A = {1, 2, 3}
Line 2,456 ⟶ 2,462:
{{libheader| Boost.Generics.Collection}}
The library [https://github.com/MaiconSoft/DelphiBoostLib Boost.Generics.Collection].
<syntaxhighlight lang="delphi">
<lang Delphi>
program Set_task;
 
Line 2,481 ⟶ 2,487:
Writeln('S1 equality S2? ', s1 = s2);
readln;
end.</langsyntaxhighlight>
{{out}}
<pre>S1 { 1, 2, 3, 4, 5, 6 }
Line 2,493 ⟶ 2,499:
S3 is subset S2 TRUE
S1 equality S2? FALSE</pre>
 
=={{header|Diego}}==
<syntaxhighlight lang="diego">use_namespace(rosettacode)_me();
 
// Set creation
add_set(A)_values(🐖,🦬,🦘,🦫,🦭);
add_set(B)_values(🐈‍⬛,🦬,🦫,🦤,🐐);
add_set(C)_values(🐈‍⬛,🦫);
add_set(M)_value(🐖);
 
// Membership
ms_msg()_calc([M]∈[B])
? with_msg()_msg(set M is an element in set B);
: with_msg()_msg(set M is not an element in set B);
;
 
ms_msg()_calc(🐖∈[A])
? with_msg()_msg(🐖 is an element in set A);
: with_msg()_msg(🐖 is not an element in set A);
;
 
// Union
ms_msg()_msg(A∪B=[])_calc([A]∪[B]);
 
// Intersection
ms_msg()_msg(A∩B=[])_calc([A]∩[B]);
 
// Difference
ms_msg()_msg(A∖B=[])_calc([A]∖[B]); // U+2216 is used not U+005c (\)
ms_msg()_msg(A\\B=[])_calc([A]\\[B]); // U+005c (\) has to be escaped
 
// Subset
ms_msg()_calc([C]⊆[A])
? with_msg()_msg(set C is a subset of set A);
: with_msg()_msg(set C is not a subset of set A);
;
 
ms_msg()_calc([C]⊆[B])
? with_msg()_msg(set C is a subset of set B);
: with_msg()_msg(set C is not a subset of set B);
;
 
// Equality
ms_msg()_calc([A]=[B])
? with_msg()_msg(set A is equal to set B);
: with_msg()_msg(set A is not equal to set B);
;
 
// Test
ms_msg()_calc([A]⊂[B])_or()_calc([A]⊊[B])
? with_msg()_msg(set A is a proper subset of set B);
: with_msg()_msg(set A is not a proper subset of set B);
;
 
ms_msg()_calc([C]⊂[B]||[C]⊊[B]) // alternative syntax
? with_msg()_msg(set C is a proper subset of set B);
: with_msg()_msg(set C is not a proper subset of set B);
;
 
// Modify a mutable set (all sets are mutable)
with_set(M)_push(🦬,🦘,🦫,🦭);
ms_msg()_calc([M]=[A])
? with_msg()_msg(set M is equal to set A);
: with_msg()_msg(set M is not equal to set A);
;
 
reset_namespace[];</syntaxhighlight>
 
{{out}}
 
<pre>set M is an element in set B
🐖 is an element in set A
A∪B=🐖,🦬,🦘,🦫,🦭,🐈‍⬛,🦤,🐐
A∩B=🦬,🦫
A∖B=🐖,🦘,🦭
A\B=🐖,🦘,🦭
set C is not a subset of set A
set C is a subset of set B
set A is not equal to set B
set A is not a proper subset of set B
set C is a proper subset of set B
set M is equal to set A</pre>
 
=={{header|EchoLisp}}==
Line 2,498 ⟶ 2,586:
 
The set operations are: ∩ ∪ ⊆ / ∈ = ∆ ×
<langsyntaxhighlight lang="lisp">
; use { } to read a set
(define A { 1 2 3 4 3 5 5}) → { 1 2 3 4 5 } ; duplicates are removed from a set
Line 2,532 ⟶ 2,620:
; A few functions return sets :
(primes 10) → { 2 3 5 7 11 13 17 19 23 29 }
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
{{works with|Elixir|1.1}}
<langsyntaxhighlight lang="elixir">iex(1)> s = MapSet.new
#MapSet<[]>
iex(2)> sa = MapSet.put(s, :a)
Line 2,563 ⟶ 2,651:
false
iex(14)> sa == sab
false</langsyntaxhighlight>
 
=={{header|Erlang}}==
Line 2,590 ⟶ 2,678:
=={{header|F_Sharp|F#}}==
The Collections.Set<'T> class implements "Immutable sets based on binary trees, where comparison is the F# structural comparison function, potentially using implementations of the IComparable interface on key values." (http://msdn.microsoft.com/en-us/library/ee353619.aspx)
<langsyntaxhighlight lang="fsharp">[<EntryPoint>]
let main args =
// Create some sets (of int):
Line 2,615 ⟶ 2,703:
printfn "s1 ⊂ s1? %A" (Set.isProperSubset s1 s1)
printfn "s1 ⊂ s2? %A" (Set.isProperSubset s1 s2)
0</langsyntaxhighlight>
{{out}}
<pre>Some sets (of int):
Line 2,639 ⟶ 2,727:
=={{header|Factor}}==
We will use Factor's hash-sets for this task. A hash-set is created with <code>HS{ ... }</code>.
<langsyntaxhighlight lang="factor">( scratchpad ) USE: sets
( scratchpad ) HS{ 2 5 4 3 } HS{ 5 6 7 } union .
HS{ 2 3 4 5 6 7 }
Line 2,653 ⟶ 2,741:
f
( scratchpad ) HS{ 6 5 7 } HS{ 5 6 7 } set= .
t</langsyntaxhighlight>
 
=={{header|Forth}}==
Line 2,660 ⟶ 2,748:
Needs the FMS-SI (single inheritance) library code located here:
http://soton.mpeforth.com/flag/fms/index.html
<langsyntaxhighlight lang="forth">include FMS-SI.f
include FMS-SILib.f
 
Line 2,717 ⟶ 2,805:
i{ 5 6 } i{ 5 6 7 } set= . 0 ok
i{ 6 5 7 } i{ 5 6 7 } set= . -1 ok
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">function is_in( N as integer, S() as integer ) as boolean
'test if the value N is in the set S
for i as integer = 0 to ubound(S)
Line 2,845 ⟶ 2,933:
print "S2 = S3? ", is_equal(S2(), S3())
print "S4 proper subset of S3? ", is_proper_subset( S4(), S3() )
print "S2 proper subset of S3? ", is_proper_subset( S2(), S3() )</langsyntaxhighlight>
{{out}}
<pre>
Line 2,866 ⟶ 2,954:
 
=={{header|Frink}}==
<langsyntaxhighlight lang="frink">
a = new set[1, 2]
b = toSet[[2,3]] // Construct a set from an array
Line 2,876 ⟶ 2,964:
isSubset[a,b] // Returns true if a is a subset of b
a==b // set equality test
</syntaxhighlight>
</lang>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">A = {1, 2, 3}
B = {3, 4, 5}
C = {1, 2, 3, 4, 5}
Line 2,901 ⟶ 2,989:
println( 'S subset of C: ' + S.subsetOf(C) )
S.remove( 1 )
println( 'S after 1 removed: ' + S )</langsyntaxhighlight>
 
{{out}}
Line 2,919 ⟶ 3,007:
S subset of C: true
S after 1 removed: {2, 3, 4}
</pre>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">include "NSLog.incl"
 
local fn DoIt
// create
CFSetRef s1 = fn SetWithArray( @[@"a",@"b",@"c",@"d",@"e"] )
CFSetRef s2 = fn SetWithArray( @[@"b",@"c",@"d",@"e",@"f",@"h"] )
CFSetRef s3 = fn SetWithArray( @[@"b",@"c",@"d"] )
CFSetRef s4 = fn SetWithArray( @[@"b",@"c",@"d"] )
NSLog(@"s1: %@",s1)
NSLog(@"s2: %@",s2)
NSLog(@"s3: %@",s3)
NSLog(@"s4: %@\n",s4)
 
// membership
NSLog(@"\"b\" in s1: %d", fn SetContainsObject( s1, @"b" ))
NSLog(@"\"f\" in s1: %d\n", fn SetContainsObject( s1, @"f" ))
 
// union
CFMutableSetRef s12 = fn MutableSetWithSet( s1 )
MutableSetUnionSet( s12, s2 )
NSLog(@"s1 union s2: %@\n", s12)
 
// intersection
CFMutableSetRef s1i2 = fn MutableSetWithSet( s1 )
MutableSetIntersectSet( s1i2, s2 )
NSLog(@"s1 intersect s2: %@\n", s1i2)
 
// difference
CFMutableSetRef s1d2 = fn MutableSetWithSet( s1 )
MutableSetMinusSet( s1d2, s2 )
NSLog(@"s1 - s2: %@\n", s1d2)
 
// subsetof
NSLog(@"s3 subset of s1: %d\n", fn SetIsSubsetOfSet( s3, s1 ))
 
// equality
NSLog(@"s3 == s4: %d\n", fn SetIsEqual( s3, s4 ))
 
// cardinality
NSLog(@"size of s1: %lu\n", fn SetCount(s1))
 
// has intersection (not disjoint)
NSLog(@"s1 intersects s2: %d\n", fn SetIntersectsSet( s1, s2 ))
 
// adding and removing elements from mutable set
CFMutableSetRef s1mut = fn MutableSetWithSet( s1 )
MutableSetAddObject( s1mut, @"g" )
NSLog(@"s1mut after adding \"g\": %@\n", s1mut)
MutableSetAddObject( s1mut, @"b" )
NSLog(@"s1mut after adding \"b\" again: %@\n", s1mut)
MutableSetRemoveObject( s1mut, @"c" )
NSLog(@"s1mut after removing \"c\": %@\n", s1mut)
end fn
 
fn DoIt
 
HandleEvents</syntaxhighlight>
 
{{out}}
<pre>
s1: {(
d,
b,
e,
c,
a
)}
s2: {(
d,
b,
e,
c,
h,
f
)}
s3: {(
b,
c,
d
)}
s4: {(
b,
c,
d
)}
 
"b" in s1: 1
"f" in s1: 0
 
s1 union s2: {(
d,
b,
f,
e,
c,
a,
h
)}
 
s1 intersect s2: {(
d,
b,
e,
c
)}
 
s1 - s2: {(
a
)}
 
s3 subset of s1: 1
 
s3 == s4: 1
 
size of s1: 5
 
s1 intersects s2: 1
 
s1mut after adding "g": {(
d,
b,
g,
e,
c,
a
)}
 
s1mut after adding "b" again: {(
d,
b,
g,
e,
c,
a
)}
 
s1mut after removing "c": {(
d,
b,
g,
e,
a
)}
</pre>
 
Line 2,925 ⟶ 3,159:
===Maps===
Go maps meet the task description in that they do not require orderable elements. To demonstrate that, a set of complex numbers is shown here. Complex numbers can be compared for equality but are not ordered.
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 3,055 ⟶ 3,289:
func properSubset(a, b set) bool {
return len(a) < len(b) && subset(a, b)
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,080 ⟶ 3,314:
 
Note that the elements here, being integers, are of course ordered and so might not meet a strict reading of the task requirements.
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,212 ⟶ 3,446:
}
return
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,235 ⟶ 3,469:
===Intsets===
Not quite in the stanard library but still in the official "sub repository", intsets are a ''sparse'' bit set. Like big.Int they use a single bit to represent a possible element, but the sparse representation efficiently allows for large "holes" in the element sequence. Also the intsets API provides a more set-like terminology so the RC task can be coded more directly.
<langsyntaxhighlight lang="go">package main
 
import (
Line 3,294 ⟶ 3,528:
}
return &set
}</langsyntaxhighlight>
{{out}}
<pre>
Line 3,315 ⟶ 3,549:
 
=={{header|Groovy}}==
<langsyntaxhighlight lang="groovy">def s1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] as Set
def m1 = 6
def m2 = 7
Line 3,333 ⟶ 3,567:
assert s1 != su && su.containsAll(s1) : 'proper subset'
s1 << 0
assert s1 == su : 'added element 0 to s1'</langsyntaxhighlight>
 
=={{header|Haskell}}==
{{works with|GHC}}
GHC offers a functional, persistent set data structure in its <code>Data.Set</code> module. It is implemented using a binary search tree. Elements must be of an orderable type (instance of <code>Ord</code>).
<langsyntaxhighlight lang="haskell">Prelude> import Data.Set
Prelude Data.Set> empty :: Set Integer -- Empty set
fromList []
Line 3,372 ⟶ 3,606:
fromList [1,2,3,4,99]
Prelude Data.Set> delete 3 s1 -- Create a new set by deleting
fromList [1,2,4]</langsyntaxhighlight>
 
Regular lists can also be used as sets. Haskell has some functions to help with using lists as sets. No requirement is made of element type. However, these are not very efficient because they require linear time to find an element.
<langsyntaxhighlight lang="haskell">Prelude> import Data.List
Prelude Data.List> let s3 = nub [1,2,3,4,3] -- Remove duplicates from list
Prelude Data.List> s3
Line 3,389 ⟶ 3,623:
[42,1,2,3,4]
Prelude Data.List> delete 3 s3 -- Return new list with first occurrence of element removed
[1,2,4]</langsyntaxhighlight>
 
=={{header|Icon}} and {{header|Unicon}}==
Line 3,397 ⟶ 3,631:
Implemented directly:
 
<langsyntaxhighlight lang="unicon">
procedure display_set (s)
writes ("[")
Line 3,459 ⟶ 3,693:
write ("(1,2,5) is not included in a")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,478 ⟶ 3,712:
Using library:
 
<langsyntaxhighlight lang="unicon">
link sets
 
Line 3,518 ⟶ 3,752:
write ("(1,2,5) is not included in a")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 3,541 ⟶ 3,775:
Here are definitions for the required operations:
 
<langsyntaxhighlight lang="j">union=: ~.@,
intersection=: [ -. -.
difference=: -.
subset=: *./@e.
equality=: -:&(/:~)</langsyntaxhighlight>
 
Examples:
 
<langsyntaxhighlight lang="j"> 2 4 6 8 ~.@, 2 3 5 7
2 4 6 8 3 5 7
2 4 6 8 ([ -. -.) 2 3 5 7
Line 3,560 ⟶ 3,794:
1
2 4 6 8 3 5 7 -:&(/:~) 8 7 6 5 4 3 2
1</langsyntaxhighlight>
 
Examples again, using names rather than code:
 
<langsyntaxhighlight lang="j"> 2 4 6 8 union 2 3 5 7
2 4 6 8 3 5 7
2 4 6 8 intersection 2 3 5 7
Line 3,575 ⟶ 3,809:
1
2 4 6 8 3 5 7 equality 8 7 6 5 4 3 2
1</langsyntaxhighlight>
 
Note that J uses 1 for true and 0 for false. Mathematical revisionists object to this, but this is consistent with the original (and revised) formulations of boolean algebra. (And there are deep ties to Bayes' rule.)
Line 3,581 ⟶ 3,815:
Note that these operations can be combined in sentences with other operations. For example we could define
 
<langsyntaxhighlight lang="j">properSubset=: subset * 1 - equality</langsyntaxhighlight>
 
=={{header|Java}}==
{{works with|Java|7+}}
To use this in Java 5 replace all "<>" with "<Integer>".
<langsyntaxhighlight lang="java5">import java.util.Arrays;
import java.util.Collections;
import java.util.Set;
Line 3,647 ⟶ 3,881:
Collections.unmodifiableSet(a); //returns an immutable copy of a
}
}</langsyntaxhighlight>
{{out}}
<pre>a: [1, 2, 3, 4, 5]
Line 3,667 ⟶ 3,901:
JavaScript does not support native sets before ECMAScript 6.
 
<langsyntaxhighlight lang="javascript">
var set = new Set();
 
Line 3,691 ⟶ 3,925:
for (var item of set) {
console.log('item is ' + item);
}</langsyntaxhighlight>
 
=={{header|jq}}==
Line 3,711 ⟶ 3,945:
 
For example:
<langsyntaxhighlight lang="jq">{"a":true, "b":true } == {"b":true, "a":true}.
{"a":true} + {"b":true } == { "a":true, "b":true}</langsyntaxhighlight>
 
Thus, it can be seen that jq has built-in support for sets of finite-length Unicode strings.
Line 3,721 ⟶ 3,955:
 
Here is a jq filter for determining whether a JSON object is a "string set":
<langsyntaxhighlight lang="jq">def is_stringset:
. as $in | type == "object" and reduce keys[] as $key (true; . and $in[$key] == true);</langsyntaxhighlight>
 
'''String-set membership''':
Line 3,728 ⟶ 3,962:
The test for set membership, m ∈ S, where m is a string and S is a
set of strings, corresponds exactly to the jq test:
<syntaxhighlight lang ="jq">T | has(m)</langsyntaxhighlight>
where T is the JSON object corresponding to S. This test is also efficient.
 
'''String-set intersection'''
<langsyntaxhighlight lang="jq"># Set-intersection: A ∩ B
def stringset_intersection(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . + {($k):true} else . end);</langsyntaxhighlight>
 
'''String-set difference'''
<langsyntaxhighlight lang="jq"># stringset_difference: A \ B
def stringset_difference(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . else . + {($k):true} end);</langsyntaxhighlight>
 
'''Subset'''
<langsyntaxhighlight lang="jq"># A ⊆ B iff string_subset(A;B)
def stringset_subset(A;B):
reduce (A|keys)[] as $k
(true; . and (B|has($k)));</langsyntaxhighlight>
 
=== Finite Sets of JSON Entities ===
Line 3,768 ⟶ 4,002:
jq operator "unique". To test whether an arbitrary JSON entity is a set
without sorting:
<langsyntaxhighlight lang="jq">def is_set:
. as $in
| type == "array" and
reduce range(0;length-1) as $i
(true; if . then $in[$i] < $in[$i+1] else false end);</langsyntaxhighlight>
 
The following library of set-theoretic functions is intended for use
Line 3,791 ⟶ 4,025:
whether m is an element of S, but for large sets, this is inefficient. A generally more efficient test membership of m in S would use
bsearch as defined at [[Binary search]] or as provided in recent versions of jq:
<langsyntaxhighlight lang="jq">def is_member(m): bsearch(m) > -1;</langsyntaxhighlight>
 
'''Intersection'''
<langsyntaxhighlight lang="jq"># If A and B are sets, then intersection(A;B) emits their intersection:
def intersection($A;$B):
def pop:
Line 3,804 ⟶ 4,038:
else [$i, $j+1] | pop
end;
[[0,0] | pop];</langsyntaxhighlight>
'''Difference'''
<langsyntaxhighlight lang="jq"># If A and B are sets, then A-B is emitted
def difference(A;B):
(A|length) as $al
Line 3,824 ⟶ 4,058:
end
) | .[2]
end ;</langsyntaxhighlight>
 
'''Union'''
Line 3,831 ⟶ 4,065:
 
To compute the union of two sets efficiently, it is helpful to define a function for merging sorted arrays.
<langsyntaxhighlight lang="jq"># merge input array with array x by comparing the heads of the arrays in turn;
# if both arrays are sorted, the result will be sorted:
def merge(x):
Line 3,852 ⟶ 4,086:
def union(A;B):
A|merge(B)
| reduce .[] as $m ([]; if length == 0 or .[length-1] != $m then . + [$m] else . end);</langsyntaxhighlight>
 
'''A ⊆ B'''
<langsyntaxhighlight lang="jq">def subset(A;B):
# TCO
def _subset:
Line 3,864 ⟶ 4,098:
else [ .[0], .[1][1:] ] | _subset
end;
[A,B] | _subset;</langsyntaxhighlight>
 
'''Test whether two sets intersect'''
Line 3,872 ⟶ 4,106:
If A and B are sets (i.e. A == (A|unique) and B == (B|unique)),
then ''[A,B] | intersect'' emits true if A and B have at least one element in common:
<langsyntaxhighlight lang="jq">def intersect:
.[0] as $A | .[1] as $B
| ($A|length) as $al
Line 3,883 ⟶ 4,117:
end
end;
</syntaxhighlight>
</lang>
 
=={{header|Julia}}==
Line 3,912 ⟶ 4,146:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun main(args: Array<String>) {
Line 3,947 ⟶ 4,181:
println("fruits5 + 'guava' : $fruits5")
println("fruits5 - 'cherry' : ${fruits5 - "cherry"}")
}</langsyntaxhighlight>
 
{{out}}
Line 3,980 ⟶ 4,214:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">// Extend set type
define set->issubsetof(p::set) => .intersection(#p)->size == .size
define set->oncompare(p::set) => .intersection(#p)->size - .size
Line 4,004 ⟶ 4,238:
 
//A = B -- equality; true if every element of set A is in set B and vice-versa.
#set1 == #set2</langsyntaxhighlight>
 
{{out}}
Line 4,017 ⟶ 4,251:
{{trans|Erlang}}
 
<langsyntaxhighlight lang="lisp">
> (set set-1 (sets:new))
#(set 0 16 16 8 80 48 ...)
Line 4,046 ⟶ 4,280:
> (=:= set-3 set-4)
true
</syntaxhighlight>
</lang>
 
=={{header|Liberty BASIC}}==
Sets are not natively available- implemented here in string form so no need to dim/redim or pass number of elements.
<syntaxhighlight lang="lb">
<lang lb>
A$ ="red hot chili peppers rule OK"
B$ ="lady in red"
Line 4,200 ⟶ 4,434:
end function
</syntaxhighlight>
</lang>
Line 4,219 ⟶ 4,453:
=={{header|Lua}}==
{{works with|lua|5.1}}
<langsyntaxhighlight lang="lua">function emptySet() return { } end
function insert(set, item) set[item] = true end
function remove(set, item) set[item] = nil end
Line 4,287 ⟶ 4,521:
return subset(setA, setB) and (size(setA) == size(setB))
end
</syntaxhighlight>
</lang>
 
{{works with|lua|5.3}}
Line 4,296 ⟶ 4,530:
The code is intended to be placed into a file and accessed as a module. E.g. if placed into the file "set.lua" it would be accessed with <code>set = require 'set'</code>.
 
<langsyntaxhighlight lang="lua">local function new(_, ...)
local r = {}
local s = setmetatable({}, {
Line 4,406 ⟶ 4,640:
end
 
return setmetatable({}, { __call = new })</langsyntaxhighlight>
 
=={{header|M2000 Interpreter}}==
Line 4,412 ⟶ 4,646:
For search in a tuple we have O(N).
 
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module Sets {
setA=("apple", "cherry", "grape")
Line 4,486 ⟶ 4,720:
}
Sets
</syntaxhighlight>
</lang>
 
=={{header|Maple}}==
Sets in Maple are built-in, native data structures, and are immutable. Sets are formed by enclosing a sequence of objects between braces. You can get something essentially equivalent to set comprehensions by using {seq}, which applies the set constructor ("{}") to the sequencing operation "seq".
<syntaxhighlight lang="maple">
<lang Maple>
> S := { 2, 3, 5, 7, 11, Pi, "foo", { 2/3, 3/4, 4/5 } };
S := {2, 3, 5, 7, 11, "foo", Pi, {2/3, 3/4, 4/5}}
Line 4,532 ⟶ 4,766:
> evalb( { 1, 2, 3 } = { 1, 2, 4 } );
false
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">set1 = {"a", "b", "c", "d", "e"}; set2 = {"a", "b", "c", "d", "e", "f", "g"};
MemberQ[set1, "a"]
Union[set1 , set2]
Line 4,542 ⟶ 4,776:
MemberQ[Subsets[set2], set1](*Subset*)
set1 == set2(*Equality*)
set1 == set1(*Equality*)</langsyntaxhighlight>
{{out}}
<pre>True
Line 4,555 ⟶ 4,789:
 
There are two types of sets supported, sets with numeric values are stored in a vector, sets with string elements are stored in a cell-array.
<syntaxhighlight lang="matlab">
<lang Matlab>
% Set creation
s = [1, 2, 4]; % numeric values
Line 4,572 ⟶ 4,806:
% A = B -- equality; true if every element of set A is in set B and vice-versa.
isempty(setxor(A, B))
</syntaxhighlight>
</lang>
 
=={{header|Maxima}}==
<syntaxhighlight lang="maxima">/* illustrating some functions on sets; names are self-explanatory */
 
a: {1, 2, 3, 4};
Line 4,652 ⟶ 4,886:
 
setequalp(a, b);
false</langsyntaxhighlight>
 
=={{header|Nanoquery}}==
This is a full implementation of a set class.
<langsyntaxhighlight lang="nanoquery">class set
declare internal_list
 
Line 4,733 ⟶ 4,967:
return str(this.internal_list)
end
end</langsyntaxhighlight>
Testing this implementation:
<langsyntaxhighlight lang="nanoquery">import "rosetta-code/set.nq"
 
a = new(set, {1, 2, 3, 4, 5})
Line 4,759 ⟶ 4,993:
println "a intersect b: " + a.intersection(b)
println "add 7 to a: " + a.append(7)
println "add 2 to a again: " + a.append(2)</langsyntaxhighlight>
{{out}}
<pre>a: [1, 2, 3, 4, 5]
Line 4,778 ⟶ 5,012:
=={{header|Nemerle}}==
The <tt>Nemerle.Collections</tt> namespace provides an implementation of a Set.
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
using Nemerle.Collections;
 
Line 4,804 ⟶ 5,038:
WriteLine($"$same\t$sub12");
}
}</langsyntaxhighlight>
 
=={{header|Nim}}==
Line 4,811 ⟶ 5,045:
Here is an example of usage of a set.
 
<langsyntaxhighlight lang="nim">var # creation
s = {0, 3, 5, 10}
t = {3..20, 50..55}
Line 4,829 ⟶ 5,063:
 
s.incl(4) # add 4 to set
s.excl(5) # remove 5 from set</langsyntaxhighlight>
 
HashSets are not very different, less efficient but more versatile. Here is the same program with HashSets. The only difference is the way to create a HashSet: this is done by converting an array or sequence to the HashSet. As there is no way to specify a range in an array, we use a conversion of range to sequence with “toSeq” and a union.
 
<langsyntaxhighlight Nimlang="nim">import sequtils, sets
 
var # creation
Line 4,853 ⟶ 5,087:
 
s.incl(4) # add 4 to set
s.excl(5) # remove 5 from set</langsyntaxhighlight>
 
Note that there exists also an Ordered constructor which remembers the insertion order. And for sets of integers, the module “intsets” provides another constructor better suited for sparse integer sets.
 
=={{header|Objective-C}}==
<langsyntaxhighlight lang="objc">#import <Foundation/Foundation.h>
 
int main (int argc, const char *argv[]) {
Line 4,914 ⟶ 5,148:
}
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,996 ⟶ 5,230:
In the interactive toplevel:
{{works with|OCaml|4.02+}}
<langsyntaxhighlight lang="ocaml"># module IntSet = Set.Make(struct type t = int let compare = compare end);; (* Create a module for our type of set *)
module IntSet :
sig
Line 5,063 ⟶ 5,297:
- : IntSet.elt list = [1; 2; 3; 4; 99]
# IntSet.elements (IntSet.remove 3 s1);; (* Create a new set by deleting *)
- : IntSet.elt list = [1; 2; 4]</langsyntaxhighlight>
 
(Note: <code>of_list</code> is only available in OCaml 4.02+. In earlier versions, you can implement one yourself like
Line 5,073 ⟶ 5,307:
 
=={{header|Ol}}==
<langsyntaxhighlight lang="scheme">
; test set
(define set1 '(1 2 3 4 5 6 7 8 9))
Line 5,105 ⟶ 5,339:
(print (equal? set1 set4))
; ==> #true
</syntaxhighlight>
</lang>
 
=={{header|ooRexx}}==
<langsyntaxhighlight ooRexxlang="oorexx">-- Set creation
-- Using the OF method
s1 = .set~of(1, 2, 3, 4, 5, 6)
Line 5,139 ⟶ 5,373:
Use Arg set_name,set
Say set_name':' set~makearray~makestring((LINE),',')
return</langsyntaxhighlight>
The set operators don't come out too well :-(
{{out}}
Line 5,151 ⟶ 5,385:
=={{header|PARI/GP}}==
Aside from ⊆, all operations are already a part of GP.
<langsyntaxhighlight lang="parigp">setsubset(s,t)={
for(i=1,#s,
if(!setsearch(t,s[i]), return(0))
Line 5,164 ⟶ 5,398:
setminus(s,t)
setsubset(s,t)
s==t</langsyntaxhighlight>
 
{{out}}
Line 5,182 ⟶ 5,416:
--[[User:Guionardo|Guionardo]] 22:03, 7 January 2012 (UTC)
 
<langsyntaxhighlight lang="pascal">program Rosetta_Set;
 
{$mode objfpc}{$H+}
Line 5,275 ⟶ 5,509:
readln;
 
end.</langsyntaxhighlight>
 
=={{header|Perl}}==
For real code, try Set::Object from CPAN. Here we provide a primitive implementation using hashes.
<langsyntaxhighlight lang="perl">use strict;
 
package Set; # likely will conflict with stuff on CPAN
Line 5,391 ⟶ 5,625:
print "w = x ^ y = ", $w = ($x ^ $y), "\n";
print "w is ", ($w == $z) ? "" : "not ", "equal to z\n";
print "w is ", ($w == $x) ? "" : "not ", "equal to x\n";</langsyntaxhighlight>
 
=={{header|Phix}}==
First, a simple implementaion using native sequences:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>sequence set1 = {1,2,3},
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
set2 = {3,4,5}
<span style="color: #008080;">function</span> <span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">return</span> <span style="color: #7060A8;">find</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span>
function element(object x, sequence set)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return find(x,set)!=0
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">set_union</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
function union(sequence set1, sequence set2)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(set2) do
<span style="color: #000000;">set1</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
if not element(set2[i],set1) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
set1 = append(set1,set2[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">set1</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return set1
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">set_intersection</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function intersection(sequence set1, sequence set2)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence res = {}
<span style="color: #008080;">if</span> <span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(set1) do
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
if element(set1[i],set2) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res = append(res,set1[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return res
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">set_difference</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
function difference(sequence set1, sequence set2)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
sequence res = {}
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(set1) do
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">])</span>
if not element(set1[i],set2) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
res = append(res,set1[i])
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return res
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">set_subset</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
function subset(sequence set1, sequence set2)
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
for i=1 to length(set1) do
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
if not element(set1[i],set2) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return false
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end if
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return true
end function
<span style="color: #008080;">function</span> <span style="color: #000000;">set_equality</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
 
<span style="color: #008080;">if</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)!=</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
function equality(sequence set1, sequence set2)
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
if length(set1)!=length(set2) then
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return false
<span style="color: #008080;">return</span> <span style="color: #000000;">set_subset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
return subset(set1,set2)
end function
<span style="color: #000080;font-style:italic;">--test code:</span>
 
<span style="color: #0000FF;">?</span><span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 1</span>
--test code:
<span style="color: #0000FF;">?</span><span style="color: #000000;">is_element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 0</span>
?element(3,set1) -- 1
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_union</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {1,2,3,4,5}</span>
?element(4,set1) -- 0
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_intersection</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {3}</span>
?union(set1,set2) -- {1,2,3,4,5}
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_difference</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- {1,2}</span>
?intersection(set1,set2) -- {3}
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_subset</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 0</span>
?difference(set1,set2) -- {1,2}
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_subset</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 1</span>
?subset(set1,set2) -- 0
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_equality</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 0</span>
?subset({1,2},set1) -- 1
<span style="color: #0000FF;">?</span><span style="color: #000000;">set_equality</span><span style="color: #0000FF;">({</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">},{</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">})</span> <span style="color: #000080;font-style:italic;">-- 1</span>
?equality(set1,set2) -- 0
<!--</syntaxhighlight>-->
?equality(set1,{3,1,2}) -- 1</lang>
{{out}}
<small>Note that you get true/false instead of 1/0 under pwa/p2js - use printf(1,"%t\n",...) in place of ? to get an exact match.</small>
<pre>
1
Line 5,471 ⟶ 5,706:
Alternative using dictionaries, which needs several additional visitor routines (at a pinch they could be merged),
but performance is better on larger sets:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>integer set1 = new_dict(),
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
set2 = new_dict()
<span style="color: #008080;">function</span> <span style="color: #000000;">create_set</span><span style="color: #0000FF;">(</span><span style="color: #004080;">sequence</span> <span style="color: #000000;">s</span><span style="color: #0000FF;">={})</span>
setd(3,0,set1)
<span style="color: #004080;">integer</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
setd(1,0,set1)
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
setd(2,0,set1)
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">],</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
setd(5,0,set2)
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
setd(3,0,set2)
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
setd(4,0,set2)
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">x</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">(</span><span style="color: #000000;">x</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set</span><span style="color: #0000FF;">)!=</span><span style="color: #000000;">0</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">u_visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">user_data</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">union_set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">user_data</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span>
<span style="color: #008080;">or</span> <span style="color: #008080;">not</span> <span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">union_set</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">union_set</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">set_union</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">union_set</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u_visitor</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">union_set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">},</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">u_visitor</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">union_set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">union_set</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">i_visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">user_data</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">inter_sect</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">user_data</span>
<span style="color: #008080;">if</span> <span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">inter_sect</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">set_intersection</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">inter_sect</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">i_visitor</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">inter_sect</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">inter_sect</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">d_visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">user_data</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">diff_set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">}</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">user_data</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">data</span><span style="color: #0000FF;">,</span><span style="color: #000000;">diff_set</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">set_difference</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">diff_set</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">()</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">d_visitor</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">diff_set</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">},</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">diff_set</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #004080;">bool</span> <span style="color: #000000;">subset_res</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">s_visitor</span><span style="color: #0000FF;">(</span><span style="color: #004080;">object</span> <span style="color: #000000;">key</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">data</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">object</span> <span style="color: #000000;">user_data</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">user_data</span>
<span style="color: #008080;">if</span> <span style="color: #008080;">not</span> <span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">key</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #000000;">subset_res</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">0</span> <span style="color: #000080;font-style:italic;">-- cease traversal</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">1</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">subset</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">subset_res</span> <span style="color: #0000FF;">=</span> <span style="color: #004600;">true</span>
<span style="color: #7060A8;">traverse_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">s_visitor</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">subset_res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">equality</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">if</span> <span style="color: #7060A8;">dict_size</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)!=</span><span style="color: #7060A8;">dict_size</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">then</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">false</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">subset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">/</span><span style="color: #000000;">map</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span> <span style="color: #000080;font-style:italic;">-- for keys()
-- matching test code:</span>
<span style="color: #004080;">integer</span> <span style="color: #000000;">set1</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_set</span><span style="color: #0000FF;">({</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">set2</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_set</span><span style="color: #0000FF;">({</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">4</span><span style="color: #0000FF;">}),</span>
<span style="color: #000000;">set3</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">create_set</span><span style="color: #0000FF;">({</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">1</span><span style="color: #0000FF;">})</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 1</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">element</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 0</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">keys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set_union</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- {1,2,3,4,5}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">keys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set_intersection</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- {3}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">keys</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set_difference</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- {1,2}</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">subset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 0</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">subset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 1</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">equality</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set2</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 0</span>
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set3</span><span style="color: #0000FF;">)</span>
<span style="color: #0000FF;">?</span><span style="color: #000000;">equality</span><span style="color: #0000FF;">(</span><span style="color: #000000;">set1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">set3</span><span style="color: #0000FF;">)</span> <span style="color: #000080;font-style:italic;">-- 1</span>
<!--</syntaxhighlight>-->
Same output as above. I have written a builtins/sets.e, and tried another approach with builtins/sets2.e, but neither really hit the spot.
 
=={{header|Phixmonti}}==
function element(object x, integer set)
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
return getd_index(x,set)!=0
end function
 
def isElement find enddef
function u_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if set2=0
or not element(key,union_set) then
setd(key,data,union_set)
end if
return 1
end function
 
def setUnion dup >ps remove ps> chain enddef
function union(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("u_visitor"),{union_set,0},set1)
traverse_dict(routine_id("u_visitor"),{union_set,set2},set2)
return union_set
end function
 
def setIntersection over >ps remove ps> swap remove enddef
function i_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if element(key,set2) then
setd(key,data,union_set)
end if
return 1
end function
 
def setDifference remove enddef
function intersection(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("i_visitor"),{union_set,set2},set1)
return union_set
end function
 
def setSubset swap remove len not nip enddef
function d_visitor(object key, object data, object user_data)
integer {union_set,set2} = user_data
if not element(key,set2) then
setd(key,data,union_set)
end if
return 1
end function
 
def setEquality sort swap sort == enddef
function difference(integer set1, integer set2)
integer union_set = new_dict()
traverse_dict(routine_id("d_visitor"),{union_set,set2},set1)
return union_set
end function
 
( 1 2 3 ) 1 isElement ?
integer res
4 isElement ?
function s_visitor(object key, object data, object user_data)
( 3 4 5 ) setUnion ?
integer set2 = user_data
( 1 2 3 ) ( 3 4 5 ) setIntersection ?
if not element(key,set2) then
( 1 2 3 ) ( 3 4 res5 =) 0setDifference ?
( 1 2 3 ) ( 3 4 return5 0) --setSubset cease traversal?
( 1 2 3 ) ( 1 2 ) setSubset ?
end if
( 1 2 3 ) ( 3 4 5 ) setEquality ?
return 1
( 1 2 3 ) ( 3 1 2 ) setEquality ?
end function
 
</syntaxhighlight>
function subset(integer set1, integer set2)
{{out}}
res = 1
<pre>1
traverse_dict(routine_id("s_visitor"),set2,set1)
0
return res
[1, 2, 3, 4, 5]
end function
[3]
 
[1, 2]
function equality(integer set1, integer set2)
0
if dict_size(set1)!=dict_size(set2) then
1
return false
0
end if
1
return subset(set1,set2)
end function
 
include builtins/map.e -- for keys()
 
=== Press any key to exit ===</pre>
-- matching test code:
?element(3,set1) -- 1
?element(4,set1) -- 0
?keys(union(set1,set2)) -- {1,2,3,4,5}
?keys(intersection(set1,set2)) -- {3}
?keys(difference(set1,set2)) -- {1,2}
?subset(set1,set2) -- 0
integer set3 = new_dict()
setd(2,0,set3)
setd(1,0,set3)
?subset(set3,set1) -- 1
?equality(set1,set2) -- 0
setd(3,0,set3)
?equality(set1,set3) -- 1</lang>
same output as above
 
=={{header|PicoLisp}}==
We may use plain lists, or '[http://software-lab.de/doc/refI.html#idx idx]' structures for sets. A set may contain any type of data.
===Using lists===
<langsyntaxhighlight PicoLisplang="picolisp">(setq
Set1 (1 2 3 7 abc "def" (u v w))
Set2 (2 3 5 hello (x y z))
Line 5,617 ⟶ 5,894:
 
: (= (sort (copy Set2)) (sort (copy Set2)))
-> T</langsyntaxhighlight>
===Using 'idx' structures===
<langsyntaxhighlight PicoLisplang="picolisp"># Create three test-sets
(balance 'Set1 (1 2 3 7 abc "def" (u v w)))
(balance 'Set2 (2 3 5 hello (x y z)))
Line 5,675 ⟶ 5,952:
 
: (= (idx 'Set2) (idx 'Set2))
-> T</langsyntaxhighlight>
 
=={{header|PowerShell}}==
Line 5,682 ⟶ 5,959:
When used in PowerShell, the syntax is clumsy. In addition, the "reference" set (<code>$set1</code>) is modified in place to become the result.
(All examples assume the variable <code>$set1</code> contains the value <code>@(1,2,3,4)</code>)
<syntaxhighlight lang="powershell">
<lang PowerShell>
[System.Collections.Generic.HashSet[object]]$set1 = 1..4
[System.Collections.Generic.HashSet[object]]$set2 = 3..6
Line 5,700 ⟶ 5,977:
5 -in $set1 # Test membership False
7 -notin $set1 # Test non-membership True
</syntaxhighlight>
</lang>
 
=={{header|Prolog}}==
Works with SWI-Prolog, library(lists).
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(lists)).
 
set :-
Line 5,753 ⟶ 6,030:
select(H1, B, B1),
equal(T1, B1).
</syntaxhighlight>
</lang>
{{out}}
<pre> ?- set.
Line 5,782 ⟶ 6,059:
It treats sets as ordered lists of unique elements:
 
<langsyntaxhighlight lang="prolog">
%% Set creation
 
Line 5,838 ⟶ 6,115:
?- ord_del_element($NewA, 3, NewerA).
NewerA = [1, 2, 4, 19].
</syntaxhighlight>
</lang>
 
=={{header|PureBasic}}==
This solution uses PureBasic's maps (hash tables).
<langsyntaxhighlight lang="purebasic">Procedure.s booleanText(b) ;returns 'True' or 'False' for a boolean input
If b: ProcedureReturn "True": EndIf
ProcedureReturn "False"
Line 6,011 ⟶ 6,288:
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</langsyntaxhighlight>
{{out}}
<pre>Set A = (blue green orange red yellow) of cardinality 5.
Line 6,041 ⟶ 6,318:
(For versions prior to 2.7, use <code>set([1, 2, 3, 4])</code> instead of <code>{1, 2, 3, 4}</code>. Even in Python 2.7+ and 3.0+, it is necessary to write <code>set()</code> to express the empty set.)
{{works with|Python|2.7+ and 3.0+}}
<langsyntaxhighlight lang="python">>>> s1, s2 = {1, 2, 3, 4}, {3, 4, 5, 6}
>>> s1 | s2 # Union
{1, 2, 3, 4, 5, 6}
Line 6,089 ⟶ 6,366:
>>> s1
{1, 2, 3, 4, 5, 6}
>>> </langsyntaxhighlight>
 
=={{header|Quackery}}==
Line 6,105 ⟶ 6,382:
===Sorted Nests of Strings===
 
<langsyntaxhighlight Quackerylang="quackery"> [ [] $ "" rot
sort$ witheach
[ tuck != if
Line 6,196 ⟶ 6,473:
set{ orange }set dup echoset say " is"
fruits propersubset dup if [ say " not" ] say " the only fruit"
not if [ say " or not a fruit" ] cr</langsyntaxhighlight>
 
{{out}}
Line 6,213 ⟶ 6,490:
===Indexed Bitmaps===
 
<langsyntaxhighlight Quackerylang="quackery"> [ stack [ ] ] is elements ( --> s )
[ elements share 2dup find
Line 6,298 ⟶ 6,575:
set{ orange }set dup echoset say " is"
fruits propersubset dup if [ say " not" ] say " the only fruit"
not if [ say " or not a fruit" ] cr</langsyntaxhighlight>
 
{{out}}
Line 6,315 ⟶ 6,592:
=={{header|Racket}}==
 
<langsyntaxhighlight lang="racket">
#lang racket
 
Line 6,328 ⟶ 6,605:
(subset? C A) ; gives #f
(subset? C B) ; gives #t
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>use Test;
 
my $a = set <a b c>;
Line 6,350 ⟶ 6,627:
nok $a ⊂ $a, "strict subset; false for equal sets";
ok $a === set(<a b c>), "equality; true if every element of set A is in set B and vice-versa";
nok $a === $b, "equality; false for differing sets";</langsyntaxhighlight>
{{out}}
<pre>ok 1 - c is an element in set A
Line 6,366 ⟶ 6,643:
=={{header|REXX}}==
REXX doesn't have native set support, but can be easily coded to handle lists as sets.
<langsyntaxhighlight lang="rexx">/*REXX program demonstrates some common SET functions. */
truth.0= 'false'; truth.1= "true" /*two common names for a truth table. */
set.= /*the order of sets isn't important. */
Line 6,421 ⟶ 6,698:
if eq then if arg()>3 then return 1
else return set$('equal', _2, _1, 1)
return set.t</langsyntaxhighlight>
'''output'''
<pre>
Line 6,444 ⟶ 6,721:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Set
 
Line 6,558 ⟶ 6,835:
next
return flag
</syntaxhighlight>
</lang>
Output:
<pre>
Line 6,571 ⟶ 6,848:
Set A is not a subset of set B
Set A is not equal to set B
</pre>
 
=={{header|RPL}}==
RPL can handle lists, which are ordered sets. They can be created either by enumerating them:
{ "Azerty" #4 3.14 }
or by first pushing their components in the stack, then invoking the appropriate function:
"Azerty" #4 3.14 3 →LIST
{{works with|Halcyon Calc|4.2.7}}
We first need a command to remove an item for a list
≪ DUP2 1 + OVER SIZE SUB ROT ROT
IF DUP 1 == THEN DROP2 ELSE
1 SWAP 1 - SUB SWAP +
END
≫ '<span style="color:blue">POPL</span>' STO <span style="color:grey">@ ''( { a1 .. ak .. an } k → { a1 .. an } )''</span>
{| class="wikitable"
! RPL code
! Comment
|-
|
≪ POS SIGN ≫ '<span style="color:blue">IN?</span>' STO
≪ → a b
≪ a 1 b SIZE '''FOR''' j
b j GET '''IF''' a OVER POS '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT'''
≫ ≫ '<span style="color:blue">UNION</span>' STO
≪ → a b
≪ { } b
1 a SIZE '''FOR''' j
a j GET
'''IF''' DUP2 POS '''THEN'''
LAST ROT SWAP <span style="color:blue">POPL</span>
ROT ROT + SWAP
'''ELSE''' DROP '''END'''
'''NEXT''' DROP
≫ ≫ ‘<span style="color:blue">INTER</span>’ STO
≪ → a b
≪ { } b
1 a SIZE '''FOR''' j
a j GET
'''IF''' DUP2 POS NOT '''THEN'''
LAST ROT SWAP <span style="color:blue">POPL</span>
ROT ROT + SWAP
'''ELSE''' DROP '''END'''
'''NEXT''' DROP
≫ ≫ ‘<span style="color:blue">DIFFL</span>’ STO
≪ → a b
≪ 1 CF 1 a SIZE '''FOR''' j
'''IF''' b a j GET POS NOT '''THEN''' 1 SF a SIZE 'j' STO '''END'''
'''NEXT''' 1 FC?
≫ ≫ '<span style="color:blue">INCL</span>'?' STO
≪ DUP2 '''INCL?''' ROT ROT SWAP '<span style="color:blue">DIFFL</span>' AND
≫ '<span style="color:blue">SAME?</span>' STO
|
<span style="color:blue">IN?</span> ''( {A} m -- boolean )'' // 1 if m ∈ A
<span style="color:blue">UNION</span> ''( {A} {B} -- {A ∪ B} )''
Scan {B}...
... and add to {A} all {B} items not already in {A}
<span style="color:blue">INTER</span> ''( {A} {B} -- {A ∩ B} )''
Put a copy of {B} in stack
Scan {A}
if {A} item in copy of {B}
remove it from copy of {B}
add it to result
<span style="color:blue">DIFFL</span> ''( {A} {B} -- {A ∖ B} )''
Put a copy of {B} in stack
Scan {A}
if {A} item not in copy of {B}
remove it from copy of {B}
add it to result
<span style="color:blue">INCL?</span> ''( {A} {B} -- boolean )'' // true if {A} ⊆ {B}
Scan {A}...
... and break loop if an {A} item is not in {B}
return flag 1, set if loop has been broken
<span style="color:blue">SAME</span> ''( {A} {B} -- boolean )'' // true if {A} = {B}
{A} = {B} <=> {A} ⊆ {B} and {B} ⊆ {A}
|}
 
{{in}}
<pre>
3 {1 2 3 4} IN?
{1 2 3 4} {3 5 6} UNION
{1 2 3 4} {3 5 6} INTER
{1 2 3 4} {3 5 6} DIFFL
{1 2} {1 2 3 4} INCL?
{1 2 3} {3 1 2} SAME?
 
</pre>
{{out}}
<pre>
6: 1
5: { 1 2 3 4 5 6 }
4: { 3 }
3: { 1 2 4 }
2: 1
1: 1
</pre>
 
=={{header|Ruby}}==
Ruby's standard library contains a "set" package, which provides <code>Set</code> and <code>SortedSet</code> classes.
<langsyntaxhighlight lang="ruby">>> require 'set'
=> true
>> s1, s2 = Set[1, 2, 3, 4], [3, 4, 5, 6].to_set # different ways of creating a set
Line 6,617 ⟶ 7,010:
>> s1.subtract(s2) # Mutability
=> #<Set: {1, 2}>
>> </langsyntaxhighlight>
 
=={{header|Run BASIC}}==
<syntaxhighlight lang="runbasic">
<lang Runbasic>
A$ = "apple cherry elderberry grape"
B$ = "banana cherry date elderberry fig"
Line 6,690 ⟶ 7,083:
print left$(sets$,1);" is not equal ";right$(sets$,1)
end if
end function</langsyntaxhighlight>Output:
<pre>A = apple cherry elderberry grape
B = banana cherry date elderberry fig
Line 6,713 ⟶ 7,106:
=={{header|Rust}}==
 
<langsyntaxhighlight lang="rust">use std::collections::HashSet;
 
fn main() {
Line 6,727 ⟶ 7,120:
println!("Is A a subset of B? {}", a.is_subset(&b));
println!("Is A equal to B? {}", a == b);
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight lang="scala">
object sets {
val set1 = Set(1,2,3,4,5)
Line 6,741 ⟶ 7,134:
println(set1 == set2)
}
</syntaxhighlight>
</lang>
 
=={{header|Scheme}}==
Implemented based on lists. Not efficient on large sets.
<langsyntaxhighlight lang="lisp">(define (element? a lst)
(and (not (null? lst))
(or (eq? a (car lst))
Line 6,787 ⟶ 7,180:
(define (set-eq? a b)
(and (subset? a b)
(subset? b a)))</langsyntaxhighlight>
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const type: charSet is set of char;
Line 6,815 ⟶ 7,208:
writeln("A < A -- proper subset: " <& A < A);
writeln("A = B -- equality: " <& A = B);
end func;</langsyntaxhighlight>
 
{{out}}
Line 6,833 ⟶ 7,226:
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">
<lang SETL>
A := {1, 2, 3, 4};
B := {3, 4, 5, 6};
Line 6,844 ⟶ 7,237:
print(C subset B); -- #T
print(C = B); -- #F
</syntaxhighlight>
</lang>
 
=={{header|Sidef}}==
{{trans|Perl}}
<langsyntaxhighlight lang="ruby">class MySet(*set) {
 
method init {
Line 6,912 ⟶ 7,305:
set.has(self)
}
}</langsyntaxhighlight>
 
Usage example:
<langsyntaxhighlight lang="ruby">var x = MySet(1, 2, 3)
5..7 -> each { |i| x += i }
 
Line 6,936 ⟶ 7,329:
say ("w = x ^ y = ", w = (x ^ y))
say ("w is ", w ≡ z ? "" : "not ", "equal to z")
say ("w is ", w ≡ x ? "" : "not ", "equal to x")</langsyntaxhighlight>
{{out}}
<pre>
Line 6,958 ⟶ 7,351:
 
=={{header|Simula}}==
<langsyntaxhighlight lang="simula">SIMSET
BEGIN
 
Line 7,160 ⟶ 7,553:
 
END.
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 7,180 ⟶ 7,573:
=={{header|Smalltalk}}==
{{works with|Pharo|1.3-13315}}
<langsyntaxhighlight lang="smalltalk">
#(1 2 3) asSet union: #(2 3 4) asSet.
"a Set(1 2 3 4)"
Line 7,201 ⟶ 7,594:
#(1 2 3) asSet = #(1 2 4) asSet.
"false"
</syntaxhighlight>
</lang>
 
=={{header|SQL}}==
{{works with|Oracle}}
<langsyntaxhighlight lang="sql">
-- set of numbers is a table
-- create one set with 3 elements
Line 7,289 ⟶ 7,682:
minus
select element from myset2));
</syntaxhighlight>
</lang>
 
<pre>
Line 7,456 ⟶ 7,849:
=={{header|Swift}}==
{{works with|Swift|1.2+}}
<langsyntaxhighlight lang="swift">var s1 : Set<Int> = [1, 2, 3, 4]
let s2 : Set<Int> = [3, 4, 5, 6]
println(s1.union(s2)) // union; prints "[5, 6, 2, 3, 1, 4]"
Line 7,482 ⟶ 7,875:
println(s1) // prints "[2, 1]"
s1.exclusiveOrInPlace(s2) // mutability
println(s1) // prints "[5, 6, 2, 3, 1, 4]"</langsyntaxhighlight>
 
=={{header|Tcl}}==
Sets in Tcl are modeled as lists of items, with operations that preserve uniqueness of membership.
{{tcllib|struct::set}}
<langsyntaxhighlight lang="tcl">package require struct::set
 
# Many ways to build sets
Line 7,507 ⟶ 7,900:
struct::set exclude s3 $item
# Getting the cardinality:
puts "cardinality: [struct::set size $s3]</langsyntaxhighlight>
 
=={{header|VBA}}==
<langsyntaxhighlight lang="vb">'Implementation of "set" using the built in Collection datatype.
'A collection can hold any object as item. The examples here are only strings.
'A collection stores item, key pairs. With the key you can retrieve the item.
Line 7,633 ⟶ 8,026:
'Add "10" to A
A.Add "10", "10"
End Sub</langsyntaxhighlight>
 
=={{header|Wren}}==
Line 7,639 ⟶ 8,032:
{{libheader|Wren-set}}
Note that the Set class in the above module uses a Map internally for storage. Consequently, iteration order is undefined.
<langsyntaxhighlight ecmascriptlang="wren">import "./set" for Set
 
var fruits = Set.new(["apple", "pear", "orange", "banana"])
Line 7,669 ⟶ 8,062:
System.print("fruits5 + 'guava' : %(fruits5)")
fruits5.remove("cherry")
System.print("fruits5 - 'cherry' : %(fruits5)")</langsyntaxhighlight>
 
{{out}}
Line 7,699 ⟶ 8,092:
fruits5 + 'guava' : <raspberry, guava, blueberry, cherry>
fruits5 - 'cherry' : <raspberry, guava, blueberry>
</pre>
 
=={{header|XPL0}}==
{{trans|Wren}}
<syntaxhighlight lang "XPL0">proc PrintBool(Str, Test);
int Str, Test;
[Text(0, Str);
Text(0, if Test then "true" else "false");
CrLf(0);
];
 
proc PrintSet(Str, Set, Names);
int Str, Set, Names, I;
[Text(0, Str);
for I:= 0 to 31 do
if 1<<I & Set then
[Text(0, Names(I)); ChOut(0, ^ )];
CrLf(0);
];
 
int Names, Fruits, Fruits2, Fruits3, Fruits4, Fruits5;
def Apple=1<<0, Pear=1<<1, Orange=1<<2, Banana=1<<3, Melon=1<<4, Lemon=1<<5,
Gooseberry=1<<6, Elderberry=1<<7, Raspberry=1<<8, Blueberry=1<<9,
Cherry=1<<10, Guava=1<<11;
[Names:= ["Apple", "Pear", "Orange", "Banana", "Melon", "Lemon", "Gooseberry",
"Elderberry", "Raspberry", "Blueberry", "Cherry", "Guava"];
Fruits:= Apple ! Pear ! Orange ! Banana;
PrintSet("Fruits : ", Fruits, Names);
Fruits2:= Melon ! Orange ! Lemon ! Gooseberry;
PrintSet("Fruits2 : ", Fruits2, Names);
CrLf(0);
PrintBool("Fruits contains 'Banana' : ", Fruits & Banana);
PrintBool("Fruits2 contains 'Elderberry' : ", Fruits2 & Elderberry);
CrLf(0);
PrintSet("Union : ", Fruits ! Fruits2, Names);
PrintSet("Intersection : ", Fruits & Fruits2, Names);
PrintSet("Difference : ", Fruits & ~Fruits2, Names);
CrLf(0);
PrintBool("Fruits2 is a subset of Fruits : ",
(Fruits2 & Fruits) # 0 & (Fruits2 & ~Fruits) = 0);
CrLf(0);
Fruits3:= Fruits;
PrintSet("Fruits3 : ", Fruits3, Names);
CrLf(0);
PrintBool("Fruits2 and Fruits are equal : ", Fruits2 = Fruits);
PrintBool("Fruits3 and Fruits are equal : ", Fruits3 = Fruits);
CrLf(0);
Fruits4:= Apple ! Orange;
PrintSet("Fruits4 : ", Fruits4, Names);
CrLf(0);
PrintBool("Fruits3 is a proper subset of Fruits : ",
(Fruits3 & Fruits) # 0 & (Fruits3 & ~Fruits) = 0 & Fruits3 # Fruits);
PrintBool("Fruits4 is a proper subset of Fruits : ",
(Fruits4 & Fruits) # 0 & (Fruits4 & ~Fruits) = 0 & Fruits4 # Fruits);
CrLf(0);
Fruits5:= Cherry ! Blueberry ! Raspberry;
PrintSet("Fruits5 : ", Fruits5, Names);
CrLf(0);
Fruits5:= Fruits5 + Guava;
PrintSet("Fruits5 + 'Guava' : ", Fruits5, Names);
Fruits5:= Fruits5 - Cherry; \Cherry better be present!
PrintSet("Fruits5 - 'Cherry' : ", Fruits5, Names);
]</syntaxhighlight>
{{out}}
<pre>
Fruits : Apple Pear Orange Banana
Fruits2 : Orange Melon Lemon Gooseberry
 
Fruits contains 'Banana' : true
Fruits2 contains 'Elderberry' : false
 
Union : Apple Pear Orange Banana Melon Lemon Gooseberry
Intersection : Orange
Difference : Apple Pear Banana
 
Fruits2 is a subset of Fruits : false
 
Fruits3 : Apple Pear Orange Banana
 
Fruits2 and Fruits are equal : false
Fruits3 and Fruits are equal : true
 
Fruits4 : Apple Orange
 
Fruits3 is a proper subset of Fruits : false
Fruits4 is a proper subset of Fruits : true
 
Fruits5 : Raspberry Blueberry Cherry
 
Fruits5 + 'Guava' : Raspberry Blueberry Cherry Guava
Fruits5 - 'Cherry' : Raspberry Blueberry Guava
</pre>
 
=={{header|zkl}}==
A simplistic implementation that is fine for smallish sets
<langsyntaxhighlight lang="zkl">var [const] unique = Utils.Helpers.listUnique;
class Set {
fcn init { var [const] set = (vm.arglist.copy() : unique(_)) }
Line 7,723 ⟶ 8,207:
}
fcn __opEQ(setB) { ((set.len() == setB.set.len()) and self.isSubset(setB)) }
}</langsyntaxhighlight>
<pre>
A := Set(1,2,3,3,3,4);
9,476

edits