       Re: Best practice passing expressions to functions

• To: mathgroup at smc.vnet.net
• Subject: [mg85317] Re: Best practice passing expressions to functions
• From: Grischika at mail.ru
• Date: Tue, 5 Feb 2008 19:47:47 -0500 (EST)
• References: <fo9gvu\$t1l\$1@smc.vnet.net>

```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:= f[x^2 + c, c]
Out= Function[x\$, c + x^2 /.c -> x\$]

In:= g
Out= 2 + x^2

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

In:= g
Out= 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