RE: Input Line Evaluation
- To: mathgroup at smc.vnet.net
- Subject: [mg42737] RE: [mg42721] Input Line Evaluation
- From: "Wolf, Hartmut" <Hartmut.Wolf at t-systems.com>
- Date: Thu, 24 Jul 2003 04:10:42 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
>-----Original Message----- >From: jmt [mailto:jmt at dxdydz.net] To: mathgroup at smc.vnet.net >Sent: Wednesday, July 23, 2003 6:25 AM >To: mathgroup at smc.vnet.net >Subject: [mg42737] [mg42721] Input Line Evaluation > > >I am trying to have every input line commands managed by >Mathematica and sent >to another program. > >I thought I could use $Pre and a function t that performs a >redirection to the >other program, something like : > >SetAttributes[$Pre,HoldAll] >$Pre:=(t[#];Identity[#])& > >but, with for instance, t = Print[Unevaluated[#]]& > >1+1 (ENTER TO EVALUATE) >2 >Out[]= >2 > >How can I have the t function get the full input line ? > >Any ideas ? > >jmt > > I'm not dealing with your final problem (perhaps consider $PreRead ?), but show that the treatment of Hold attributes is not sufficient here; observe: In[1]:= Print[Unevaluated[1 + 1]] >From In[1]:= 1 + 1 ...this works as expected In[2]:= SetAttributes[pp, HoldAll] In[3]:= pp[1 + 1] Out[3]= pp[1 + 1] ...(our model for $Pre) works as expected. Now we assign a function: In[4]:= pp = (Print[Unevaluated[#]]; #) &; In[5]:= pp[1 + 1] >From In[5]:= 2 Out[5]= 2 ... 1+1 becomes evaluated before printing, where? a Trace might show you, but we might image what is done: (1) pp is evaluated, and evaluates to (Print[Unevaluated[#]]; #)&, (Set or SetDelayed, doesn't matter) such we get: (2) (Print[Unevaluated[#]]; #)&[1 + 1] Now, this function has no Hold attributes, such next (3) 1 + 1 evaluates to 2 (4) (Print[Unevaluated[2]]; 2) is executed (5) Print[Unevaluated[2]] is executed, (5) Print[2] (6) Null (and 2 is printed as a side-effect) (7) now 2 is met as next and last element of CompoundExpression, such that results to (8) 2 So, now it's clear what to do, use a function with Hold-attribute: In[7]:= pp = Function[expr, (Print[Unevaluated[expr]]; expr), {HoldAll}]; In[8]:= pp[1 + 1] >From In[8]:= 1 + 1 Out[8]= 2 Also we have seen: the attributes of pp (or $Pre) don't matter (as DownValues for pp are not involved); such In[9]:= ClearAttributes[pp, HoldAll] In[10]:= pp[1 + 1] >From In[10]:= 1 + 1 Out[10]= 2 -- Hartmut Wolf