Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

Re: Conditional SetDelayed with Replacement

  • To: mathgroup at smc.vnet.net
  • Subject: [mg92841] Re: Conditional SetDelayed with Replacement
  • From: Albert Retey <awnl at gmx-topmail.de>
  • Date: Tue, 14 Oct 2008 05:01:10 -0400 (EDT)
  • References: <gcv7d5$e1i$1@smc.vnet.net>

Raffy wrote:
> I've been having difficulty getting the following code to work as
> desired.
> 
> Clear[f];
> Do[f[x_ /; x < i^2] = i, {i, 5}];
> 
> The problem should be obvious: the i^2 is not be evaluated (since its
> held by SetDelayed).
> 
> The only solution I've found thus far, has been doing the following:
> 
> Clear[f];
> (f[x_ /; x < #^2] = #)& /@ Range[5]
> or
> Do[(f[x_ /; x < #^2] = #)&[i], {i, 5}];
> 
> What kind of Hold, ReleaseHold, Evalulate, or Pattern Replacement
> would be needed to get the first example working without using a pure-
> function for replacement?
> 
the same question has been asked many times in this group, e.g.
yesterday in a variation with Buttons. Here is a solution which I think
is clear and general:

Do[With[{i = i}, f[x_ /; x < i^2] = i], {i, 5}]


hth,

albert


  • Prev by Date: Re: Exclude O[] from Series[] for Solve[] in Mathematica
  • Next by Date: Re: Easiest Mathematica algorhitm needed
  • Previous by thread: Re: Conditional SetDelayed with Replacement
  • Next by thread: Re: Conditional SetDelayed with Replacement