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

MathGroup Archive 2007

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

Search the Archive

Re: What is a good way of returning a function from a Module[]?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg83387] Re: [mg83287] What is a good way of returning a function from a Module[]?
  • From: DrMajorBob <drmajorbob at bigfoot.com>
  • Date: Mon, 19 Nov 2007 06:12:56 -0500 (EST)
  • References: <27565761.1195340077377.JavaMail.root@m35>
  • Reply-to: drmajorbob at bigfoot.com

Module[{a, b}, a = 1; b = a + 1; Function[{c}, Evaluate[a + b + c]]]

Function[{c$}, 3 + c$]

Bobby

On Sat, 17 Nov 2007 04:10:37 -0600, Szabolcs Horvát <szhorvat at gmail.com>  
wrote:

> What is an elegant way of returning a function from a Module[]?
>
> Module[{a, b}, a = 1; b = a+1; (a+b+#)&] does not work because
> Function[] holds its arguments.
>
> The best way I could find was
>
> Module[{p, q}, p = 1; q = p+1; With[{a = p, b = q}, (a+b+#)&]]
>
> Is there a nicer/more concise way of doing this?  This is a simplified
> example, but the important points are:
>
> 1. The returned function may depend on more than one parameter ('a' and
> 'b'; let's forget that in this case their sum could have been computed
> inside the Module[]).
>
> 2. 'a' and 'b' are not calculated independently.  The value of 'a' is
> needed to find 'b'
>
> Szabolcs
>
>



-- 
DrMajorBob at bigfoot.com


  • Prev by Date: Re: classes.m
  • Next by Date: ordered positions (OrderedPosition?)
  • Previous by thread: Re: What is a good way of returning a function from a Module[]?
  • Next by thread: Re: What is a good way of returning a function from a Module[]?