       Re: Replacing a part of a list/matrix?

• Subject: [mg2215] Re: [mg2180] Replacing a part of a list/matrix?
• From: hay at haystack.demon.co.uk (Allan Hayes)
• Date: Mon, 16 Oct 1995 15:54:39 GMT
• Approved: usenet@wri.com
• Distribution: local
• Newsgroups: wri.mathgroup
• Organization: Wolfram Research, Inc.
• Sender: daemon at wri.com ( )

```In [mg2180] Replacing a part of a list/matrix?
"O. Lee" <olee at ripco.com> writes

> I am trying to assign a value to a matrix position, such as:
>
>        pop[[ x,y ]] = value
>
> in a module function. When I try manually assigning a value
> to the matrix position outside of the module, it works. When
> I run the function with the statement, Mathematica gives me
> the following error message:
>
> Part::setps:
>
>        {{1, 1, 0, 0}, <<1>>}
>          in assignment of part is not a symbol.
>
> ............
> I appreciate any help. Thanks.

I hope that the following examples may help.

First define pop

In:=
pop = {1,2};

Example 1:

In:=
foo[p_] := p[] = 4                     (**)
In:=
{foo[pop],pop}
Part::setps: {1, 2} in assignment of part is not a symbol.
Out=
{4, {1, 2}}

The crucial parts of the evaluation of foo[pop] are
0. (**) is used with p_ replaced by pop;
1. pop is evaluated on the *left* to give ,{1,2};
2. then the symbol  p on the right is replaced by this value
to give
{1,2}[] = 4
3. the evaluation of {1,2}[] = 4 causes the message.
Since the symbol pop never got to the right side its value is unchanged

Example 2:

To make a symbol with value {1,4} on the right , we can do this:

In:=
foo[p_] := {p2 = p; p2[] = 4, p2}
In:=
{foo[pop],pop}
Out=
{{4, {1, 4}}, {1, 2}}

Again, the value of pop is not changed.

Example 3:

To actually change the value of the symbol pop, we must prevent the
evaluation of anything put in place of p_ on the left.
This can be done by giving foo the attribute HoldAll (or HoldFirst)

In:=
SetAttributes[foo, HoldAll]
In:=
foo[p_] := {p[] = 4, p}            (****)
In:=
{foo[pop], pop}
Out=
{{4, {1, 4}}, {1, 4}}

Here the symbols p on the right of (****) have been replaced by the
*symbol* pop, not its value; so that we evaluate  pop[] = 4; and
after this pop  has value {1,4}

Allan Hayes
hay at haystack.demon.co.uk

```

• Prev by Date: Formating output of Modules
• Next by Date: Re: Solid Geometry Problem
• Previous by thread: Re: Replacing a part of a list/matrix?
• Next by thread: Re: Replacing a part of a list/matrix?