Error: assigning to a long list, please use arrays

Maple allows you to change a list by assigning a new value to one of its components.

> L:= [a,b,c,d,e]: L[2]:= B; L;

However, it only works for lists of length 100 or less. Trying it for a longer list produces this error message.

> L:= [seq(i,i=1..101)]:
L[2]:= 3;

Error, assigning to a long list, please use arrays

Nevertheless, the same effect can be obtained for a list of any length, using subsop :

> L:=subsop(2=3,L);

This is rather inefficient if the list is long, because it requires copying the whole list structure. If you used an array or Array it would be very quick. Nevertheless, if only a few of these operations are to be done, it may still be preferable to use lists rather than arrays, because lists can be produced very quickly using seq . Here is an example, where we create a list of 1000000 integers and then replace 100 of them. Timings were done

under Windows 95 on a 266-mhz Pentium II with 128Mb RAM.

> restart; t:= time(): L:= [seq(1000000-i,i=1..1000000)]:
print("Setup time",time()-t);
for i from 1 to 100 do
L:= subsop(i^2 = 0, L)
od:
print("Total time",time()-t);

And here is the equivalent calculation using an array. Changing 100 elements of the array is practically instantaneous, but setting up the array in the first place is very time-consuming (an Array would be faster).

> restart; t:= time(): L:= array(1..1000000):
for i from 1 to 1000000 do L[i]:= 1000000-i od:
print("Setup time",time()-t);
for i from 1 to 100 do
L[i^2] := 0
od:
print("Total time",time()-t);