       Re: Q: efficient in-place list element replacement?

• To: mathgroup at smc.vnet.net
• Subject: [mg21140] Re: Q: efficient in-place list element replacement?
• From: Bojan Bistrovic <bojanb at physics.odu.edu>
• Date: Fri, 17 Dec 1999 01:21:41 -0500 (EST)
• Organization: Old Dominion Universityaruba
• References: <83208o\$gik@smc.vnet.net>
• Sender: owner-wri-mathgroup at wolfram.com

```This might shed more light on your problem if not solve it. After typing
in your example, try the following:

In:= b=Unevaluated[a]

Out= {4,5,a,b,b,7,67,89}

In:= c=a

Out= {4,5,a,b,b,7,67,89}

The output of "a", "b" and "c" will be the same, but they aren't stored
in memory in the same way. This is how they are stored internaly:

In:= Definition[a]

Out= {4,5,Sequence["a","b","c"],7,67,89}

In:= Definition[b]

Out= a

In:= Definition[c]

Out= {4,5,a,b,b,7,67,89}

Therefore, doing something like a[]=Pi doesn't work because Set has
attribute HoldFirst which prevents the left hand side of the equation
from being evaluated. You might think that something like

Unprotect[Set];
ClearAttributes[Set, HoldFirst]

would help, but it won't (I've tried); it will start producing weird
error messages. Functions like Part, Length or FullForm evaluate their
arguments first so you don't see the Sequence fuction. On the other
hand, commands like  Unevaluated[a] or Hold[a] will prevent the List
{4,5,...} from being assigned to the symbol "a" so the result will be
just Unevaluated[a] or Hold[a]

Last, but not least, there's a "trick" that will solve your problem
(altho it probably involves copying). Try following:

In:= a=a

Out= {4,5,a,b,b,7,67,89}

In:= Definition[a]

Out= {4,5,a,b,b,7,67,89}

What happened is this: Set evaluates the right-hand side of the
aquation, but not the left-hand; so the right-hand "a" was evaluated,
replaced by {4,5,a,b,b,7,67,89} and then assigned to the unevaluated
symbol "a" on the left-hand side.

```

• Prev by Date: Re: Re: modified list interval calculation
• Next by Date: Re: Adding interpolated values to a list
• Previous by thread: Q: efficient in-place list element replacement?
• Next by thread: Re: Q: efficient in-place list element replacement?