@nm

For the situation you describe ("I want to iterate over the list and make some substituion to each entry if needed"), you should encode your substitution algorithm into a procedure (perhaps an anonymous one) and map that over the list or set. This is efficient, as only one new list or set is constructed (the one containing all the modifications). Here's a cooked up example:

S := {seq(i,i=1..1000)}; # Generate a set of integers.
# Square the odd ones.
S := map(proc(x) if x::odd then x^2 else x fi end, S);

Simple "one-liner" procedures like the one above can be written in a more compact notation too:

S := map(x -> if x::odd then x^2 else x fi, S);

Internally, Maple creates a new expression sequence the same size as the one in the original set, applies your procedure to each in turn, in-place, and then turns that back into a set (re-ordering, removing duplicates that may have resulted, etc.).

Note that the example above could not have been written using substitution into the set, whether by the non-existent `S[i] := expr`

syntax, nor using `subsop`

. After each substitution was completed, the set would be resimplified, and likely reordered, and the remaining elements not yet processed would no longer be in their original locations:

Using map:

> S := {seq(i,i=1..10)};
S := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> S := map(x -> if x::odd then x^2 else x fi, S);
S := {1, 2, 4, 6, 8, 9, 10, 25, 49, 81}

Using a loop (I've added a print statement in the loop so we can see why we're going to get an error):

> S := {seq(i,i=1..10)};
S := {1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
> for i to 10 do
> if S[i] :: odd then
> S := subsop(i=S[i]^2,S);
> print(S)
> fi
> od:
{1, 2, 3, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 4, 5, 6, 7, 8, 9, 10}
{1, 2, 4, 6, 7, 8, 9, 10, 25}
{1, 2, 4, 6, 8, 9, 10, 25, 49}
{1, 2, 4, 6, 8, 10, 25, 49, 81}
{1, 2, 4, 6, 8, 10, 49, 81, 625}
{1, 2, 4, 6, 8, 10, 49, 625, 6561}
{1, 2, 4, 6, 8, 10, 49, 625, 43046721}
Error, invalid subscript selector

Notice that the second time we square an entry, 3, it becomes 9, and there is now one less member in the set (because there was already a 9). Furthermore, when we square 5 and replace it with 25, the 25 ends up at the "end" of the set in the underlying data structure, so later we encounter the 25 and square it as well (since it's also odd). Finally, we get an error when accessing the no-longer present 10th element of the set (the one that was lost when we squared 3).