Set: Difference between revisions

4,650 bytes added ,  3 months ago
m
m (→‎{{header|Wren}}: Minor tidy)
 
(5 intermediate revisions by 5 users not shown)
Line 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.
<syntaxhighlight lang="bbcbasic"> DIM list$(6)
Line 4,808 ⟶ 4,809:
 
=={{header|Maxima}}==
<syntaxhighlight lang="textmaxima">/* illustrating some functions on sets; names are self-explanatory */
 
a: {1, 2, 3, 4};
Line 5,805 ⟶ 5,806:
<!--</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 5 ) setDifference ?
( 1 2 3 ) ( 3 4 5 ) setSubset ?
( 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}}==
Line 6,816 ⟶ 6,856:
"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
Line 6,821 ⟶ 6,867:
|-
|
≪ POS SIGN ≫ ''''<span style="color:blue">IN?'''</span>' STO
≪ → a b
Line 6,827 ⟶ 6,873:
b j GET '''IF''' a OVER POS '''THEN''' DROP '''ELSE''' + '''END'''
'''NEXT'''
≫ ≫ ''''<span style="color:blue">UNION'''</span>' STO
≪ → a b
≪ { } 1 a SIZE '''FOR'''b j
1 a j GETSIZE '''IFFOR''' bj OVER POS '''THEN''' + '''ELSE''' DROP '''END'''
NEXT a j GET
'''IF''' DUP2 POS ''INTER'THEN''' STO
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
≪ { } 1 a SIZE '''FOR'''b j
1 a j GET IF b OVER POSSIZE '''THENFOR''' DROPj '''ELSE''' + '''END'''
'''NEXT''' a j GET
'''IF''' DUP2 POS NOT ''DIFFL'THEN''' STO
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
Line 6,845 ⟶ 6,901:
'''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≪ DUP2 '''INCL?''' ROT ROT SWAP '''INCL?''<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
'''INTER''' ''( {A} {B} -- {A ∩ B} )''
Scan {A}...
... and keep {A} items also in {B}
if {A} item in copy of {B}
.
remove it from copy of {B}
.
add it to result
'''DIFFL''' ''( {A} {B} -- {A ∖ B} )''
Scan {A}...
... and keep {A} items not in {B}
.
.
<span style="color:blue">DIFFL</span> ''( {A} {B} -- {A ∖ B} )''
.
Put a copy of {B} in stack
'''INCL?''' ''( {A} {B} -- boolean )'' // true if {A} ⊆ {B}
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}
|}
Line 7,966 ⟶ 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.
<syntaxhighlight lang="ecmascriptwren">import "./set" for Set
 
var fruits = Set.new(["apple", "pear", "orange", "banana"])
Line 8,026 ⟶ 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>
 
9,482

edits