MathGroup Archive 2000

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

Search the Archive

Re: Defining a function within a module (2)

  • To: mathgroup at smc.vnet.net
  • Subject: [mg22483] Re: Defining a function within a module (2)
  • From: "Allan Hayes" <hay at haystack.demon.co.uk>
  • Date: Wed, 8 Mar 2000 02:22:22 -0500 (EST)
  • References: <89ssrf$e3v@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

David,

Further to my posting last night.
Here are some simple solutions


foo6[expr_] := Function @@ Hold[x, expr]

foo6[x^2]

Function[x, x^2]

Function is not the head of an expression for the

However, the entry in foo6 is evaluating before being passed

x = a;

foo6[x^2]

        Function[x, a^2]

(this also happens with all of the examples that you gave)

We can avoid this with

Attributes[foo6] = {HoldAll};

foo6[x^2]

        Function[x, x^2]

You might also like to consider

foo7[expr_] := Function[expr]

if you can accept using #, #1,... ## ..

(This slot function has no scoping variables!)

foo7[#^2]

        #1^2 &

You might still like to give foo7 the attribute HoldAll.


Allan
---------------------
Allan Hayes
Mathematica Training and Consulting
Leicester UK
www.haystack.demon.co.uk
hay at haystack.demon.co.uk
Voice: +44 (0)116 271 4198
Fax: +44 (0)870 164 0565



"David Park" <djmp at earthlink.net> wrote in message
news:89ssrf$e3v at smc.vnet.net...
> Dear MathGroup,
>
> I wish to define and use a function within a module, using an expression
passed to
> the module. This is a simpler example, where I simply return the function.
If I use
> the mkfun routine, described in Section 2.6.5 of the Mathematica Book,
everything
> works. Here x is a global variable without a value, and expr is an
expression
> containing x.
>
> foo1[expr_] :=
>   Module[{mkfun},
>     mkfun[x_, body_] := Function[x, body];
>     mkfun[x, expr]
>     ]
>
> foo1[x^2]
> Function[x, x^2]
>
> But, since mkfun simply substitutes x and body into the unevaluated
Function, the
> following should also work.
>
> foo2[expr_] :=
>   Module[{g},
>     g = Hold[Function[x, body]] /. body -> expr;
>     ReleaseHold[g]
>     ]
>
> foo2[x^2]
> Function[x, x^2]
>
> It does, but body is a global variable and so the routine will fail if
body has a
> value. Therefore, make body internal to the module.
>
> foo3[expr_] :=
>   Module[{g, body},
>     g = Hold[Function[x, body]] /. body -> expr;
>     ReleaseHold[g]
>     ]
>
> foo3[x^2]
> Function[x$, x^2]
>
> This does not work because the variable name has been changed. Why was it
changed
> even though it was within a Hold? Why did making body a Module variable
rather than a
> global variable cause it to be changed? But in the following routine, we
can change
> x$ back to x.
>
> foo4[expr_] :=
>   Module[{g, body},
>     g = Hold[Function[x, body]] /. body -> expr;
>     ReleaseHold[g] /. x$ -> x
>     ]
>
> foo4[x^2]
> Function[x, x^2]
>
> In fact, we don't even need the Hold, since it didn't work anyway.
>
> foo5[expr_] :=
>   Module[{},
>     Function[x, expr] /. x$ -> x
>     ]
>
> And here is an even simpler method for obtaining our function.
>
> foo6[expr_] :=
>   Module[{g},
>     g[x, expr] /. g -> Function
>     ]
>
> foo6[x^2]
> Function[x, x^2]
>
> I wish I had a better understanding of when Function does and does not
change the
> variable name.
>
> David Park
> djmp at earthlink.net
> http://home.earthlink.net/~djmp/
>
>



  • Prev by Date: Re: Simplifying Problems
  • Next by Date: RealTime3D for Macs
  • Previous by thread: Re: Defining a function within a module
  • Next by thread: RealTime3D for Macs