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: [mg92820] Re: Conditional SetDelayed with Replacement
  • From: Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>
  • Date: Tue, 14 Oct 2008 04:57:09 -0400 (EDT)
  • Organization: The Open University, Milton Keynes, UK
  • 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).

*snip*

One possible way is as follows (We use a string expression.):

Clear[f]
Do[ToExpression["f[x_/;x<" <> ToString[i^2] <> "]=" <> ToString[i]], {i, 5}]
?f

Global`f

f[x_/;x<1]=1

f[x_/;x<4]=2

f[x_/;x<9]=3

f[x_/;x<16]=4

f[x_/;x<25]=5

Regards,
-- Jean-Marc


  • Prev by Date: Re: naive question
  • Next by Date: Re: Easiest Mathematica algorhitm needed
  • Previous by thread: Conditional SetDelayed with Replacement
  • Next by thread: Re: Conditional SetDelayed with Replacement