Re: C/Fortran-like #include functionality for large expressions?
- To: mathgroup at smc.vnet.net
- Subject: [mg123548] Re: C/Fortran-like #include functionality for large expressions?
- From: "Oleksandr Rasputinov" <oleksandr_rasputinov at hmamail.com>
- Date: Sun, 11 Dec 2011 03:45:13 -0500 (EST)
- Delivered-to: l-mathgroup@mail-archive0.wolfram.com
- References: <jbvjbn$isb$1@smc.vnet.net>
On Sat, 10 Dec 2011 12:29:43 -0000, Frank Iannarilli <frankeye at cox.net>
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
>
It is possible, but not with Module, because Module creates new local
variables under different names (so a always refers to Global`a). You can
use Block instead to get the behaviour you want. The string/ToExpression
indirection is not needed, and personally I dislike delayed definitions
being made using an unnecessary function call pattern as required in other
languages, so I changed these too:
testInclude := Block[{a}, a = 2.; p; Print[a]]
testInclude
prints:
2.
Now, we try
p := a = 3; testInclude
which prints:
3.
(you can get 2. again by evaluating p =. to clear the "include".)
Also note that the C++ comment syntax "// comment" has a different meaning
in Mathematica, so I would encourage you to use Mathematica "(* comment
*)" comments in code examples.