Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #7133

Re: C/Fortran-like #include functionality for large expressions?

From "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: C/Fortran-like #include functionality for large expressions?
Date 2011-12-11 08:46 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <jc1qli$ss5$1@smc.vnet.net> (permalink)
References <jbvjbn$isb$1@smc.vnet.net>

Show all headers | View raw


On Sat, 10 Dec 2011 12:29:43 -0000, Frank Iannarilli <frankeye@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.

Back to comp.soft-sys.math.mathematica | Previous | Next — Previous in thread | Find similar | Unroll thread


Thread

C/Fortran-like #include functionality for large expressions? Frank Iannarilli <frankeye@cox.net> - 2011-12-10 12:29 +0000
  Re: C/Fortran-like #include functionality for large expressions? David Bailey <dave@removedbailey.co.uk> - 2011-12-11 08:57 +0000
  Re: C/Fortran-like #include functionality for large expressions? "Oleksandr Rasputinov" <oleksandr_rasputinov@hmamail.com> - 2011-12-11 08:46 +0000

csiph-web