Πέμπτη 17 Οκτωβρίου 2024

A zip function for lists

module zip_function {
const rightjustify=3, normaljustify=0
function zip(f) {
n=stack.size
if n=0 then =(,):exit
mL=infinity
// array to save arrays
dim a(1 to n)
for i=1 to n
read a(i)
ml=min.data(len(a(i))-1, ml)
next
if ml<0 then =(,):exit
// array for return value
dim b(0 to ml)
for k=0 to ml
for j=n to 1
push a(j)(k)
next
// [] is a function. return the current stack object,
// leave an empty stack object as the current stack object
// !s where s is a stack object in a parameter list leave all
// items of that stack to the parameter list (which is a stack object)
b(k)=f(![])
next
=b()
}

add=lambda ->{
sum=0
while not empty
if isnum then
sum+=number
else
drop
end if
end while
=sum
}
// double coma ,, at print list is a new line
? (1,2,3),, (10,20,30),, (2, 2, 200)
m=zip(add, (1,2,3), (10,20,30), (2, 2, 200))
//clipboard m#str$()
? m#str$()="13 24 233"
? m#str$()

concat=lambda ->{
string all
while not empty
if isnum then
all+=""+number
else
all+=letter$
end if
end while
=all
}
? $(rightjustify), (1,2,3),, (10,20,30),, (2, "X", 200),, ("aaa",2,3,4), $(normaljustify),,
m=zip(concat, (1,2,3), (10,20,30), (2, "X", 200), ("aaa",2,3,4))
? m#str$()="1102aaa 220X2 3302003"
? m#str$()
m=zip(add, (1,2,3), (10,20,30), (2, "X", 200), ("aaa",2,3,4))
? m#str$()="13 24 236"
? m#str$()
m=zip(add, (1,2,3))
? m#str$()
m=zip(add,(1,2,3), (,))
? Len(m)=0
m=zip(add)
? Len(m)=0
}
zip_function

Τρίτη 15 Οκτωβρίου 2024

New addition for M2000 at rosettacode.org

https://rosettacode.org/wiki/Append_numbers_at_same_position_in_strings#M2000_Interpreter 

There are 438 tasks for M2000 Interpreter. Look at the end of this page:

https://rosettacode.org/wiki/Category:M2000_Interpreter


This is a small program:

module testme {
List1=(1,2,3,4,5,6,7,8,9)
List2=(10,11,12,13,14,15,16,17,18)
List3=(19,20,21,22,23,24,25,26,27)
List=(,)
k=each(list1)
l=each(list2)
m=each(list3)
while k, l, m
append List, (array$(k)+array$(l)+array$(m),)
end while
print "list1 = ["+List1#str$(",")+"]"
print "list2 = ["+List2#str$(",")+"]"
print "list3 = ["+List3#str$(",")+"]"
print "list  = ["+List#str$(",")+"]"
}
TestMe

Variable List has color blue because there is a statement List. The editor use the blue color for known identifiers. So for Module testme list used as variable (pointer to array). We can use the statement List, which show variables. If we make a Module List inside testme then the command List skipped as module List. Run m2000.exe (the M2000 interpreter) and write: Edit C then press enter and copy these lines then press Esc and write C and press enter (c or C is the same for M2000)

global k=100
module testList {
A=100+k
module list {
? "this is module list"
}
list
@list
}
testlist
? "return from testlist"
list

We get this:

this is module list

K = 100, C.TESTLIST.A = 200

return from testlist

K = 100