Re: C/Fortran-like #include functionality for large expressions?
- To: mathgroup at smc.vnet.net
- Subject: [mg123570] Re: C/Fortran-like #include functionality for large expressions?
- From: David Bailey <dave at removedbailey.co.uk>
- Date: Sun, 11 Dec 2011 03:49:12 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jbvjbn$isb$1@smc.vnet.net>
On 10/12/2011 12:29, Frank Iannarilli wrote:
> Hi All,
>
>
> Is there any C #include or similarly Fortran INCLUDE functionality I can gain within Mathematica? The purpose is to enhance readability of very large function definitions or Manipulate[] invocations, by splicing-in "chunks" of code (expressions) defined in separate cells.
>
> Here's an example of an attempt that doesn't work:
>
> testInclude[] :=
> Module[{a}, a = 2.; ToExpression[p]; Print[a]]
>
> p = "a=3." // The expression chunk to "include"
>
> testInclude[]
> // would like this to return "3", but stuck returning "2".
>
> Thanks,
> Frank
>
>
Your example didn't work because a is local to the Module, so in effect
you executed a line of the form:
a$17=2.;ToExpression["a=3."];Print[a$17]
(The number 17 is arbitrary - see the help for the Module function)
If the expression that you want to include does not contain variables
that should be local, the solution is trivial - just assign them to a
variable:
subst=Hold[a=3.]
(no need to convert to string) and insert the expression as needed in
your function as ReleaseHold[subst].
Getting the replacement a -> a$17 would be a lot harder, and is
essentially pointless because a should presumably be global.
David Bailey
http://www.dbaileyconsultancy.co.uk