Modularity and the Naming of Things
- To: mathgroup at smc.vnet.net
- Subject: [mg38027] Modularity and the Naming of Things
- From: Name <mee at home.com.redline.ru>
- Date: Tue, 26 Nov 2002 00:49:18 -0500 (EST)
- Sender: owner-wri-mathgroup at wolfram.com
1. Consider In[1]:= Function[y,Module[{x=1},y+x]][x] Module[{x=1},#+x]&[x] Out[1]= 1+x Out[2]= 2 The first example always works as a function returning its argument plus one (unless we pass it 'unallowed' argument x$); the second example doesn't. I think the unnamed Slot parameters should internally be treated this way: In[3]:= Function[slot1,Module[{x=1},slot1+x]][x] Out[3]= 1+x This works consistently for any arguments passed to the function, doesn't interfere with the Module local variables and gives the same results as the definition using named parameters. 2. Consider In[4]:= x=1; Function[x,Evaluate@x] Module[{x=2},Function[x,Evaluate@x]] x=. Out[5]= Function[x,1] Out[6]= Function[x,1] The reason why the second example works this way is that Function makes the global x variable local, not the Module's x$n. This means we can't define functions that depend on Module's local variables unless we use some special tricks. Wouldn't it be better if Function made Module's variables local instead so that Module[{x}, Function[x,fx]] became Module[{x}, Function[x$n,fx]]? This behaviour would be useful in conjunction with the modification discussed below. The same goes for Rule and Set inside Module. 3. Consider In[8]:= y=x; Function[x,Evaluate[2x+y]] y=. Module[{y=x}, Function[x,Evaluate[2x+y]]] Out[9]= Function[x,3 x] Out[11]= Function[x$,x+2 x$] problem is that it makes the task of creating functions inside Modules much more complicated; if we want to do something like Unapply[fx, x], there is a simple solution Evaluate[fx/.x->#]& but it doesn't work for fx=#+x&. Is it possible to change the evaluation rules so that in Function[x,Evaluate[fx]] fx is evaluated outside the Function scope? Then the result is passed to the Function; in the second example above it would work as follows: Evaluate[2x+y]->3x->Function[x,3x], or, in the case of x being local to the Module, Evaluate[2x$n+y]->3x$n->Function[x$n,3x$n]. The same goes for the right-hand side of Rule and Set. Maxim Rytin m.r at prontomail.com