Drexel dragonThe Math ForumDonate to the Math Forum



Search All of the Math Forum:

Views expressed in these public forums are not endorsed by Drexel University or The Math Forum.


Math Forum » Discussions » Software » comp.soft-sys.math.mathematica

Topic: "Programming With Mathematica" Exercise help
Replies: 2   Last Post: Apr 12, 2013 2:16 AM

Advanced Search

Back to Topic List Back to Topic List Jump to Tree View Jump to Tree View   Messages: [ Previous | Next ]
hmichel@cox.net

Posts: 122
Registered: 1/29/05
Re: "Programming With Mathematica" Exercise help
Posted: Apr 12, 2013 2:15 AM
  Click to see the message monospaced in plain text Plain Text   Click to reply to this topic Reply

Darrell

I put most expressions in FullForm or close to FullForm

The following with no Hold

In[1]:= Clear[z,a];
Set[z,11];
Set[a,9];
ReplaceAll[z+3,Rule[z,a]]
Out[4]= 14

Now with Hold
In[5]:= Clear[z,a];
Hold[Set[z,11]];
Set[a,9];
ReplaceAll[z+3,Rule[z,a]]
Out[8]= 12

As you pointed out in the clipping of the exercise question, "use the Hold
.. in the compound expression .. value of 12". The solution is in the
question.

So I put a Hold on z where z is being initially Set in the compound
expression.

Hans

-----Original Message-----
From: plank.in.sequim@gmail.com [mailto:plank.in.sequim@gmail.com]
Sent: Thursday, April 11, 2013 3:14 AM
Subject: "Programming With Mathematica" Exercise help

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






Point your RSS reader here for a feed of the latest messages in this topic.

[Privacy Policy] [Terms of Use]

© Drexel University 1994-2014. All Rights Reserved.
The Math Forum is a research and educational enterprise of the Drexel University School of Education.