MathGroup Archive 2001

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

Search the Archive

RE: Unevaluated Expression


Hermann,

Without HoldAll or HoldFirst Mathematica evaluates the argument(s) before
they are substituted into the function. With HoldAll Mathematica substitutes
into the function without evaluation, but then goes on to evaluate. With a
more elaborate routine you could intercept the argument and put it in your
own Hold before using it. Using Trace shows what happens in this case. I
define two functions, one with a HoldAll and one without.

a = 10;

Attributes[f] = {HoldAll};
f[x_] := x^2
g[x_] := x^2

g[a] // Trace
{{a, 10}, g[10], 10^2, 100}

f[a] // Trace
{f[a], a^2, {a, 10}, 10^2, 100}

But, more to the point, if you want to deal will a as a symbol, don't set a
value for it. It is generally far easier to insert actual values using
substitution rules. For example, here is a slight extension of your case.

ClearAll[a, f];
data = {a -> 10, b -> 2};
f[x_, y_] := x^2 + y^2

f[a, b]
% /. data
a^2 + b^2
104

David Park
djmp at earthlink.net
http://home.earthlink.net/~djmp/

>
> Dear Mathematica users,
>
> a = 10;
> Unprotect[ToString]; SetAttributes[ToString, HoldAll]
> ToString[a]//FullForm
> gives (as desired) "a", not 10
>
>
> Then I wanted to prevent the argument in f[x_] := x^2  from
> getting evaluated.
>
> Attributes[f] = {HoldAll}
> f[x_] := x^2
> f[Unevaluated[a]]
> gives 100.
>
> The longwinded code
> ToExpression[ToString[f[a]], InputForm, Hold] /. Hold[f[nr_]] -> nr
> or
> First[Level[Composition[Hold][f[#]],{-1}]]& /@ {a,10}
>
> at least manages to give 10 instead of 100.
>
> Is there really no way to prevent the argument in f[x] from firing ?
> (I use Mathematica 4.1 for Windows).
>
>
> With best regards
> Hermann Meier
>
>
>
>
>
>
>
>
>
>



  • Prev by Date: Re: Queries: notebook, matrices
  • Next by Date: RE: RE: 1. Input of screen coordinates; 2. Fast graphics
  • Previous by thread: Re: Unevaluated Expression
  • Next by thread: Re: Unevaluated Expression