Function butlast, nbutlast

Syntax:

butlast list &optional n result-list

nbutlast list &optional n result-list

Arguments and Values:

list—a list, which might be a dotted list but must not be a circular list.

n—a non-negative integer.

result-list—a list.

Description:

15.2.0 36butlast returns a copy of list from which the last n elementsconses have been omitted. If n is not supplied, its value is 1. If there are fewer than n elementsconses in list, nil is returned and, in the case of nbutlast, list is not modified. Barmar notes that if there are n elements, NIL is returned, too. He suggests says "fewer than n+1". But KMP thinks the definition already covers that, and the reason for the exception about NIL is to clarify the behavior in the cases not covered.

15.2.0 37nbutlast is like butlast, but nbutlast may modify list. It changes the cdr of the cons n+1 from the end of the list to nil.

Examples:

 (setq lst '(1 2 3 4 5 6 7 8 9)) → (1 2 3 4 5 6 7 8 9)
 (butlast lst) → (1 2 3 4 5 6 7 8)
 (butlast lst 5) → (1 2 3 4)
 (butlast lst (+ 5 5)) → NIL
 lst → (1 2 3 4 5 6 7 8 9)
 (nbutlast lst 3) → (1 2 3 4 5 6)
 lst → (1 2 3 4 5 6)
 (nbutlast lst 99) → NIL
 lst → (1 2 3 4 5 6)
 (butlast '(a b c d)) → (A B C)
 (butlast '((a b) (c d))) → ((A B))
 (butlast '(a)) → NIL
 (butlast nil) → NIL
 (setq foo (list 'a 'b 'c 'd)) → (A B C D)
 (nbutlast foo) → (A B C)
 foo → (A B C)
 (nbutlast (list 'a)) → NIL
 (nbutlast '()) → NIL

The following example was present but no one could figure out what made anyone think this would reliably work, so I removed it. -kmp 14-Feb-92 (butlast '(1 2 . 3)) \EV (1)

Affected By:

None.

Exceptional Situations:

Should signal an error of type type-error if list is not a proper list or a dotted list. Should signal an error of type type-error if n is not a non-negative integer.

See Also:

None.

Notes:

 (butlast list n) ≡ (ldiff list (last list n))