[Date Index]
[Thread Index]
[Author Index]
Re: Variable containing code
*To*: mathgroup at smc.vnet.net
*Subject*: [mg74977] Re: [mg74937] Variable containing code
*From*: Carl Woll <carlw at wolfram.com>
*Date*: Fri, 13 Apr 2007 02:05:27 -0400 (EDT)
*References*: <200704120847.EAA24473@smc.vnet.net> <461E1AE2.10008@wolfram.com> <461E27E1.1020406@metrohm.ch> <461E3773.2030403@wolfram.com> <461E3E65.1050309@metrohm.ch>
Daniel Huber wrote:
> Hello carl,
> thank's a lot, this was really helpfull.
> However, may I point out a mistake: the normal behaviour of a function
> is to strip "Unevaluated" inside the function body. You may convince
> yourself by e.g.:
> foo[x_]:=Print["x=",Hold[x]];
> foo[Unevaluated[Print["Hello"]]]
> Therefore, why does SetDelayed not strip "Unevaluated"?
Your paraphrase of how Unevaluated is treated is confusing you, and you
should look up evaluation order again. In your example below, you have
Set[ t2, Extract[OwnValues[t1], {1,2}, Unevaluated] ]
So, here is what Mathematica does. It looks at Set and notices that Set
is HoldFirst. So, Mathematica evaluates the second argument, but not the
first, arriving at
Set[ t2, Unevaluated[Print["Hello"]] ]
Now, at this point, Mathematica is done evaluating the arguments of Set,
and so it looks at the DownValues for Set, and sets t2 to
Unevaluated[Print["Hello"]], as you can check for yourself.
In your example, we have:
foo[ Unevaluated[Print["Hello"]] ]
Here Mathematica finds that foo has no Hold attributes, so it goes ahead
and evaluates the arguments. Evaluating an Unevaluated[Print["Hello"]]
argument yields Print["Hello"]. So, after Mathematica finishes
evaluating the foo arguments, it ends up with
foo[ Print["Hello"] ]
and now it is time to look for foo DownValues. I hope this clarifies why
there is an Unevaluated in the first example, but not the second.
Carl Woll
Wolfram Research
> Daniel
>
> Carl Woll wrote:
>
>> Daniel Huber wrote:
>>
>>> Hallo Carl,
>>> thank's a lot. I knew that there is some magic word around but I was
>>> not able to find it.
>>> However, this only solves half the problem. If I try:
>>> t1:=Print["Hello"];
>>> Set[ t2, Extract[OwnValues[t1],{1,2},Unevaluated] ];
>>> t2 does not evaluate the code, the reason is:
>>> Although OwnValues[t1] and OwnValues[t2] return similar results,
>>> Information (??) returns different results, namely: t1:=Print[Hello]
>>> and t2=Unevaluated[Print[Hello]]
>>> It looks like Set did not strip the "Unevaluated". Does Set not
>>> behave like a normal function?
>>
>>
>> Well, presumably you meant to use SetDelayed instead of Set. However,
>> if you replace Set (or SetDelayed) by foo, you get the same behavior:
>>
>> In[6]:= foo[t2, Extract[OwnValues[t1], {1, 2}, Unevaluated]]
>>
>> Out[6]= foo[t2, Unevaluated[Print["Hello"]]]
>>
>> So, no functions strip the Unevaluated. There are many ways to get t2
>> to have the same Information as t1, here are a couple:
>>
>> OwnValues[t1] /. _HoldPattern->t2 /. RuleDelayed->SetDelayed
>>
>> Function[rhs, t2:=rhs,HoldAll]@@Extract[OwnValues[t1],{1,2},Hold]
>>
>> Carl Woll
>> Wolfram Research
>>
>>> Daniel
>>
>>
>>>
>>> Carl Woll wrote:
>>>
>>>> dh wrote:
>>>>
>>>>> Hello,
>>>>>
>>>>> how can one get at the value of a variable if this value is
>>>>> executable
>>>>> code? E.g. consider variable t1:= Print["Hello"]. How can I set the
>>>>> value of a second variable t2 to the value of t1 without executing
>>>>> the code?
>>>>>
>>>>> Daniel
>>>>>
>>>>>
>>>>>
>>>>>
>>>> Try OwnValues[t1]
>>>>
>>>> Carl Woll
>>>> Wolfram Research
>>>>
>>>>
>>>
>>>
>>
>>
>>
>
>
Prev by Date:
**Re: About Table**
Next by Date:
**more in Assumptions**
Previous by thread:
**Re: Variable containing code**
Next by thread:
**Re: Variable containing code**
| |