/** Replace the old cons cell with the macro expansion, and return it. **/ public Pair expand(Scheme interpreter, Pair oldPair, Object args) { Object expansion = apply(interpreter, args); if (expansion is Pair) { oldPair.first = ((Pair)expansion).first; oldPair.rest = ((Pair)expansion).rest; } else { oldPair.first = "begin"; oldPair.rest = cons(expansion, null); } return oldPair; }
/** Map proc over a list of lists of args, in the given interpreter. * If result is non-null, accumulate the results of each call there * and return that at the end. Otherwise, just return null. **/ static Pair map(Procedure proc, Object args, Scheme interp, Pair result) { Pair accum = result; if (rest(args) == null) { args = first(args); while (args is Pair) { Object x = proc.apply(interp, list(first(args))); if (accum != null) accum = (Pair) (accum.rest = list(x)); args = rest(args); } } else { Procedure car = Procedure.proc(interp.eval("car")); Procedure cdr = Procedure.proc(interp.eval("cdr")); while (first(args) is Pair) { Object x = proc.apply(interp, map(car, list(args), interp, list(null))); if (accum != null) accum = (Pair) (accum.rest = list(x)); args = map(cdr, list(args), interp, list(null)); } } return (Pair)rest(result); }