From: David Park on
I use multiple statements in one cell, with multiple outputs, quite often
and don't see any problem with it. It's a good way to develop a calculation
before moving it to a Module, or to develop a derivation or to just try out
things.

When everything is in one cell you can safely use % and %% without worrying
about order of evaluation. You can intersperse Print statements if you want
to annotate the output. You can keep adding further steps and reevaluating
as a calculation is built up. You can add or remove ";"s as you want to see
more or less.

I never have any problem with Return being misinterpreted as a
multiplication. It is far easier than using multiple cell evaluations or
using a new Subsection or even selecting the correct set of cells.

There is a subset of Mathematica users who see writing extended definitions
and routines as a barrier. The multiple statement, single cell form is a
good way to transition them to it, because it almost is a routine.


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/



From: Helen Read [mailto:hpr(a)together.net]

On 7/17/2010 8:16 AM, Sam Takoy wrote:
> Hi,
>
> When Mathematica prints an expression not terminated by a semicolon, I
> find it hard to match up the output with what I was trying output when a
> block of commands has many commands not terminated by a semicolon.

I don't think it is a good idea to have a block of commands all in one
cell that are not separated by semi-colons. As you point out, it is
difficult to follow. Worse, it can easily lead to errors. For example
two things that you intended as separate commands end up getting
multiplied together. If you need to see the output of each command, put
each input in a separate cell. Then you will have a sequence
input/output, input/output, which is very easy to read and follow. If
you find it tiresome to evaluate each input one by one, put them all in
a section and select the cell bracket for the entire section. Then when
you evaluate (Shift-Enter), everything that is selected will be evaluated.

On the other hand, if you only to see output from the last command, put
it all in a single cell, separating the commands with semi-colons (and
no semi-colon on the last one).

For a block of code that you will re-use with different input, write a
function in the form of a Module or Block. Use Print within the Module
(or Block) anywhere you want to see output.

> Is there a way to get Mathematica to output the LHS so when I have
> R=1+1 it outputs R = 2 or something like that.

Print a Row. This is especially useful within a Module or Block.

r = 1 + 1;
Print[Row[{"r = ", r}]];

One last comment: it is a good habit to use lower case letters for your
own variable and function names, to avoid inadvertent conflicts with
built-in functions and symbols. I also think it makes it a bit easier to
read your code that way -- it is immediately clear which functions and
symbols you have defined yourself, and which ones are built-in.

If you have very long function names, you can use what is sometimes
called camel case.

myVeryLongFunctionNameInCamelCase[x_,y_,z_]:= x^2+2y+z


--
Helen Read



From: David Bailey on
On 17/07/10 13:16, Sam Takoy wrote:
> Hi,
>
> When Mathematica prints an expression not terminated by a semicolon, I
> find it hard to match up the output with what I was trying output when a
> block of commands has many commands not terminated by a semicolon. Is
> there a way to get Mathematica to output the LHS so when I have R=1+1 it
> outputs R = 2 or something like that.
>
> Thanks!
>
>
The real answer is to put the various commands in separate cells in your
notebook, and all the output will appear in the correct place.

David Bailey

http://www.dbaileyconsultancy.co.uk

From: Helen Read on
On 7/19/2010 2:07 AM, David Park wrote:
> I use multiple statements in one cell, with multiple outputs, quite often
> and don't see any problem with it. It's a good way to develop a calculation
> before moving it to a Module, or to develop a derivation or to just try out
> things.
>
> When everything is in one cell you can safely use % and %% without worrying
> about order of evaluation. You can intersperse Print statements if you want
> to annotate the output. You can keep adding further steps and reevaluating
> as a calculation is built up. You can add or remove ";"s as you want to see
> more or less.

Each to his/her own. I personally find student work (and my own work) to
be much easier to follow with one input per cell, or multiple inputs in
a cell separated with semi-colons. As you point out, you can always
intersperse Print statements if you like.

As for % and %%, I never use them, and I certainly don't teach them.
Using % in a single cell vs. multiple cells is a distinction that my
students are likely not to make, with dire consequences. When I first
started teaching with Mathematica ~15 years ago (!), the use of % was
one of the two biggest sources of problems for my students. A couple of
years in, life got much better when I made a conscious decision not to
introduce the % notation to my students, and to discourage its use by
the few students who discover it (or even worse, the dreaded Out[17]
notation, which is a living nightmare) on their own.

When I stopped teaching %, I realized I didn't need it either, and I
stopped using it myself. Nobody misses %, least of all me. I teach my
students to define functions for practically everything, and to assign
names for anything that is not a function if they think they are going
to use it again, or go back and assign a name when needed if they didn't
name it to begin with. If they do that, there is no need for %,
preventing all the problems they used to have with it, and making it
much easier for them (and me, when I have to grade it) to follow their work.

(The other one of the two biggest stumbling blocks in the old days was
students failing to load packages, then remembering after the fact --
when something didn't work -- leading to the Shadowing error, which just
baffled them. I could explain it 100 times, and most of the students
never understood why it was too late once they already tried to use a
function from a package, and never remembered to do it in sequence the
next time. So we started loading commonly used packages right in the
init.m in the classrooms we maintain...which was great, except that the
students would get confused when working somewhere else and completely
forget to load packages. Things got much, much better when most all of
the functionality that we needed from packages was moved to the main
Mathematica. I can't remember the last time I had my students load a
package. Life is good.)

Those two simple things (1) don't teach %, and (2) no more need to load
packages were huge quality-of-life improvements for my classes.

> I never have any problem with Return being misinterpreted as a
> multiplication.

I have seen it happen with student work. Sometimes they do a lot of
copy/paste, and something that looks like a line-break might not be one.
It can be very difficult to trouble-shoot. There is a lot less of this
sort of problem if I teach them to put one input per cell, or separate
with semi-colons if they put multiple inputs within a cell.

> There is a subset of Mathematica users who see writing extended definitions
> and routines as a barrier. The multiple statement, single cell form is a
> good way to transition them to it, because it almost is a routine.

I encourage this all the time with my students, for just that reason,
but have them separate with semi-colons. They don't have any problem
doing that.

--
Helen Read
University of Vermont

From: Helen Read on
On 7/21/2010 7:14 AM, Helen Read wrote:
> On 7/19/2010 2:07 AM, David Park wrote:
>>
>> When everything is in one cell you can safely use % and %% without worrying
>> about order of evaluation.
>
> As for % and %%, I never use them, and I certainly don't teach them.
> Using % in a single cell vs. multiple cells is a distinction that my
> students are likely not to make, with dire consequences. When I first
> started teaching with Mathematica ~15 years ago (!), the use of % was
> one of the two biggest sources of problems for my students. A couple of
> years in, life got much better when I made a conscious decision not to
> introduce the % notation to my students, and to discourage its use by
> the few students who discover it (or even worse, the dreaded Out[17]
> notation, which is a living nightmare) on their own.
>
> When I stopped teaching %, I realized I didn't need it either, and I
> stopped using it myself. Nobody misses %, least of all me.

One more comment about the % and %%. I remember that they were useful
way back in the olden days, when I was using Mathematica on unix
workstations. There was no notebook interface -- it was line in, line
out, and there was no way to edit something you had already entered and
evaluated. About the only way to refer back to something you had already
done was with % and %% etc., or the Out[23] notation. This was OK at the
time, because it was all done sequentially, input/output, with no going
back. But with the notebook interface, we find ourselves jumping around
the notebook, editing and re-evaluating, saving the whole thing and
coming back another time when the % and Out[] numbers will be completely
changed. With all of that going on in a notebook, the use of % and %%
and %7 and Out[113] etc. is far more trouble than it's worth, as far as
I'm concerned. If the notebook interface had been around at the very
beginning, I'm not sure Wolfram would have ever included the % and %%
functionality.

--
Helen Read
University of Vermont

From: David Park on
1) I agree that % and %% are generally a bad idea - except when used within
a single cell, where they are perfectly all right and convenient. Your
teaching of naming expressions is a good idea, but not always necessary. (I
would use names like step1, step2, etc., to avoid assigning values to common
symbols)

2) Might it be that copying and pasting parts of expressions is an
error-prone procedure that should be discouraged in favor of using Part or
Cases or Select.

3) Sooner or later I would think that students would have to use packages.
Why not teach them to have an Initialization section at the top of their
notebook with Initialization cells for packages and anything else they might
need? You already teach them to use Sectional organization, which is another
very good thing in your teaching practices. (If I forget to load a package I
always just Quit the kernel and start over. Of course, if I had just
performed a three hour calculation I might think differently.)


David Park
djmpark(a)comcast.net
http://home.comcast.net/~djmpark/




From: Helen Read [mailto:hpr(a)together.net]

On 7/19/2010 2:07 AM, David Park wrote:
> I use multiple statements in one cell, with multiple outputs, quite often
> and don't see any problem with it. It's a good way to develop a
calculation
> before moving it to a Module, or to develop a derivation or to just try
out
> things.
>
> When everything is in one cell you can safely use % and %% without
worrying
> about order of evaluation. You can intersperse Print statements if you
want
> to annotate the output. You can keep adding further steps and reevaluating
> as a calculation is built up. You can add or remove ";"s as you want to
see
> more or less.

Each to his/her own. I personally find student work (and my own work) to
be much easier to follow with one input per cell, or multiple inputs in
a cell separated with semi-colons. As you point out, you can always
intersperse Print statements if you like.

As for % and %%, I never use them, and I certainly don't teach them.
Using % in a single cell vs. multiple cells is a distinction that my
students are likely not to make, with dire consequences. When I first
started teaching with Mathematica ~15 years ago (!), the use of % was
one of the two biggest sources of problems for my students. A couple of
years in, life got much better when I made a conscious decision not to
introduce the % notation to my students, and to discourage its use by
the few students who discover it (or even worse, the dreaded Out[17]
notation, which is a living nightmare) on their own.

When I stopped teaching %, I realized I didn't need it either, and I
stopped using it myself. Nobody misses %, least of all me. I teach my
students to define functions for practically everything, and to assign
names for anything that is not a function if they think they are going
to use it again, or go back and assign a name when needed if they didn't
name it to begin with. If they do that, there is no need for %,
preventing all the problems they used to have with it, and making it
much easier for them (and me, when I have to grade it) to follow their work.

(The other one of the two biggest stumbling blocks in the old days was
students failing to load packages, then remembering after the fact --
when something didn't work -- leading to the Shadowing error, which just
baffled them. I could explain it 100 times, and most of the students
never understood why it was too late once they already tried to use a
function from a package, and never remembered to do it in sequence the
next time. So we started loading commonly used packages right in the
init.m in the classrooms we maintain...which was great, except that the
students would get confused when working somewhere else and completely
forget to load packages. Things got much, much better when most all of
the functionality that we needed from packages was moved to the main
Mathematica. I can't remember the last time I had my students load a
package. Life is good.)

Those two simple things (1) don't teach %, and (2) no more need to load
packages were huge quality-of-life improvements for my classes.

> I never have any problem with Return being misinterpreted as a
> multiplication.

I have seen it happen with student work. Sometimes they do a lot of
copy/paste, and something that looks like a line-break might not be one.
It can be very difficult to trouble-shoot. There is a lot less of this
sort of problem if I teach them to put one input per cell, or separate
with semi-colons if they put multiple inputs within a cell.

> There is a subset of Mathematica users who see writing extended
definitions
> and routines as a barrier. The multiple statement, single cell form is a
> good way to transition them to it, because it almost is a routine.

I encourage this all the time with my students, for just that reason,
but have them separate with semi-colons. They don't have any problem
doing that.

--
Helen Read
University of Vermont