01: package kawa.standard;
02:
03: import gnu.mapping.*;
04: import gnu.lists.*;
05:
06: /**
07: * Implement the Scheme standard function "append".
08: * @author Per Bothner
09: */
10:
11: public class append extends ProcedureN {
12: public static final append append = new append();
13: static {
14: append.setName("append");
15: }
16:
17: public Object applyN(Object[] args) {
18: return append$V(args);
19: }
20:
21: public static Object append$V(Object[] args) {
22: int count = args.length;
23: if (count == 0)
24: return LList.Empty;
25: Object result = args[count - 1];
26: for (int i = count - 1; --i >= 0;) {
27: Object list = args[i];
28: Object copy = null;
29: Pair last = null;
30: while (list instanceof Pair) {
31: Pair list_pair = (Pair) list;
32: Pair new_pair = new Pair(list_pair.car, null);
33: if (last == null)
34: copy = new_pair;
35: else
36: last.cdr = new_pair;
37: last = new_pair;
38: list = list_pair.cdr;
39: }
40: if (list != LList.Empty)
41: throw new WrongType(append, i + 1, args[i], "list");
42: if (last != null) {
43: last.cdr = result;
44: result = copy;
45: }
46: }
47: return result;
48: }
49: }
|