MathGroup Archive 2010

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

Search the Archive

Re: pure function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg113718] Re: pure function
  • From: Leonid Shifrin <lshifr at gmail.com>
  • Date: Tue, 9 Nov 2010 03:51:49 -0500 (EST)

Same thing -  use With inside the Module:

In[8]:=
Clear[getFunc];
getFunc :=
 Module[{a = 2, b = 3},
  With[{al = a + b},
   Function[{x}, al*x]]]


In[10]:= getFunc

Out[10]= Function[{x$}, 5 x$]

Regards,
Leonid


On Mon, Nov 8, 2010 at 1:40 PM, Stephan <stschiff80 at googlemail.com> wrote:

> Ah, I forgot a follow up:
>
> What if I don't want to use a constant like in the example I stated? What
> if I want to compute the local value of a in the module and return the
> computed value instead of the symbol? I am kind of looking for a general way
> to evaluate a part of an expression within the delayed evaluation of the
> function body.
>
> Thanks,
>
> Stephan
>
>
> Am 08.11.2010 um 09:20 schrieb Leonid Shifrin:
>
> > Stephan,
> >
> > Yes, sure, use With instead of Module:
> >
> > In[2]:== getFunc:==With[{a==2},Function[{x},a*x]]
> >
> > In[3]:== f==getFunc
> >
> > Out[3]== Function[{x$},2 x$]
> >
> > So, by using Module, you can create a closure whose state (variable <a>
> > in this case) comes from surrounding context but can be modified by it
> > at a later time. Using With gets you a textual substitution of a value
> that
> > a given expression had, at the moment when With was invoked.
> >
> > You can also accomplish your goal  somewhat differently, by redefining
> your getFunc:
> >
> > In[4]:==
> > Clear[getFunc];
> > getFunc[y_] :== Function[{x}, y*x]
> >
> > In[6]:== getFunc[2]
> >
> > Out[6]== Function[{x$}, 2 x$]
> >
> > The semantics of parameter-passing in Mathematica is somewhat similar to
> With:
> > parameters are textually substituted to the body before the body starts
> to evaluate.
> > There are a few subtle differences (related to name collision resolution
> in nested
> > scoping constructs), but they do not show up in this particular case.
> >
> > Regards,
> > Leonid
> >
> >
> > On Mon, Nov 8, 2010 at 11:37 AM, Stephan <stschiff80 at googlemail.com>
> wrote:
> > Hi,
> >
> > I have a function that returns a pure function:
> >
> > In[1]:== getFunc :== Module[{a==2}, Function[{x}, a * x]]
> >
> > In[2]:== f == getFunc
> >
> > Out[2]== Function[{x$}, a$57 x$]
> >
> >
> > Is there any way, to have the body of the returned function contain the
> a=
> ctual _value_ of the local variable a, instead of the _symbol_ ?
> >
> > So I would like the returned function to be written as
> >
> >        Function[{x$}, 2 x$]
> >
> > The reason is, that I would like to have a quick way to actually see the
> =
> value instead of digging out the local variable a$57...
> >
> > Thanks,
> >
> > Stephan
> >
> >
>
>


  • Prev by Date: Default Value for SetterBar
  • Next by Date: Re: Tag List Protected error
  • Previous by thread: Re: pure function
  • Next by thread: Re: pure function