       RE: Pure Functions in rules

• To: mathgroup at smc.vnet.net
• Subject: [mg15976] RE: [mg15932] Pure Functions in rules
• From: "Ersek, Ted R" <ErsekTR at navair.navy.mil>
• Date: Fri, 19 Feb 1999 03:26:59 -0500
• Sender: owner-wri-mathgroup at wolfram.com

```When you use  {1,2,3}/. (m_List->(2*#& /@ m))
(2*#& /@ m) evaluates before the replacement is made.
To see what it evaluates to see Out.

In:=
Clear[m];
2*#&/@m
Out=
m

So what you end up doing is simply
{1,2,3}/.m_List->m

________________________________
of (->) as in In.  When you do it
this way (2*#& /@ m) doesn't evaluate
until  m={1,2,3}.

In:=
{1,2,3}/.(m_List:>(2*#& /@ m))
Out=
{2,4,6}

_____________________
Now I give you another reason to
prefer (:>) over (->).  Suppose (m) has a global value as below.

In:=
m=Log;
In:=

....
....
Then you do a lot of work, and forget about your previous use of (m).
....
....

Now you try to use (->) in a case where it worked before, and your stunned
by the result at Out.  I have yet to find a book about Mathematica that
this point.

In:=
{1,2,3}/.m_List->2*m
Out=
2 Log
_______________________

If you use (:>) you get the right result even
though (m) has a value assigned to it.

In:=
{1,2,3}/.m_List:>2*m
Out=
{2,4,6}

In general you shouldn't use (lhs->rhs) if (lhs) contains a named pattern
and the name of the pattern is used in (rhs).  Notice the next line comes
out right because (m) isn't used on the right hand side of (->).  You only
have trouble when the name of the pattern is used on the right hand side of
(->).

In:=
{1,2,3,4,5,6}/.m_?EvenQ->0
Out=
{1,0,3,0,5,0}

Actually there is a way to always get the right result using (lhs->rhs).
You have to use Block (as below), but this is more complicated than
(lhs:>rhs).

In:=
Block[{m},{1,2,3}/.m_List->2*m]
Out=
{2,4,6}

In:=
m
Out=
Log

At Out we got the right result even though (m) still has the value
Log.

Regards,
Ted Ersek

_______________________

Will Self wrote:

It appears that I cannot depend on using a pure function
in a pattern-matching rule.

Here I am trying to convince reluctant students that they're
better off learning to use Mathematica than doing things
by hand, and we run across something like this, and in a
much more complicated situation where the trouble was
hard to isolate.

I am quite frankly incensed by the behavior shown in
In/Out 80, below.  Look at these examples:

In:=     {1,2,3}/.(m_List->7)
Out=    7

In:=     {1,2,3}/.(m_List->(2*m))
Out=    {2,4,6}

In:=     2*#& /@ {1,2,3}
Out=    {2,4,6}

In:=     f[m_List]:=2*#& /@ m

In:=     f[{1,2,3}]
Out=    {2,4,6}

In:=     {1,2,3}/.m_List->f[m]
Out=    {2,4,6}

Now try this:

In:=     {1,2,3}/.(m_List->(2*#& /@ m))
Out=    {1,2,3}

Does anyone (say, at WRI for example) care to comment on
this?

Will Self

```

• Prev by Date: RE: Re: Compiling a Module
• Next by Date: Re: Losing Mathematica password
• Previous by thread: Re: Pure Functions in rules
• Next by thread: Re: Pure Functions in rules