Set: Difference between revisions

1,734 bytes added ,  4 months ago
m
(Added XPL0 example.)
m (→‎{{header|Wren}}: Minor tidy)
 
(3 intermediate revisions by 3 users not shown)
Line 4,809:
 
=={{header|Maxima}}==
<syntaxhighlight lang="textmaxima">/* illustrating some functions on sets; names are self-explanatory */
 
a: {1, 2, 3, 4};
Line 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,817 ⟶ 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,822 ⟶ 6,867:
|-
|
≪ POS SIGN ≫ ''''<span style="color:blue">IN?'''</span>' STO
≪ → a b
Line 6,828 ⟶ 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,846 ⟶ 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,967 ⟶ 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"])
9,486

edits