RosettaCodeData/Task/Combinations/M2000-Interpreter/combinations-2.m2000

56 lines
1.4 KiB
Plaintext

Module StepByStep {
Function CombinationsStep (a, nn) {
c1=lambda (&f, &a) ->{
=car(a) : a=cdr(a) : f=len(a)=0
}
m=len(a)
c=c1
n=m-nn+1
p=2
While m>n
c1=lambda c2=c,n=p, z=(,) (&f, &m) ->{
if len(z)=0 then z=cdr(m)
=cons(car(m),c2(&f, &z))
if f then z=(,) : m=cdr(m) : f=len(m)+len(z)<n
}
c=c1
p++
m--
End While
=lambda c, a (&f) -> {
=c(&f, &a)
}
}
enum out {screen="", file="out.txt"}
m=each(out)
while m
open eval(m) for output as #f
k=false
StepA=CombinationsStep((1, 2, 3, 4,5), 3)
While not k
Print #f, StepA(&k)#str$()
End While
Print #f
k=false
StepA=CombinationsStep((0, 1, 2, 3, 4), 3)
While not k
Print #f, StepA(&k)#str$()
End While
Print #f
k=false
StepA=CombinationsStep(("A", "B", "C", "D","E"), 3)
While not k
Print #f, StepA(&k)#str$("-")
End While
Print #f
k=false
StepA=CombinationsStep(("CAT", "DOG", "BAT"), 2)
While not k
Print #f, StepA(&k)#str$("-")
End While
close #f
end while
win "notepad", dir$+file
}
StepByStep