append.java 1.1 KB

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