RosettaCodeData/Task/Power-set/M2000-Interpreter/power-set.m2000

73 lines
1.4 KiB
Plaintext

Function ToStringSet {
if match("A") then
read SetA
else.if not empty then
SetA=Cons(array([]))
else
SetA=(,)
End if
k=each(SetA)
variant z
while k
z=Array(k)
if type$(z)="tuple" then
if len(Array(k))=0 then
data "∅"
else.if type$(z, 0)="tuple" then
data "{∅}"
else
data "{"+(Array(k)#str$(", "))+"}"
end if
else
data z
end if
end while
if len(SetA)=0 then
="∅"
else
="{"+Array([])#Str$(", ")+"}"
end if
}
Function PowerSet {
if match("A") then
read SetA
else.if not empty then
SetA=Cons(array([]))
else
SetA=(,)
End if
a$=string$("0",Len(SetA))
p=lambda a$ -> {
boolean ok
if len(a$)=0 then ="": exit
for i=1 to len(A$)
if mid$(a$,i,1)="0" then insert i,1 a$="1": ok=true : exit else insert i,1 a$="0"
next
if ok then =a$ else =""
}
if Len(setA)>1 then
setAll=("{}",)
else
setAll=((,),)
end if
do
z=p()
if len(z)=0 then exit
SetB=(,)
for i=1 to len(z)
if mid$(z, i,1)="1" then
SetB=SetB#end(SetA#Slice(i-1, i-1))
end if
next
Append setAll, (SetB,)
always
=setAll
}
open dir$+"powerset.txt" for wide output as #f
Print #f, "P(";ToStringSet();")=";ToStringSet(PowerSet())
Print #f, "P(";ToStringSet(PowerSet());")=";ToStringSet(PowerSet(PowerSet()))
Print #f, "P(";ToStringSet(1,2,3,4);")=";ToStringSet(PowerSet(1,2,3,4))
Print #f, "P(";ToStringSet("A", "B");")=";ToStringSet(PowerSet("A", "B"))
close #f
win notepad.exe, dir$+"powerset.txt"