Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2008

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

Search the Archive

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[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