Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #14170

Re: "Programming With Mathematica" Exercise help

From Roland Franzius <roland.franzius@uos.de>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: "Programming With Mathematica" Exercise help
Date 2013-04-13 06:02 +0000
Message-ID <kkasd1$ril$1@smc.vnet.net> (permalink)
References <kk5r6r$ric$1@smc.vnet.net>
Organization Time-Warner Telecom

Show all headers | View raw


Am 11.04.2013 10:11, schrieb plank.in.sequim@gmail.com:
> 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!

In order to block evaluation during input use "Unevaluated"

z = 11;
a = 9;
Unevaluated[z + 3] /. z -> a

"Hold" blocks evaluation completely, so its use needs an additional 
"ReleaseHold" after.

#z = 11;
a = 9;
Hold[z +3] /. z -> a //ReleaseHold

Of course you may use any other self made Identity function with 
attributes, Hold, HoldAll, HoldComplete,HoldAllComplete

SetAttributes[id,HoldAll]
id=Identity

id[z]/.z->a

-- 

Roland Franzius

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread


Thread

"Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-11 08:11 +0000
  Re: "Programming With Mathematica" Exercise help Peter Pein <petsie@dordos.net> - 2013-04-12 06:16 +0000
    Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
  Re: "Programming With Mathematica" Exercise help spamslayer19@gmail.com - 2013-04-12 06:15 +0000
  Re: "Programming With Mathematica" Exercise help drallenbio@gmail.com - 2013-04-12 06:15 +0000
    Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
  Re: "Programming With Mathematica" Exercise help Roland Franzius <roland.franzius@uos.de> - 2013-04-13 06:02 +0000
    Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000
  Re: "Programming With Mathematica" Exercise help plank.in.sequim@gmail.com - 2013-04-14 00:36 +0000

csiph-web