MathGroup Archive 2000

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

Search the Archive

RE: With[{software=Mathematica}, Frustration]

  • To: mathgroup at smc.vnet.net
  • Subject: [mg24317] RE: [mg24308] With[{software=Mathematica}, Frustration]
  • From: Wolf Hartmut <hwolf at debis.com>
  • Date: Sun, 9 Jul 2000 04:52:29 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

> -----Original Message-----
> From:	AES [SMTP:siegman at stanford.edu]
To: mathgroup at smc.vnet.net
> Sent:	Friday, July 07, 2000 6:12 AM
> To:	mathgroup at smc.vnet.net
> Subject:	[mg24308] With[{software=Mathematica}, Frustration]
> 
> 
[Hartmut Wolf]  The Mathematica Book of course is the central reference. For
programming (and deaper understanding) I recommend Roman Maeder's
"Programming in Mathematica"

> Pages 359-360 of The Mathematica Book says (admittedly, taken a little 
> out of context), 
> 
>    "You can think of  With  as a generalization of the /. operator. . ."
> 
[Hartmut Wolf]  This is exactly that kind of diction in the book I dislike.
However "you can think of..." should have warned you to *respect* the
context!
> and
> 
>    " With[{x=x0}, body]  works essentially like body /. x->x0 . . . "
[Hartmut Wolf]  Same here with "essentially"
>  
> Great, looks neat, let's try it for evaluating expressions without 
> permanently setting the variables in them:
> 
>    In[1]:= c = a b
> 
>    Out[1]= a b
> 
>    In[2]:= c
> 
>    Out[2]= a b
> 
>    In[10]:= c /. {a -> 2, b -> 3}
> 
>    Out[10]= 6
> 
>    In[3]:= With[{a = 2, b = 3}, c]
> 
>    Out[3]= a b
> 
> *Not* what I was hoping for  . . .
> 
[Hartmut Wolf]  

In[2]:= With[{a = 2, b = 3}, Evaluate[c]]
Out[2]= 6

will do what you want! Why? What was meant saying by the quotations is that
With gives a *textual* substitution for the given variables occuring in the
*unevaluated* body. Here within the unevaluated body (= literal c) a and b
are not visible; you make them so, if you force the evaluation of c before
the substitution is tried.

A warning added: the substitution by With works not the same way as by
ReplaceAll, With respects the scoping of Module, Function, and With (but not
of Block), ReplaceAll doesn't. And for ReplaceAll body is evaluated before
substitution. Admittedly you have to try to understand these points when
proceeding along your learning curve, but it's worth it! After some time
you'll agree to

	With[{software=Mathematica}, Exp[much joy!]]



  • Prev by Date: RE: Do loop var in deferred assignment
  • Next by Date: Re: difference equation
  • Previous by thread: Re: With[{software=Mathematica}, Frustration]
  • Next by thread: Re: With[{software=Mathematica}, Frustration]