last
last Function
Syntax:
last list &optional n → tail
Arguments and Values:
list—a list, which might be a dotted list but must not be a circular list.
n—a non-negative integer . The default is 1.
tail—an object.
Description:
last returns the last n conses (not the last n elements) of list). If list is (), last returns ().
If n is zero, the atom that terminates list is returned. If n is greater than or equal to the number of cons cells in list, the result is list.
Examples:
(last nil) → NIL
(last ’(1 2 3)) → (3)
(last ’(1 2 . 3)) → (2 . 3)
(setq x (list ’a ’b ’c ’d)) → (A B C D)
(last x) → (D)
(rplacd (last x) (list ’e ’f)) x → (A B C D E F)
(last x) → (F)
(last ’(a b c)) → (C)
(last ’(a b c) 0) → ()
(last ’(a b c) 1) → (C)
(last ’(a b c) 2) → (B C)
(last ’(a b c) 3) → (A B C)
(last ’(a b c) 4) → (A B C)
(last ’(a . b) 0) → B
(last ’(a . b) 1) → (A . B)
(last ’(a . b) 2) → (A . B)
Exceptional Situations:
The consequences are undefined if list is a circular list. Should signal an error of type type-error if n is not a non-negative integer .
See Also:
butlast, nth
Notes:
The following code could be used to define last.
(defun last (list &optional (n 1))
(check-type n (integer 0))
(do ((l list (cdr l))
(r list)
(i 0 (+ i 1)))
((atom l) r)
(if (>= i n) (pop r))))
Expanded Reference: last
Getting the last cons of a list
last returns the last cons cell of a list (not the last element). By default it returns a list of the last 1 cons.
(last '(a b c))
=> (C)
(last '(1 2 3 4 5))
=> (5)
Getting the last element itself
Since last returns the last cons, use car to extract the actual last element.
(car (last '(a b c)))
=> C
(car (last '(1)))
=> 1
last with the optional n argument
The optional second argument specifies how many trailing conses to return.
(last '(a b c d) 0)
=> NIL
(last '(a b c d) 1)
=> (D)
(last '(a b c d) 2)
=> (C D)
(last '(a b c d) 3)
=> (B C D)
(last '(a b c d) 10)
=> (A B C D)
last on the empty list
When applied to NIL, last returns NIL.
(last nil)
=> NIL
last with dotted lists
For dotted lists, last returns the last cons cell, which includes the non-nil terminator.
(last '(a b . c))
=> (B . C)
(last '(a . b))
=> (A . B)
(last '(a . b) 0)
=> B
Practical use: appending to the end of a list
last is commonly used with rplacd to destructively append to the end of a list.
(let ((lst (list 1 2 3)))
(rplacd (last lst) (list 4 5))
lst)
=> (1 2 3 4 5)