Re: Compound Expressions
- To: mathgroup at smc.vnet.net
- Subject: [mg29216] Re: Compound Expressions
- From: aes <siegman at stanford.edu>
- Date: Mon, 4 Jun 2001 05:30:51 -0400 (EDT)
- Organization: Stanford University
- References: <9f7keh$h0o$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Further notes on compound expressions like In[147]:= a = 3; Remove["Global`*"]; a = 3; a In[149]:= a = 3; Remove["Global`*"]; Print[a]; a = 3; Print[a] and how they behave, or should behave, including a few comments from: David Withoff <withoff at wolfram.com>. It's certainly helpful -- may even be essential -- for users to have a mental image of how a software program is going to process the inputs that the user feeds to it. My mental image is obviously colored (some might say "irreparably damaged") by experience with languages like FORTRAN, or even worse BASIC. In any case, the most common mental image for any language is certainly that individual expressions, or statements, will normally be executed *sequentially*, in the order in which they are encountered. There are obviously exceptions to this in situations like explicit control structures, function arguments, or other explicitly grouped situations; but that's the normal expectation. The simplest mental image for interpreting a cell containing CompoundExpression[expr1; expr2; expr3] will then be that the kernel will respond to this by doing the same things it would do if it encountered expr1, expr2, expr3 in three successive but independent cells. In other words, CompoundExpression[] is interpreted primarily as a way of condensing or formatting expr1, expr2, expr3 into one cell -- not as something that's going to change the *meaning*, or the *functioning* of expr1, expr2, or expr 3. Apparently, however, Mathematica doesn't work this way, but in some more complex fashion. This is then a major disadvantage for "ordinary" users. It means that the user has to understand not only how expr1, expr2 and expr3 would function as separate cells, something which can be complex enough, depending on what's inside these expressions. The user also now has to understand what are the more complex *non-sequential* consequences of putting these expressions inside a CompoundExpression[]. David has written in an earlier posting: > . . . the computer obviously has to encounter and recognize "Remove" > before it can know that it is being asked to remove something. > Similarly, "a" is not and cannot be removed immediately when it is > encountered, since at least a few such encounters have to take place > before the computer can know what it is being asked to do. I guess I just don't understand what the last sentence here is saying. He then adds: > The subsequent "a=3" does not recreate "a". The "a" was already > created when it was encountered when the computer looked at the input > to figure out what it was being asked to do. But doesn't the Remove[] expression remove "a" (if it exists), and of course all other symbols in the Global context, as soon as it's encountered and executed? This certainly seems to be confirmed by the result of the first "Print[a]" statement in Line[149] above. The immediately following "a=3" statement then recreates "a" and gives it a value, as is confirmed by the output of the second "Print[a]" statement. (Note the discussion of Remove[] on p. 1181 of The Mathematica Book: "Once you have removed a symbol, you will never be able to refer to it again, unless you recreate it.") A second question is, does CompoundExpression[] *have* to function in this more complex way? -- or is this the consequence of certain design decisions that could have been made otherwise?. Are there situations where simply making e1;e2;e3 in a single cell function differently from e1, e2, e3 in three successive cells would lead to inherently unacceptable consequences? (all this assuming that e1, e2, e3 are expressions that would be syntactically acceptable written as three separate cells) I lack the knowledge to address this question; but I still wonder, "Did it *have* to be designed this way?" David Withoff's answer was "Yes", but he and I may have been interpreting the question at different levels. If it could have been otherwise, then I still think it was an unfortunate design decision. Finally, note that someone else has pointed out that In[149]:= a = 3; Remove["Global`*"]; Print[a]; a = 3; Print[a]; a In[150]:= a and In[149]:= a = 3; Remove["Global`*"]; Print[a]; a = 3; Print[a]; a In[150]:= a function quite differently, which suggests that more than meets the eye may be going on here. It also motivates one to ask, where is the ordinary user sternly warned that this kind of unexpected behavior might occur? And, does it make any difference whether the user actively inserts the line breaks, or they are inserted by the line-wrapping action of the Mathematic Front End? --AES