MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: "Programming With Mathematica" Exercise help

  • To: mathgroup at smc.vnet.net
  • Subject: [mg130457] Re: "Programming With Mathematica" Exercise help
  • From: Murray Eisenberg <murray at math.umass.edu>
  • Date: Fri, 12 Apr 2013 02:17:45 -0400 (EDT)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net
  • References: <20130411081349.074EF6B4E@smc.vnet.net>

Look at the page tutorial/Evaluation in the Documentation Center.

Briefly, unless the head h[e1, e2, . . ., en] has some kind of Hold* attribute , then after the head h is evaluated, each element e1, e2, etc., evaluated in turn.  Only then are the rules built-in or user-defined for h are applied.

   Attributes[ReplaceAll]
{Protected}

   FullForm[Hold[z + 3 /. z -> a]
Hold[ReplaceAll[Plus[z,3],Rule[z,a]]]

Thus the expression Plus[z,3] is evaluated first, before any replacement is made with z -> a. Since

  Plus[z,3]
14

there is nothing to replace there, so the Rule[z,a] has no actual effect.

On Apr 11, 2013, at 4:13 AM, plank.in.sequim at gmail.com wrote:

> I'm loving Paul Wellin's book "Programming with Mathematica: An Introduction" and am trying to diligently do all the exercises.  Most of them have answers in the back but I'm stuck on Section 4.2, Exercise 2 and there's no answer given.  It gives the following Mathematica code:
>
> z = 11;
> a = 9;
> z + 3 /. z -> a
>
> 14
>
> So "z+3" is being evaluated to 14 and then the substitution has no effect. He asks how to "use the Hold function in the compound expression to obtain a value of 12".  I don't seem to be able to get this to work.  My original thought was to hold z+3, but then the z in the replacement part gets evaluated so the replacement is actually 11->3 which doesn't match in the held z+3 expression.  In fact, if you replace "z+3" with Hold[11+3] then you'll end up with Hold[9+3].  Curiously, this works differently if you use replace.
>
> In:  Replace[Hold[11+3],11->9]
> Out: Replace[Hold[11+3],11->9]
>
> In:  Hold[11+3]/.11->9
> out: Hold[9+3]
>
> I thought these two were supposed to be equivalent so I'm a bit confused here.
>
> In any event, I've tried all the commonsense ideas I've had and then spent some time flailing about randomly with Hold's but nothing seems to work correctly.  Can anybody help me understand this?  Thanks!
>
> Darrell

---
Murray Eisenberg                                    murray at math.umass.edu
Mathematics & Statistics Dept.      
Lederle Graduate Research Tower            phone 413 549-1020 (H)
University of Massachusetts                               413 545-2838 (W)
710 North Pleasant Street                         fax   413 545-1801
Amherst, MA 01003-9305








  • Prev by Date: Re: "Programming With Mathematica" Exercise help
  • Next by Date: Running an external program inside a Do loop
  • Previous by thread: Re: "Programming With Mathematica" Exercise help
  • Next by thread: Re: "Programming With Mathematica" Exercise help