01: /*
02: * last.java
03: *
04: * Copyright (C) 2003 Peter Graves
05: * $Id: last.java,v 1.6 2003/11/15 11:03:35 beedlem 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) {
28: super (name);
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: throw new ConditionThrowable(new TypeError(second,
75: "non-negative integer"));
76: }
77:
78: private static final last LAST = new last("last");
79: }
|