Previous Page Next Page Contents

last -- access a previously computed object

Introduction

% returns the result of the last command.

last(n) or %n returns the result of the nth previous command.

Call(s)

last(n)

%
%n

Parameters

n - a positive integer

Returns

a MuPAD object.

Further Documentation

Chapter 12 of the MuPAD Tutorial.

Related Functions

HISTORY, history

Details

Example 1

Here are some examples for using last at interactive level. Note that last(n) refers to the nth 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

Example 2

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"

Example 3

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]

Example 4

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

Example 5

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()]

Changes




Do you have questions or comments?


Copyright © SciFace Software GmbH & Co. KG 2000