MathGroup Archive 2008

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

Search the Archive

Re: Evaluate part of rhs of RuleDelayed

Eugene Kirpichov wrote:

> I am writing a lazy recursive function (actually, a tree of UI widgets
> that is too large to be constructed at once).
> Consider, for example, the following code:
> SomeComplexFunction[a_] := Module[{x},
>   x = a*a;
>   {value :> SomeComplexFunction[x + a]}]
> The value of SomeComplexFunction[5] is {value :>
> SomeComplexFunction[x$7418 + 5]} and seemingly can'd be used for
> anything useful.
> Question:
> How should I fix the code of SomeComplexFunction so that the value of
> SomeComplexFunction[5] be {value :> SomeComplexFunction[30]} ?
> Unfortunately, this one
> SomeComplexFunction[a_] := Module[{x},
>   x = a*a;
>   {value :> SomeComplexFunction[Evaluate[x + a]]}]
> yields {value :> SomeComplexFunction[Evaluate[x$7421 + 5]]}

What you want to do is evaluating the argument of a function without 
evaluating the function itself (You use RuleDelayed to prevent the 
evaluation of the function and the infinite loop that would result in 
doing so).

You can use ReplacePart to modify selectively any parts of the rule, so 
you can replace the unevaluated argument by its result. (Note that the 
right sequence of indices can be found by using FullForm or TreeForm, 
for instance.)

   SomeComplexFunction[a_] :=
     Module[{x}, x = a*a;
       ReplacePart[{value :> SomeComplexFunction[x + a]},
         {1, 2, 1} -> x + a]]


   {value :> SomeComplexFunction[30]}

-- Jean-Marc

  • Prev by Date: Re: Can not run Mathematica 6.0.3 on Fedora 9
  • Next by Date: Re: Incoherent value for partial derivative
  • Previous by thread: Re: Evaluate part of rhs of RuleDelayed
  • Next by thread: Find count of binary number pattern within concatenated number