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?..
3 Responses
Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.
"No mentions of competing products" is part of Mathgroup moderation policy. It can be hard to tell what is a competing product, last time my message was censored for mentioning "GAP". A couple of times I posted a message and it didn't make it to the list at all, apparently couple of other users had same problem
I stumbled across your blog while searching for something else... Anyways, two comments about MathGroup:
1. MathGroup is moderated by a single guy, and he isn't paid to do it. Yikes. I've also had references to other CAS systems and Matlab censored --- and received an email explaining why, which was nice. Basically, past flame wars lead the volunteer moderator to adopt the do-not-mention policy. Annoying, but I understand the time and energy this must save him.
2. You should join StackOverflow.com, if you haven't already done so. It is a much better/faster place to get MMA help, and the site is set up to publicly reward the best answers, good questions, and useful comments with reputation points.
One final thought, about plotting in MMA, which you mentioned in another post...
I agree that it can be a PITA sometimes to get things to look right, but plotting functions seem to fit the MMA design pattern of 'make things as general as possible'. Hopefully things will continue to improve---as I'm sure they will---given that all the Wolfram|Alpha plots are produced by Mathematica in the background (and they look great, IMO).
If you've upgraded to version 8, you might try using the link to Wolfram|Alpha; send it your data, have it generate the plot, and then just grab that 'pod' of W|A output.
The Demonstrations Project is also good source of plot customization code and examples.
GIMP (open source) alternative to Photoshop may also be able to handle the necessary post processing, if you're sick of trying to code perfection in MMA.
:)
1. I appreciate Steven Christensen's efforts, and I'm willing to believe his contribution elevated the quality of discussion on Mathgroup back in the day. But today, such moderation just strikes one as odd, particularly because the target audience here is extremely highly educated, (hopefully) mature, and are quite capable of conducting themselves nicely. Experiencing mid-1990s style moderation in the year 2011 is very strange. (Of course, the entire smc.vnet.net domain is a blast from the past, so maybe it's all part of an immersive experience to make me feel nostalgic about the olden days of 486s and 14.4kbps modems).
2. SO is awesome. I'm on it. Furthermore, it is another reason for why moderation on Mathgroup is completely unnecessary: newbies can get their questions answered on SO, while the more sophisticated crowd can engage with Mathgroup luminaries.
3. Yes, having plots be very general is, in principle, desirable. Unfortunately, without a good interface (something akin to what Matlab provides) this generality is useless. I recently spent a whole day (!) reformatting a single plot to prepare it for publication. Manually changing line styles and fonts by constructing complex pattern-based replacement expressions, but that was the easy part. MMA PDF export is spectacularly bad: it's anything but WYSIWYG and requires much trial and error to get right. Next time I'm doing this in Inkscape.