MathGroup Archive 2013

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

Search the Archive

Re: "Programming With Mathematica" Exercise help

  • To: mathgroup at
  • Subject: [mg130450] Re: "Programming With Mathematica" Exercise help
  • From: "djmpark" <djmpark at>
  • Date: Fri, 12 Apr 2013 02:15:25 -0400 (EDT)
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • Delivered-to:
  • References: <19267322.194.1365668274681.JavaMail.root@m06>

Hold[(z = 11; a = 9; z + 3)] /. z -> a

Hold[a = 11; a = 9; a + 3] 

David Park
djmpark at 

From: at [ at] 

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


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

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

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!


  • Prev by Date: Re: Reading a file in reverse order --- How?
  • Next by Date: Re: Timing puzzle
  • Previous by thread: Re: "Programming With Mathematica" Exercise help
  • Next by thread: Re: "Programming With Mathematica" Exercise help