MathGroup Archive 2008

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

Search the Archive

Re: Best practice passing expressions to functions


On 5 =C6=C5=D7, 13:23, Remo Aschwanden <rca... at yahoo.com> wrote:
> Hi
>
> I want to write procedures that accept expressions as parameters and be
> able define functions based on these expressions, i.e.
>
> f[exp_]:=Module[
> =9A =9A"g[x_] := exp "
> ]
>
> The code between the " " should clarify what I intend to do. Calling
> =9A =9Af[x^2-2x+4]
> should lead to the definition of a function (or pattern)
> =9A =9Ag[x_]:=x^2-2x+4;
>
> What's the best way to do this? How can I do this without being
> dependent on the variables used in the expressions ("x")?
>
> Thank you.
> Remo A.

Hello.
Try this defenition:

f[exp_, v_: x] := Module[{}, g = Function[x, exp /. v -> x]]

In[31]:= f[x^2 + c, c]
Out[31]= Function[x$, c + x^2 /.c -> x$]

In[32]:= g[2]
Out[32]= 2 + x^2

In[33]:= f[x^2 + c]
Out[33]= Function[x$, c + x^2 /.x -> x$]

In[34]:= g[2]
Out[34]= 4 + c

I do not know if it possible to avoid rule replacement in function
definition.


  • Prev by Date: NDSolve: PDE's with more than 2 indep. variables.
  • Next by Date: Re: Best practice passing expressions to functions
  • Previous by thread: Re: Best practice passing expressions to functions
  • Next by thread: Re: Best practice passing expressions to functions