MathGroup Archive 2001

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

Search the Archive

Re: setdelayed function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg27978] Re: setdelayed function
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Tue, 27 Mar 2001 01:26:14 -0500 (EST)
  • References: <99n5kd$igv@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Your use of SetDelayed is correct: it prevents the right side of the
following  evaluating

p:=28^2+300^2-2*28*300*Cos[40 Degree]

?p

        Global`p

        p := 28^2 + 300^2 - 2*28*300*Cos[40*Degree]

However, we need to to prevent the evaluation of  p  within test1[p]. So
give test1 the attribute HoldFirst.

SetAttributes[test1, HoldFirst];

Unfortunately this is still not good enough s

test1[x_]:=Level[Unevaluated[x],1]

test1[p]

        {}

This is because we end up evaluating Level[Unevaluated[p],1].

We need to use the value of p without evaluating it.
This can be done by using

OwnValues[p]

        {HoldPattern[p] :> 28^2 + 300^2 -
            2*28*300*Cos[40*Degree]}

Thus

test1[x_]:=Level[Hold[x]/.OwnValues[x],{2}]

test1[p]

        {784, 90000, -16800 Cos[40 Degree]}

--
Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565

"Pek" <phsoh at alum.mit.edu> wrote in message news:99n5kd$igv at smc.vnet.net...
> 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
>
>




  • Prev by Date: Re: Puzzled over (un)changing argument symbols in functions
  • Next by Date: Re: setdelayed function
  • Previous by thread: setdelayed function
  • Next by thread: Re: setdelayed function