final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
Sequence zero = args[1];
Sequence input = args[0];
while (!input.isEmpty()) {
final SequenceIterator i = input.iterate();
zero = ref.evalFunction(contextSequence, null, new Sequence[] { zero, i.nextItem().toSequence() });
ValueSequence tail = new ValueSequence();
while (i.hasNext()) {
tail.add(i.nextItem());
}
input = tail;
}
result = zero;
} else if (isCalledAs("fold-right")) {
final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
final Sequence zero = args[1];
final Sequence input = args[0];
result = foldRight(ref, zero, input, contextSequence);
} else if (isCalledAs("map-pairs")) {
final FunctionReference ref = (FunctionReference) args[0];
ref.analyze(cachedContextInfo);
final SequenceIterator i1 = args[1].iterate();
final SequenceIterator i2 = args[2].iterate();
while (i1.hasNext() && i2.hasNext()) {
final Sequence r = ref.evalFunction(contextSequence, null,
new Sequence[] { i1.nextItem().toSequence(), i2.nextItem().toSequence() });
result.addAll(r);
}
} else if (isCalledAs("for-each-pair")) {
final FunctionReference ref = (FunctionReference) args[2].itemAt(0);
ref.analyze(cachedContextInfo);
final SequenceIterator i1 = args[0].iterate();
final SequenceIterator i2 = args[1].iterate();
while (i1.hasNext() && i2.hasNext()) {
final Sequence r = ref.evalFunction(contextSequence, null,
new Sequence[] { i1.nextItem().toSequence(), i2.nextItem().toSequence() });
result.addAll(r);
}
}
return result;
}