Set consolidation: Difference between revisions

m
→‎{{header|REXX}}: added/changed whitespace and comments, made other cosmetic changes.
(Added EchoLisp)
m (→‎{{header|REXX}}: added/changed whitespace and comments, made other cosmetic changes.)
Line 1,464:
 
=={{header|REXX}}==
<lang rexx>/*REXX program demonstrates how toa method consolidate of a sampleconsolidating bunch ofsome sample sets. */
@.=; @.1 = '{A,B} {C,D}'
sets. = /*assign all SETS. to null.*/
sets @.12 = '"{A,B} {CB,D}'"
sets @.23 = "'{A,B} {BC,D}" {D,B}'
sets @.34 = '{H,I,K} {A,B} {C,D} {D,B} {F,G,H}'
sets.4 = '{H,I,K} {A,B} @.5 = '{Csnow,Dice,slush,frost,fog} {Dicebergs,Bicecubes} {Frain,Gfog,Hsleet}'
sets.5 = '{snow,ice,slush,frost,fog} {iceburgs,icecubes} {rain,fog,sleet}'
 
do j=1 while sets@.j\=='' /*traipse through theeach of sample sets. */
call SETcombo sets.j call SETcombine @.j /*have the other guyfunction do the heavy work. */
end /*j*/
exit /*stick a fork in it, we're all done. */
/*──────────────────────────────────ISIN subroutine─────────────────────SUBRoutine───────────────────────────*/
/*──────────────────────────────────SETCOMBO subroutine─────────────────*/
SETcomboisIn: procedure;return parsewordpos(arg(1), arg(2))\==0 bunch; n=words/*is (bunchword); arg1 in set arg2 newBunch=? */
/*──────────────────────────────────SETCOMBINE subroutine─────────────────────*/
say ' the old sets=' space(bunch)
SETcombine: procedure; parse arg old,new; #=words(old) /*nullify NEW.*/
say ' the old setsset=' space(bunchold)
 
do k=1 for n# /* [↓] change all commas to a blank. */
@!.k=translate(word(bunchold,k), , '},{') /*create a list of words (=aka, a set).*/
end /*k*/ /* [↑] ··· and also remove the braces.*/
 
do until \changed; changed=0 /*consolidate some sets (well, maybe).*/
do set=1 for n#-1
do item=1 for words(@!.set); x=word(@!.set,item)
do other=set+1 to n#
if isIn(x,@!.other) then do; changed=1 /*has changed.*/
@!.set=@!.set @!.other; @ !.other=
iterate set
end
end /*other*/
end /*item */
end /*set */
end /*until ¬changed*/
/* ╔╦══════════════════════════════════════════════════elide any duplicates.*/
 
do set=1 for n#; new$= /*remove duplicates in a set. /*nullify $ */
do items=1 for words(@!.set); x=word(@!.set, items)
if x==',' then iterate; if x=='' then leave
new$=new$ x /*start building the new set. /*build new. */
do until \isIn(x, @!.set)
_=wordpos(x, @!.set)
@ !.set=subword(@!.set,1,_-1) ',' subwordsubwOrd(@!.set,_+1) /*purify set.*/
end /*until ¬isIn ··· */
end /*items*/
@!.set=translate(strip(new$), ',', " ")
end /*set*/
 
do newi=1 for n#; if @!.newi=='' then iterate
newBunchnew=space(newbunchnew '{'@!.newi"}")
end /*newi*/
 
say ' the new setsset=' newBunchnew; say
return</lang>
{{out}}'''output''' &nbsp; when using the default supplied sample sets:
/*──────────────────────────────────ISIN subroutine─────────────────────*/
isIn: return wordpos(arg(1), arg(2))\==0 /*is (word) arg1 in set arg2? */</lang>
{{out}} when using the default supplied sample sets:
<pre>
the old setsset= {A,B} {C,D}
the new setsset= {A,B} {C,D}
 
the old setsset= {A,B} {B,D}
the new setsset= {A,B,D}
 
the old setsset= {A,B} {C,D} {D,B}
the new setsset= {A,B,D,C}
 
the old setsset= {H,I,K} {A,B} {C,D} {D,B} {F,G,H}
the new setsset= {H,I,K,F,G} {A,B,D,C}
 
the old setsset= {snow,ice,slush,frost,fog} {iceburgsicebergs,icecubes} {rain,fog,sleet}
the new setsset= {snow,ice,slush,frost,fog,rain,sleet} {iceburgsicebergs,icecubes}
</pre>