Issue:LAST-N

References:LAST(p267)

Category:ENHANCEMENT

Edit history:04-Dec-87, Version 1 by Pitman12-Mar-88, Version 2 by Pitman

Problem Description:

People always ask why

LASTreturns a cons and not an element.

BUTLASTtakes an argument N butLASTdoes not.

Proposal (LAST-N:ALLOW-OPTIONAL-ARGUMENT):

Allow

LASTto take an optional argument N, saying how many cells to return.The default for N would be 1.

It is an error if N is negative or L is circular.

If N is zero, then the atom that terminates the list L is returned.

If N is greater than or equal to the number of cons cells in the list

L, then the result is L.

Test Cases:

(

LAST'(A B C) 0) => ()(

BUTLAST'(A B C) 0) => (A B C)

(

LAST'(A B C) 1) => (C)(

BUTLAST'(A B C) 1) => (A B)

(

LAST'(A B C) 2) => (B C)(

BUTLAST'(A B C) 2) => (A)

(

LAST'(A B C) 3) => (A B C)(

BUTLAST'(A B C) 3) => ()

(

LAST'(A B C) 4) => (A B C)(

BUTLAST'(A B C) 4) => ()

(

LAST'(A B C)) => (C)(

BUTLAST'(A B C)) => (A B)

(

LAST'(A . B) 0) => B(

LAST'(A . B) 1) => (A . B)(

LAST'(A . B) 2) => (A . B)

Rationale:

BUTLASTandLASTwould select complementary parts of a list in general.That is (

EQUALL (APPEND(BUTLASTL N) (LASTL N))) would be T for N>=0and L being a proper list.

This would make it more obvious why

LASTshould return a list and notan element. ie, it would return the "last N elements" where N=1 by default.

Current Practice:

Probably nobody does this.

Adoption Cost:

Very slight. The following code would suffice:

(

DEFUNLAST(LIST&OPTIONAL(N 1))(

CHECK-TYPEN (INTEGER0))(R

LIST)(I 0 (+ I 1)))

((

ATOML) R)

Some implementations might want to

providecompiler optimizations forthe N=1 case.

Benefits:

This utility is probably needed often enough to warrant its inclusion.

It's present (as a separate function called NLEFT) in Symbolics Common

Lisp and Interlisp.

Conversion Cost:

None. This is an upward compatible extension.

Aesthetics:

This would make things a little prettier.

Discussion:

This suggestion came up recently on a Symbolics design discussion list.

Symbolics is contemplating offering it as an extension, but it seemed like

the rest of the CL community might want to adopt it, too. Pitman thinks

it's a nice idea.

Masinter opposes this extension as gratuitous.

Moon and Daniels think this is very low priority but have expressed a

lack of major objection to this proposal.