Re: Transform list of numbers into pure functions using replace rule

*To*: mathgroup at smc.vnet.net*Subject*: [mg89490] Re: Transform list of numbers into pure functions using replace rule*From*: "Jean-Marc Gulliet" <jeanmarc.gulliet at gmail.com>*Date*: Tue, 10 Jun 2008 07:15:21 -0400 (EDT)*References*: <g2lb8t$964$1@smc.vnet.net> <484E43A8.9080506@gmail.com>

On Tue, Jun 10, 2008 at 11:42 AM, Malcolm Davidson <mwjdavidson at googlemail.com> wrote: > Hello, > > Many thanks for your very quick reply. Your solution doesn't quite > work because I would also like to transform lists of this type (it was > in the first example I gave): > > In[3]:= {1, 2, f[x], 3 &} /. x_ /; NumberQ[x] -> (x &) > > Out[3]= {1 &, 2 &, f[x], 3 & &} > > The problem is the "3&&" which I would like to be "3&". > > Any magic that you can propose ? Well, this might not be magic, but it will do the work! You want to apply the rule to the first level of the list and not to every part of the list. Think of a list and its components as a tree: 1 and 2 are atomic expression whereas 3& is itself a tree made of two atomic expression: 3 and &. Evaluating TreeForm[{1, 2, f[x], 3 &}] should clarify the above statement. Now, /. is a shortcut for *Replace/All*, which attempts to transform each subpart of an expression by applying a rule (or list of rule) at every level of the expression. What you need is the function *Replace* that accepts a third argument to control to which level the rule must be applied. In your case, you want to transform the first level only. So, Replace[{1, 2, f[x], 3 &}, x_ /; NumberQ[x] -> (x &), {1}] {1 &, 2 &, f[x], 3 &} HTH, Jean-Marc > many thanks in any case > > Malcolm > > 2008/6/10 Jean-Marc Gulliet <jeanmarc.gulliet at gmail.com>: >> Mac wrote: >>> >>> Hello, >>> >>> I'm developed a program to invert satellite observations into >>> geophysical variables such as forest biomass. One of the input >>> parameters are the errors in the observation channels which can be >>> either constant or a function. To be consistent the program expects >>> all errors to be functions but they can also be constant functions >>> e.g. if the error is 4 then the input parameter is "4&". >>> >>> I've however hit a problem with the replacement rule that allows me to >>> transform all input errors into a function. The following works as >>> expected in that constants in the list are transformed into virtual >>> functions and functions in the list are left alone. >>> >>> In[40]:= If[NumberQ[#], ToExpression[ToString[#] <> "&"], #] & /@ {1, >>> 2, 3 &, f[x]} >>> >>> Out[40]= {1 &, 2 &, 3 &, f[x]} >>> >>> However, I cannot find a replacement rule to do the same job. Here is >>> one try: >>> >>> In[39]:= {1, 2, f[x]} /. x_ /; NumberQ -> Hold[x &] >>> >>> Out[39]= {1, 2, f[x]} >>> >>> Any help would be appreciated. >>> >>> Mac >>> >>> >> >> Either one of the following expressions returns the desired result {1 &, 2 >> &, f[x]}. >> >> >> {1, 2, f[x]} /. x_ /; NumberQ[x] -> (x &) >> >> {1, 2, f[x]} /. x_ /; NumberQ[x] -> Function[x] >> >> >> Regards, >> -- Jean-Marc >> > -- Jean-Marc