last
-- access a previously
computed object%
returns the result of the last command.
last(
n)
or %n
returns the
result of the n
th previous command.
last(n)
%
%n
n |
- | a positive integer |
a MuPAD object.
Chapter 12 of the MuPAD Tutorial.
last(
n)
returns the result entry of the
n
th element in this table, counted from the end of the
table. Thus last(
1)
returns the result of the
last command, last(
2)
returns the result of
the next to last one, etc. Instead of last(
n)
one can also write more briefly %n
. Instead of
last(
1)
or %1
, one can use even
more briefly %
.HISTORY
determines the number of
previous results that can be accessed at interactive level, i.e., the
number of entries in the history table. In procedures, the length of this table is always
3, independent of the value of HISTORY
. Thus admissible values for
n
are the integers between 1
and HISTORY
at interactive level,
and the integers 1, 2, 3
inside a procedure.
Use history
to
access entries of the history table at interactive level directly,
including the command that produced the corresponding result.
last
or %
is not
evaluated again. Use the function eval
to force a subsequent evaluation.
See example 4.last
behaves differently at interactive
level and in procedures. At interactive
level, compound statements, such as for
, repeat
, and while
loops and if
and case
branching instructions, are stored
in the history table as a whole. In procedures, the statements within a
compound statement are stored in a separate history table of this
procedure, but not the compound statement itself. See example 5.
last(
n)
may differ from the n
th
previous output that is visible on the screen at interactive level. See
example 1.fread
or read
are stored in the history table
before the fread
or read
command
itself. If the option Plain is used, then a
separate history table is valid within the file, and the commands from
the file do not appear in the history table of the enclosing context.
See the help page of history
for examples.last
in procedures is generally considered bad
programming style and is therefore deprecated. Future MuPAD
releases may no longer support the use of last
within
procedures.%n
is used, then
n
must be a positive integer
literally. If this is not the case, but n
evaluates to a
positive integer, use the equivalent functional notation
last(
n)
(see example 3).last
is a function of the system kernel.Here are some examples for using last
at
interactive level. Note that last(
n)
refers
to the n
th previously computed result, whether it was
displayed or not:
>> a := 42; last(1), %, %1
42 42, 42, 42
>> a := 34: b := 56: last(2) = %2
34 = 34
Commands appearing on one input line lead to separate entries in the history table:
>> "First command"; 11: 22; 33:
"First command" 22
>> last(1), last(2);
33, 22
If a sequence of commands is bracketed, it is regarded as a single command:
>> "First command"; (11: 22; 33:)
"First command" 33
>> last(1), last(2);
33, "First command"
An expression sequence is also regarded as a single command:
>> "First command"; 11, 22, 33;
"First command" 11, 22, 33
>> last(1), last(2);
11, 22, 33, "First command"
Due of the fact that the MuPAD parser expects a
number after the %
sign, there is a difference between the
use of %
and last
. last
can be
called with an expression that evaluates to a positive integer:
>> n := 2: a := 35: b := 56: last(n)
35
If you try the same with %
, an error
occurs:
>> n := 2: a := 35: b := 56: %n
Error: Unexpected 'identifier' [line 2, col 0]
The result of last
is not evaluated
again:
>> delete a, b: c := a + b + a: a:= b: last(2)
2 a + b
Use eval
to enforce the evaluation:
>> eval(%)
3 b
We demonstrate the difference between the use of
last
at interactive level and in procedures:
>> 1: for i from 1 to 3 do i: print(last(1)): end_for:
1 1 1
Here last(
1)
refers to the
most recent entry in the history table, which is the 1
executed before the for
loop. We can also verify this by inspecting the history table after
these commands. The command history
returns a list with two
elements. The first entry is a previously entered MuPAD command,
and the second entry is the result of this command returned by
MuPAD. You see that the history table contains the whole
for
loop as a single
command:
>> history(history() - 1), history(history())
[1, 1], [(for i from 1 to 3 do i; print(last(1)) end_for), null()]
However, if the for
loop defined above is executed
inside a procedure, then we obtain a different result. In the following
example, last(
1)
refers to the last evaluated
expression, namely the i
inside the loop:
>> f := proc() begin 1: for i from 1 to 3 do i: print(last(1)): end_for end_proc:
>> f():
1 2 3
The command history
refers only to the
interactive inputs and their results:
>> history(history())
[f(), null()]
last
within procedures now differs
from the behavior at interactive level.