01: /*
02: * last.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: last.java,v 1.5 2003/12/13 00:58:51 piso Exp $
06: *
07: * This program is free software; you can redistribute it and/or
08: * modify it under the terms of the GNU General Public License
09: * as published by the Free Software Foundation; either version 2
10: * of the License, or (at your option) any later version.
11: *
12: * This program is distributed in the hope that it will be useful,
13: * but WITHOUT ANY WARRANTY; without even the implied warranty of
14: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15: * GNU General Public License for more details.
16: *
17: * You should have received a copy of the GNU General Public License
18: * along with this program; if not, write to the Free Software
19: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
20: */
21:
22: package org.armedbear.lisp;
23:
24: // ### last
25: // last list &optional n => tail
26: public final class last extends Primitive {
27: public last(String name, String arglist) {
28: super (name, arglist);
29: }
30:
31: public LispObject execute(LispObject arg) throws ConditionThrowable {
32: LispObject list = checkList(arg);
33: if (list == NIL)
34: return NIL;
35: LispObject result = list;
36: int n = 1;
37: while (list instanceof Cons) {
38: list = list.cdr();
39: if (n-- <= 0)
40: result = result.cdr();
41: }
42: return result;
43: }
44:
45: public LispObject execute(LispObject first, LispObject second)
46: throws ConditionThrowable {
47: LispObject list = checkList(first);
48: if (second instanceof Fixnum) {
49: int n = ((Fixnum) second).getValue();
50: if (n >= 0) {
51: if (list == NIL)
52: return NIL;
53: LispObject result = list;
54: while (list instanceof Cons) {
55: list = list.cdr();
56: if (n-- <= 0)
57: result = result.cdr();
58: }
59: return result;
60: }
61: } else if (second instanceof Bignum) {
62: if (list == NIL)
63: return NIL;
64: LispObject n = second;
65: LispObject result = list;
66: while (list instanceof Cons) {
67: list = list.cdr();
68: if (!n.plusp())
69: result = result.cdr();
70: n = n.decr();
71: }
72: return result;
73: }
74: return signal(new TypeError(second, "non-negative integer"));
75: }
76:
77: private static final last LAST = new last("last",
78: "list &optional n");
79: }
|