by kazinator · 10y ago · 0 comments
void markobject (object *obj) {
if (obj == NULL) return;
object* arg = car(obj);
if (marked(obj)) return;
int type = obj->type;
mark(obj);
if (type != SYMBOL && type != NUMBER) { // cons
markobject(arg);
markobject(cdr(obj)); // <--- TAIL CALL!
}
}
It's simple enough to obj = cdr(obj) and wrap a loop around this.That's why I wrote "in proportion to list length", not "tree structure depth" in general.
Above, we still have recursion when marking the arg = car(obj) value. However, Lisp lists tend to be greatly unbalanced in favor of growing in the cdr direction; that is why it helps to iterate on the cdr.