I make heavy use of Out in Mathematica's notebook environment. When I get a computation to work, it will often be spread across multiple cells and lines with %'s and %%'s littered throughout. Thereafter, if I want to use the code as a kind of script (say, I want to iterate over values of some variable with Do), I have to go back and locate all the %'s and give them variable names or else refactor multiple lines of code into one, typically much less readable line of code. Is there any way to avoid doing this extra work?
I will give an example of what would be nice, if it in fact worked. Say I have the following computation:
Cos[1.0]/2;
ArcTan[%];
E^-%^2
and I decide I want to iterate over different values of the argument that appears in the cosine. I'd like to be able to just write:
results = {}
Do[
Cos[w]/2;
ArcTan[%];
results = results~Join~{E^-%^2},
{w, {1.0, 2.0, 3.0}}
]
and have in results the list of values {0.93275, 0.958788, 0.809559}.
I know I can do this:
results = {}
Do[
w1 = Cos[w]/2;
w2 = ArcTan[w1];
results = results~Join~{E^-w2^2},
{w, {1.0, 2.0, 3.0}}
]
but in practice this amounts to a lot of busywork. Also it ultimately negates any time or mental overhead saved by using % as soon as I have need to use the code in any program.
Also, sorry if this has already been asked. It is quite difficult to search for Out and %.
ClearAll[RES]; results = Sequence[]; Do[ val = {k^3, k^2}; results = RES[results, val], {k, -4, 4}]; Echo[results, "Linked list"]; results = List @@ Flatten[results]; Echo[results, "Flattened"]; ListLinePlot[results]. If you have a lot of data, this way is more efficient. $\endgroup$CompoundExpressioninteracts withOut. For example,1; 2; 3;is a shorthand forCompoundExpression[1, 2, 3]and is a single expression that only gets assigned a singleOutnumber. You cannot refer to previous results within the same compound expression. For example,2; % + 7does not give9because the%does not refer to the2, but rather to the expression that was executed before this one. $\endgroup$CompoundExpressionhas changed over time... certainly I don't pretend to understand how it works in the evaluation sequence. But I thought-- apparently incorrectly-- that I'd communicated the desired result clearly. $\endgroup$