Re: setdelayed function
- To: mathgroup at smc.vnet.net
- Subject: [mg27975] Re: [mg27953] setdelayed function
- From: Andrzej Kozlowski <andrzej at platon.c.u-tokyo.ac.jp>
- Date: Tue, 27 Mar 2001 01:26:11 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
This is not really aproblem with SetDelayed but simply due to the fact that you have defined a function (test1) which evaluates its arguments. When you evaluate test1[p], p is evaluated to 90784 - 16800 Cos[40 °] before your function test1 is used. There are many ways to achieve what you want with SetDelayed. It seems to me that the simplest way is to insert Hold or HoldForm in your definition of p. Thus: In[1]:= p = HoldForm[28^2 + 300^2 - 2*28*300*Cos[40 Degree]]; In[2]:= test1[x_] := Level[x, {2}] In[3]:= test1[p] Out[3]= {784,90000,-16800 Cos[40 Degree]} You can also achieve your effect in various ways without using Hold or HoldForm if you give your function test1 the HoldFirst or HoldAll attribute, e.g.: In[5]:= Clear[p,test1] In[6]:= p :=28^2 + 300^2 - 2*28*300*Cos[40 Degree]; In[7]:= SetAttributes[test1,HoldFirst] In[8]:= test1[x_]:=Rest[Level[OwnValues[x],{3}]] In[9]:= test1[p] Out[9]= {784,90000,-16800 Cos[40 Degree]} -- Andrzej Kozlowski Toyama International University JAPAN http://platon.c.u-tokyo.ac.jp/andrzej/ on 3/26/01 11:27 AM, Pek at phsoh at alum.mit.edu wrote: > Hi, > I am trying the setdelayed function but I didn't understand why the > solution differs between the two methods below: > > Method 1 with setdelayed > In[4]:= > p := 28^2 + 300^2 - 2*28*300*Cos[40 Degree] > > In[2]:= > test1[x_] := Level[Unevaluated[x], 1] > > In[5]:= > test1[p] > > Out[5]= > {90784, -16800 Cos[40 Degree]} > > Method 2 without setdelayed > In[9]:= > Level[Unevaluated[28^2 + 300^2 - 2*28*300*Cos[40 Degree]], 1] > > Out[9]= > {784, 90000, -16800 Cos[40 Degree]} > > > Finally, Out[9] is what is I wanted but I would like to get this > result using setdelayed. Did I use setdelayed incorrectly? > > Would really appreciate your help. > > Thanks. > > Pek > > >