Set: Difference between revisions
m
→{{header|Wren}}: Minor tidy
m (→{{header|Wren}}: Minor tidy) |
|||
(14 intermediate revisions by 10 users not shown) | |||
Line 35:
{{trans|Python}}
<
V s2 = Set([3, 4, 5, 6])
print(s1.union(s2))
Line 56:
print(s1)
s1.discard(99)
print(s1)</
{{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}}
<
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</
{{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.
<
use ada.text_io;
Line 423:
end loop;
end set_demo;
</syntaxhighlight>
{{out}}
Line 438:
=={{header|Aime}}==
<
union(record a, record b)
{
Line 518:
return 0;
}</
{{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.
<
# 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 )</
{{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.
<
public class MySetController{
public Set<String> strSet {get; private set; }
Line 738:
}
}
</syntaxhighlight>
</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>.
<
use framework "Foundation"
--use scripting additions
Line 794:
end doSetTask
doSetTask()</
{{out}}
<
\"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"</
=={{header|Arturo}}==
<
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]</
{{out}}
Line 859:
You might notice the operator overloading.
(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
(*------------------------------------------------------------------*)</
{{out}}
Line 1,638 ⟶ 1,643:
set1 = set2: false
set2 = set1: false
(set1 ^ set2) = (set2 ^ set1): true
(set1 ^ set2) = set1: false
(set1 ^ set2) = set2: false</pre>
=={{header|AutoHotkey}}==
<
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
}</
Examples:<
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</
{{out}}
<pre>A:= ["apple", "cherry", "elderberry", "grape"]
Line 1,758 ⟶ 1,763:
A is not a equal to Set E</pre>
=={{header|
==={{header|BBC BASIC}}===
The sets are represented as 32-bit integers, which means that the maximum number of elements is 32.
<
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$))</
{{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.
<
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</
⟨ 2 ⟩
⟨ 4 6 8 ⟩
0
0
⟨ 2 3 5 7 ⟩</
=={{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.
<
typedef unsigned int set_t; /* probably 32 bits; change according to need */
Line 1,891 ⟶ 1,897:
return 0;
}</
=={{header|C sharp|C#}}==
<
using System.Collections.Generic;
using System.Linq;
Line 1,949 ⟶ 1,955:
Console.ReadKey();
}
}</
{{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.
<
#include <set>
#include <iostream>
Line 2,059 ⟶ 2,065:
return 0;
}
</syntaxhighlight>
=={{header|Ceylon}}==
<
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``");
}</
=={{header|Clojure}}==
<
; 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)</
=={{header|CoffeeScript}}==
This implements functions from the task, along with an iteration helper called "each".
<
# 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>
=={{header|Common Lisp}}==
Common Lisp provides some set operations on lists.
<
(setf b '(2 3 4 5))
Line 2,212 ⟶ 2,218:
(subsetp b a))
"~a = ~a~%"
"~a ≠ ~a~%") a b)</
=={{header|D}}==
<
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));
}</
<syntaxhighlight lang="d">
module set;
import std.typecons : Tuple, tuple;
Line 2,389 ⟶ 2,395:
return true;
}
</syntaxhighlight>
=={{header|Dart}}==
<
//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)}');
}</
{{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">
program Set_task;
Line 2,481 ⟶ 2,487:
Writeln('S1 equality S2? ', s1 = s2);
readln;
end.</
{{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: ∩ ∪ ⊆ / ∈ = ∆ ×
<
; 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>
=={{header|Elixir}}==
{{works with|Elixir|1.1}}
<
#MapSet<[]>
iex(2)> sa = MapSet.put(s, :a)
Line 2,563 ⟶ 2,651:
false
iex(14)> sa == sab
false</
=={{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)
<
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</
{{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>.
<
( 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</
=={{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
<
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>
=={{header|FreeBASIC}}==
<
'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() )</
{{out}}
<pre>
Line 2,866 ⟶ 2,954:
=={{header|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>
=={{header|FunL}}==
<
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 )</
{{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.
<
import "fmt"
Line 3,055 ⟶ 3,289:
func properSubset(a, b set) bool {
return len(a) < len(b) && subset(a, b)
}</
{{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.
<
import (
Line 3,212 ⟶ 3,446:
}
return
}</
{{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.
<
import (
Line 3,294 ⟶ 3,528:
}
return &set
}</
{{out}}
<pre>
Line 3,315 ⟶ 3,549:
=={{header|Groovy}}==
<
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'</
=={{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>).
<
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]</
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.
<
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]</
=={{header|Icon}} and {{header|Unicon}}==
Line 3,397 ⟶ 3,631:
Implemented directly:
<
procedure display_set (s)
writes ("[")
Line 3,459 ⟶ 3,693:
write ("(1,2,5) is not included in a")
end
</syntaxhighlight>
{{out}}
Line 3,478 ⟶ 3,712:
Using library:
<
link sets
Line 3,518 ⟶ 3,752:
write ("(1,2,5) is not included in a")
end
</syntaxhighlight>
{{out}}
Line 3,541 ⟶ 3,775:
Here are definitions for the required operations:
<
intersection=: [ -. -.
difference=: -.
subset=: *./@e.
equality=: -:&(/:~)</
Examples:
<
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</
Examples again, using names rather than code:
<
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</
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
<
=={{header|Java}}==
{{works with|Java|7+}}
To use this in Java 5 replace all "<>" with "<Integer>".
<
import java.util.Collections;
import java.util.Set;
Line 3,647 ⟶ 3,881:
Collections.unmodifiableSet(a); //returns an immutable copy of a
}
}</
{{out}}
<pre>a: [1, 2, 3, 4, 5]
Line 3,667 ⟶ 3,901:
JavaScript does not support native sets before ECMAScript 6.
<
var set = new Set();
Line 3,691 ⟶ 3,925:
for (var item of set) {
console.log('item is ' + item);
}</
=={{header|jq}}==
Line 3,711 ⟶ 3,945:
For example:
<
{"a":true} + {"b":true } == { "a":true, "b":true}</
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":
<
. as $in | type == "object" and reduce keys[] as $key (true; . and $in[$key] == true);</
'''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
where T is the JSON object corresponding to S. This test is also efficient.
'''String-set intersection'''
<
def stringset_intersection(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . + {($k):true} else . end);</
'''String-set difference'''
<
def stringset_difference(A;B):
reduce (A|keys)[] as $k
({}; if (B|has($k)) then . else . + {($k):true} end);</
'''Subset'''
<
def stringset_subset(A;B):
reduce (A|keys)[] as $k
(true; . and (B|has($k)));</
=== 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:
<
. as $in
| type == "array" and
reduce range(0;length-1) as $i
(true; if . then $in[$i] < $in[$i+1] else false end);</
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:
<
'''Intersection'''
<
def intersection($A;$B):
def pop:
Line 3,804 ⟶ 4,038:
else [$i, $j+1] | pop
end;
[[0,0] | pop];</
'''Difference'''
<
def difference(A;B):
(A|length) as $al
Line 3,824 ⟶ 4,058:
end
) | .[2]
end ;</
'''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.
<
# 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);</
'''A ⊆ B'''
<
# TCO
def _subset:
Line 3,864 ⟶ 4,098:
else [ .[0], .[1][1:] ] | _subset
end;
[A,B] | _subset;</
'''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:
<
.[0] as $A | .[1] as $B
| ($A|length) as $al
Line 3,883 ⟶ 4,117:
end
end;
</syntaxhighlight>
=={{header|Julia}}==
Line 3,912 ⟶ 4,146:
=={{header|Kotlin}}==
<
fun main(args: Array<String>) {
Line 3,947 ⟶ 4,181:
println("fruits5 + 'guava' : $fruits5")
println("fruits5 - 'cherry' : ${fruits5 - "cherry"}")
}</
{{out}}
Line 3,980 ⟶ 4,214:
=={{header|Lasso}}==
<
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</
{{out}}
Line 4,017 ⟶ 4,251:
{{trans|Erlang}}
<
> (set set-1 (sets:new))
#(set 0 16 16 8 80 48 ...)
Line 4,046 ⟶ 4,280:
> (=:= set-3 set-4)
true
</syntaxhighlight>
=={{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">
A$ ="red hot chili peppers rule OK"
B$ ="lady in red"
Line 4,200 ⟶ 4,434:
end function
</syntaxhighlight>
Line 4,219 ⟶ 4,453:
=={{header|Lua}}==
{{works with|lua|5.1}}
<
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>
{{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>.
<
local r = {}
local s = setmetatable({}, {
Line 4,406 ⟶ 4,640:
end
return setmetatable({}, { __call = new })</
=={{header|M2000 Interpreter}}==
Line 4,412 ⟶ 4,646:
For search in a tuple we have O(N).
<syntaxhighlight lang="m2000 interpreter">
Module Sets {
setA=("apple", "cherry", "grape")
Line 4,486 ⟶ 4,720:
}
Sets
</syntaxhighlight>
=={{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">
> 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>
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
MemberQ[set1, "a"]
Union[set1 , set2]
Line 4,542 ⟶ 4,776:
MemberQ[Subsets[set2], set1](*Subset*)
set1 == set2(*Equality*)
set1 == set1(*Equality*)</
{{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">
% 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>
=={{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</
=={{header|Nanoquery}}==
This is a full implementation of a set class.
<
declare internal_list
Line 4,733 ⟶ 4,967:
return str(this.internal_list)
end
end</
Testing this implementation:
<
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)</
{{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.
<
using Nemerle.Collections;
Line 4,804 ⟶ 5,038:
WriteLine($"$same\t$sub12");
}
}</
=={{header|Nim}}==
Line 4,811 ⟶ 5,045:
Here is an example of usage of a set.
<
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</
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.
<
var # creation
Line 4,853 ⟶ 5,087:
s.incl(4) # add 4 to set
s.excl(5) # remove 5 from set</
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}}==
<
int main (int argc, const char *argv[]) {
Line 4,914 ⟶ 5,148:
}
return 0;
}</
{{out}}
<pre>
Line 4,996 ⟶ 5,230:
In the interactive toplevel:
{{works with|OCaml|4.02+}}
<
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]</
(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}}==
<
; test set
(define set1 '(1 2 3 4 5 6 7 8 9))
Line 5,105 ⟶ 5,339:
(print (equal? set1 set4))
; ==> #true
</syntaxhighlight>
=={{header|ooRexx}}==
<
-- 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</
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.
<
for(i=1,#s,
if(!setsearch(t,s[i]), return(0))
Line 5,164 ⟶ 5,398:
setminus(s,t)
setsubset(s,t)
s==t</
{{out}}
Line 5,182 ⟶ 5,416:
--[[User:Guionardo|Guionardo]] 22:03, 7 January 2012 (UTC)
<
{$mode objfpc}{$H+}
Line 5,275 ⟶ 5,509:
readln;
end.</
=={{header|Perl}}==
For real code, try Set::Object from CPAN. Here we provide a primitive implementation using hashes.
<
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";</
=={{header|Phix}}==
First, a simple implementaion using native sequences:
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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;">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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">set1</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;">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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</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;">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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #004600;">true</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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;">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>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<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>
<!--</syntaxhighlight>-->
{{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)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<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>
<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>
<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>
<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>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<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}}==
<syntaxhighlight lang="Phixmonti">include ..\Utilitys.pmt
def isElement find enddef
def setUnion dup >ps remove ps> chain enddef
def setIntersection over >ps remove ps> swap remove enddef
def setDifference remove enddef
def setSubset swap remove len not nip enddef
def setEquality sort swap sort == enddef
( 1 2 3 ) 1 isElement ?
4 isElement ?
( 3 4 5 ) setUnion ?
( 1 2 3 ) ( 3 4 5 ) setIntersection ?
( 1 2 3 ) ( 3 4
( 1 2 3 ) ( 3 4
( 1 2 3 ) ( 1 2 ) setSubset ?
( 1 2 3 ) ( 3 4 5 ) setEquality ?
( 1 2 3 ) ( 3 1 2 ) setEquality ?
</syntaxhighlight>
{{out}}
<pre>1
0
[1, 2, 3, 4, 5]
[3]
[1, 2]
0
1
0
1
=== Press any key to exit ===</pre>
=={{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===
<
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</
===Using 'idx' structures===
<
(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</
=={{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">
[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>
=={{header|Prolog}}==
Works with SWI-Prolog, library(lists).
<
set :-
Line 5,753 ⟶ 6,030:
select(H1, B, B1),
equal(T1, B1).
</syntaxhighlight>
{{out}}
<pre> ?- set.
Line 5,782 ⟶ 6,059:
It treats sets as ordered lists of unique elements:
<
%% Set creation
Line 5,838 ⟶ 6,115:
?- ord_del_element($NewA, 3, NewerA).
NewerA = [1, 2, 4, 19].
</syntaxhighlight>
=={{header|PureBasic}}==
This solution uses PureBasic's maps (hash tables).
<
If b: ProcedureReturn "True": EndIf
ProcedureReturn "False"
Line 6,011 ⟶ 6,288:
Print(#crlf$ + #crlf$ + "Press ENTER to exit"): Input()
CloseConsole()
EndIf</
{{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+}}
<
>>> s1 | s2 # Union
{1, 2, 3, 4, 5, 6}
Line 6,089 ⟶ 6,366:
>>> s1
{1, 2, 3, 4, 5, 6}
>>> </
=={{header|Quackery}}==
Line 6,105 ⟶ 6,382:
===Sorted Nests of Strings===
<
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</
{{out}}
Line 6,213 ⟶ 6,490:
===Indexed Bitmaps===
<
[ 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</
{{out}}
Line 6,315 ⟶ 6,592:
=={{header|Racket}}==
<
#lang racket
Line 6,328 ⟶ 6,605:
(subset? C A) ; gives #f
(subset? C B) ; gives #t
</syntaxhighlight>
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku"
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";</
{{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.
<
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</
'''output'''
<pre>
Line 6,444 ⟶ 6,721:
=={{header|Ring}}==
<
# Project : Set
Line 6,558 ⟶ 6,835:
next
return flag
</syntaxhighlight>
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.
<
=> 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}>
>> </
=={{header|Run BASIC}}==
<syntaxhighlight 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</
<pre>A = apple cherry elderberry grape
B = banana cherry date elderberry fig
Line 6,713 ⟶ 7,106:
=={{header|Rust}}==
<
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);
}</
=={{header|Scala}}==
<
object sets {
val set1 = Set(1,2,3,4,5)
Line 6,741 ⟶ 7,134:
println(set1 == set2)
}
</syntaxhighlight>
=={{header|Scheme}}==
Implemented based on lists. Not efficient on large sets.
<
(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)))</
=={{header|Seed7}}==
<
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;</
{{out}}
Line 6,833 ⟶ 7,226:
=={{header|SETL}}==
<syntaxhighlight 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>
=={{header|Sidef}}==
{{trans|Perl}}
<
method init {
Line 6,912 ⟶ 7,305:
set.has(self)
}
}</
Usage example:
<
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")</
{{out}}
<pre>
Line 6,958 ⟶ 7,351:
=={{header|Simula}}==
<
BEGIN
Line 7,160 ⟶ 7,553:
END.
</syntaxhighlight>
{{out}}
<pre>
Line 7,180 ⟶ 7,573:
=={{header|Smalltalk}}==
{{works with|Pharo|1.3-13315}}
<
#(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>
=={{header|SQL}}==
{{works with|Oracle}}
<
-- set of numbers is a table
-- create one set with 3 elements
Line 7,289 ⟶ 7,682:
minus
select element from myset2));
</syntaxhighlight>
<pre>
Line 7,456 ⟶ 7,849:
=={{header|Swift}}==
{{works with|Swift|1.2+}}
<
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]"</
=={{header|Tcl}}==
Sets in Tcl are modeled as lists of items, with operations that preserve uniqueness of membership.
{{tcllib|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]</
=={{header|VBA}}==
<
'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</
=={{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.
<
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)")</
{{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
<
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)) }
}</
<pre>
A := Set(1,2,3,3,3,4);
|