Re: Pure Functions in rules
- To: mathgroup at smc.vnet.net
- Subject: [mg16010] Re: Pure Functions in rules
- From: Paul Abbott <paul at physics.uwa.edu.au>
- Date: Sat, 20 Feb 1999 02:52:03 -0500
- Organization: University of Western Australia
- References: <7ag34l$aie@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Will Self wrote:
> It appears that I cannot depend on using a pure function
> in a pattern-matching rule.
Pure functions are not the problem in your example. (Sometimes you do
need to take care with parens because of the precedence of # and &).
> I am quite frankly incensed by the behavior shown in
> In/Out 80, below.
>
> In[80]:= {1,2,3}/.(m_List->(2*#& /@ m))
> Out[80]= {1,2,3}
See what happens when you replace -> with :>
{1, 2, 3} /. m_List :> (2*# & ) /@ m
I think you might now work out what is going on. The distinction
between -> and :> can be very important but somwhat difficult to
explain. My favorite example to explain the difference between immediate
and delayed replacement is to get the student to compare
{x,x,x} /. x -> Random[]
with
{x,x,x} /. x :> Random[]
Analogously, to explain the difference between Set and SetDelayed,
compare
r = Random[];
Table[r,{3}];
with
r := Random[];
Table[r,{3}];
Cheers,
Paul
____________________________________________________________________
Paul Abbott Phone: +61-8-9380-2734
Department of Physics Fax: +61-8-9380-1014
The University of Western Australia
Nedlands WA 6907 mailto:paul at physics.uwa.edu.au
AUSTRALIA http://www.physics.uwa.edu.au/~paul
God IS a weakly left-handed dice player
____________________________________________________________________