Re: functions inside Module
- To: mathgroup at smc.vnet.net
- Subject: [mg38302] Re: functions inside Module
- From: "Allan Hayes" <hay at haystack.demon.co.uk>
- Date: Thu, 12 Dec 2002 01:31:28 -0500 (EST)
- References: <at4auf$ebd$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Gianluca Gorni" <gorni at dimi.uniud.it> wrote in message news:at4auf$ebd$1 at smc.vnet.net... [Gianluca - I have reset your question a little - Allan] I am confused by this result: Module[{a},a=x;g[x_]=a] x g[y] x ?g Global`g g[x$_] = x Somehow I would expect the same output as Module[{},a=x;g[x_]=a] x g[y] y ?g Global`g g[x_] = x The difference is a dollar sign. > +---------------------------------+ > | Gianluca Gorni | > | Universita` di Udine | > | Dipartimento di Matematica | > | e Informatica | > | via delle Scienze 208 | > | I-33100 Udine UD | > | Italy | > +---------------------------------+ > | Ph.: (39) 0432-558422 | > | Fax: (39) 0432-558499 | > | mailto:gorni at dimi.uniud.it | > | http://www.dimi.uniud.it/~gorni | > +---------------------------------+ EXPLANATION FIRST EXAMPLE: Module[{a},a=x;g[x_]=a] In Module 1) the occurences of variable a are renamed to a$n (standard evaluation of Module); 2) because the a in g[x_]= a is in the target of the x in x_ , and in the target of the a in {a}, the x is changed to x$ (standard scoping renaming to avoid clashes - not specific to Module); giving Module[{a$n},a$n=x;g[x$_]=a$n] The body of Module, a$n=x; g[x$_]=a$n, is now evaluated. The rule for g[x$_] = x is stored. The output from Module is x SECOND EXAMPLE: Module[{},a=x;g[x_]=a];g[y] There is no renaming When the body of Module is evaluated the rule for g[x_]=x is stored. We can follow the evaluations with TracePrint. Allan --------------------- Allan Hayes Mathematica Training and Consulting Leicester UK www.haystack.demon.co.uk hay at haystack.demon.co.uk Voice: +44 (0)116 271 4198 Fax: +44 (0)870 164 0565