My Mathematica adventures continue. I am slowly hammering out a good way to manage namespaces and scoping. I was hoping to write a set of concise utility functions for that purpose, but ran into severe roadblocks. Certain things related to switching contexts in the front end turn out to be impossible programmatically from the kernel — the workaround is to manipulate the front-end from the kernel, essentialy spawning and evaluating cells. Trying to do this resulted in a bunch of unexplained and unanticipated behavior, meaning my utility functions remain in the “almost but not quite” state of readiness. I don’t intend to work on that any longer, since I already have a set of (more verbose) commands at my disposal that do what I need.
I did, however, finally ask the question on Mathgroup about why there isn’t a scoping construct that would effectively shield every symbol inside of it by default. I’ve been meaning to ask this for years. I didn’t get a direct answer, but some insightful comments did arise on that thread — in particular David Park discussed f[a_,b_][x_] := … and With[{a=...,b=...}, f[x_]:=…] idioms for managing constant parameters, and Leonid Shifrin suggested the possibility of using a `Private subcontext in a Begin["Context`"];…;End[] construct. That’s a good idea that might come in handy at some point.
But a most curious thing happened to my message on the way to the Mathgroup subscribers. You can read the original message below. Twice in the body of the message I draw comparisons with my other workhorses — Matlab and R. If you now search for my message on the Mathgroup archives or Google Groups, you will see that the mention of Matlab has disappeared, leaving R sad and alone.
Mathgroup is moderated, yet I can’t quite fathom the rationale for that peculiar redaction. But it’s fun to hypothesize that there’s some sort of a cold war going on between Mathworks and Wolfram, with hackers, and secret agents, poison-tipped umbrellas in Boston, and car bombings outside sultry cafes in Champaign, IL. Which brings up an interesting question — to whose posse do I belong?.. Am I on the L-shaped membrane eigenmode crew, or are hyperbolic dodecahedrons my homies?.. As of late, I am spending lots of time with both, so I’d consider myself a double agent of sorts. If history is any indication, though, I am much more likely to flame Mathematica — although I’ve become less aggressive after I RTFMed the core docs and bits of the Mathematica book. Text of my message to Mathgroup follows.
Dear Mathematica gurus,
One of the things that initially made Mathematica difficult for me to use was scoping — in particular, the fact that all symbols by default appear in the global namespace. Even though this is a default behavior for interactive evaluation in many packages, e.g. Matlab and R, in Mathematica, it leads to a greater potential for errors because unlike those languages, in Mathematica (1). a symbol can have multiple DownValues, and (2). if one forgets to explicitly localize a symbol inside a scoping construct, it may silently be taken from the global namespace.
After many years I finally figured out a (more or less) clean way to structure my code and workflow, through a combination of defining modules, contexts, packages, and careful use of Clear and Remove.
I still wonder, however, why there isn’t a construct similar to Module that would define a unique private context for _all_ symbols within the construct (i.e. without having to declare them in a list). You can kind of simulate this behavior by using BeginContext["MyCont`"] together with redefining $ContextPath temporarily to only have “MyCont`” and “System`”. This is obviously too verbose to be of practical use, but I do wonder why there isn’t a built-in construct.
I suppose my question is — is there a deep wisdom behind its absence, or perhaps I am an anomaly in thinking that such behavior (automatic lexical scoping for symbols in subroutines, present in Matlab, R, and many others) would be incredibly handy?..
Post a Comment