User Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cl:functions:last [2019/06/15 02:00]
cl:functions:last [2019/06/16 07:00] (current)
Line 1: Line 1:
 +====== Function LAST ======
  
 +====Syntax====
 +  * **last** //list ''&​optional''​ n// → //tail//
 +
 +====Arguments and Values====
 +  * //list// - a //​[[CL:​Glossary:​list]]//,​ which might be a //​[[CL:​Glossary:​dotted list]]// but must not be a //​[[CL:​Glossary:​circular list]]//.
 +  * //n// - a non-negative //​[[CL:​Glossary:​integer]]//​. The default is ''​1.''​
 +  * //tail// - an //​[[CL:​Glossary:​object]]//​.
 +
 +====Description====
 +**last** returns the last //n// //​[[CL:​Glossary:​cons|conses]]// ​  (not the last //n// elements) of //list//). If //list// is **[[CL:​Constant Variables:​nil]]**,​ **last** returns **[[CL:​Constant Variables:​nil]]**.
 +
 +If //n// is zero, the atom that terminates //list// is returned. If //n// is greater than or equal to the number of //​[[CL:​Glossary:​cons]]//​ cells in //​list//, ​  the result is //list//.
 +
 +====Examples====
 +<​blockquote>​
 +(last nil) <​r>​[[CL:​Constant Variable:​nil|NIL]]</​r>​
 +(last '(1 2 3)) <​r>​(3)</​r>​
 +(last '(1 2 . 3)) <r>(2 . 3)</​r>​
 +([[CL:​Macros:​defparameter]] *x* (list 'a 'b 'c 'd)) <​r>​*X*</​r>​
 +(last *x*) <​r>​(D)</​r>​
 +([[CL:​Functions:​rplacd]] (last *x*) (list 'e 'f)) <r>(D E F)</​r>​
 +*x* <r>(A B C D E F)</​r>​
 +(last *x*) <​r>​(F)</​r>​
 +
 +(last '(a b c)) <​r>​(C)</​r>​
 +
 +(last '(a b c) 0) <​r>​[[CL:​Constant Variable:​nil|NIL]]</​r>​
 +(last '(a b c) 1) <​r>​(C)</​r>​
 +(last '(a b c) 2) <r>(B C)</​r>​
 +(last '(a b c) 3) <r>(A B C)</​r>​
 +(last '(a b c) 4) <r>(A B C)</​r>​
 +
 +(last '(a . b) 0) <​r>​B</​r>​
 +(last '(a . b) 1) <r>(A . B)</​r>​
 +(last '(a . b) 2) <r>(A . B)</​r>​
 +</​blockquote>​
 +
 +====Side Effects====
 +None.
 +
 +====Affected By====
 +None.
 +
 +====Exceptional Situations====
 +The consequences are undefined if //list// is a //​[[CL:​Glossary:​circular list]]//.
 +
 +Should signal an error of type type-error if //n// is not a non-negative //​[[CL:​Glossary:​integer]]//​.
 +
 +====See Also====
 +**[[CL:​Functions:​butlast|Function BUTLAST]]**,​ **[[CL:​Functions:​nth|Function NTH]]**
 +
 +====Example Implementation====
 +The following code could be used to define **last**.
 +
 +<​blockquote>​
 +([[CL:​Macros:​defun]] last (list &​optional (n 1))
 +  ([[CL:​Macros:​check-type]] n ([[CL:​Types:​integer]] 0))
 +  ([[CL:​Macros:​do]] ((l list ([[CL:​Functions:​cdr]] l))
 +       (r list)
 +       (i 0 ([[CL:​Functionc:​1-plus|1+]] i)))
 +      (([[CL:​Functions:​atom]] l) r)
 +    ([[CL:​Special Operators:​if]] ([[CL:​Functions:​greater-equal|>​=]] i n) ([[CL:​Macros:​pop]] r))))
 +</​blockquote>​
 +
 +====Notes==== ​                                            
 +None.
 +
 +\issue{LAST-N}
 +\issue{DOTTED-LIST-ARGUMENTS:​CLARIFY}