Set: Difference between revisions
Content added Content deleted
m (BBC BASIC moved to the BASIC section.) |
(Added XPL0 example.) |
||
Line 8,027: | Line 8,027: | ||
fruits5 + 'guava' : <raspberry, guava, blueberry, cherry> |
fruits5 + 'guava' : <raspberry, guava, blueberry, cherry> |
||
fruits5 - 'cherry' : <raspberry, guava, blueberry> |
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> |
</pre> |
||