User Tools


Function LAST

Syntax

  • last list &optional ntail

Arguments and Values

Description

last returns the last n conses (not the last n elements) of list). If list is nil, last returns nil.

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)

(defparameter *x* (list 'a 'b 'c 'd))

*X*

(last *x*)

(D)

(rplacd (last *x*) (list 'e 'f))

(D E F)

*x*

(A B C D E F)

(last *x*)

(F)

(last '(a b c))

(C)

(last '(a b c) 0)

NIL

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

Side Effects

None.

Affected By

None.

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

Example Implementation

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 (1+ i))) ((atom l) r) (if (>= i n) (pop r))))

Notes

None.

\issue{LAST-N} \issue{DOTTED-LIST-ARGUMENTS:CLARIFY}