Skip to content

Commit 4fe625c

Browse files
authored
Merge pull request #350 from knorth55/last-optional-n
add &optional n in last
2 parents 3e25e57 + e7bd2f3 commit 4fe625c

File tree

3 files changed

+8
-7
lines changed

3 files changed

+8
-7
lines changed

doc/jlatex/jsequences.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,8 +236,8 @@ \subsection{リスト}
236236
\funcdesc{nthcdr}{count list}{
237237
{\em list}に{\bf cdr}を{\em count}回適用した後のリストを返す。}
238238

239-
\funcdesc{last}{list}{
240-
{\em list}の最後の要素でなく、最後のconsを返す。}
239+
\funcdesc{last}{list \&optional (n 1)}{
240+
{\em list}の最後の要素でなく、最後の{\em n}個のconsを返す。}
241241

242242
\funcdesc{butlast}{list \&optional (n 1)}
243243
{{\em list}の最後から{\em n}個の要素を削除したリストを返す。}

doc/latex/sequences.tex

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -237,8 +237,8 @@ \subsection{Lists}
237237
\funcdesc{nthcdr}{count list}{
238238
applies {\bf cdr} {\em count} times to {\em list}.}
239239

240-
\funcdesc{last}{list}{
241-
the last cons is returned, not the last element.}
240+
\funcdesc{last}{list \&optional (n 1)}{
241+
the last {\em n} conses is returned, not the last element.}
242242

243243
\funcdesc{butlast}{list \&optional (n 1)}
244244
{ returns a list which does not contain the last {\em n} elements.}

lisp/l/common.l

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,10 @@ if pos is bigger than the length of list, item is nconc'ed at the tail"
448448
(nconc result1 result2)))
449449

450450
(defun rotate-list (l) (append (cdr l) (list (car l))))
451-
(defun last (x)
452-
(while (consp (cdr x)) (setq x (cdr x)))
453-
x)
451+
(defun last (x &optional (n 1))
452+
(unless (and (integerp n) (>= n 0))
453+
(error "last &optional n must be a non negative integer"))
454+
(nthcdr (max (- (length x) n) 0) x))
454455
(defun copy-tree (x) (subst t t x))
455456
(defun copy-list (x) (nreverse (reverse x)))
456457
(defun nreconc (x y) (nconc (nreverse x) y))

0 commit comments

Comments
 (0)