Unfortunately there isn’t an elegant way for us to allow for this, without adding in some special syntax. So we’re going to hard-code some system into our language using a special symbol &.

    When assigning our formal arguments we’re going to look for a & symbol and if it exists take the next formal argument and assign it any remaining supplied arguments we’ve been passed. It’s important we convert this argument list to a Q-Expression. We need to also remember to check that & is followed by a real symbol, and if it isn’t we should throw an error.

    Suppose when calling the function the user doesn’t supply any variable arguments, but only the first named ones. In this case we need to set the symbol following to the empty list. Just after we delete the argument list, and before we check to see if all the formals have been evaluated, add in this special case.

    1. /* If '&' remains in formal list bind to empty list */
    2. if (f->formals->count > 0 &&
    3. strcmp(f->formals->cell[0]->sym, "&") == 0) {
    4. /* Check to ensure that & is not passed invalidly. */
    5. if (f->formals->count != 2) {
    6. "Symbol '&' not followed by single symbol.");
    7. }
    8. /* Pop and delete '&' symbol */
    9. lval_del(lval_pop(f->formals, 0));
    10. lval* sym = lval_pop(f->formals, 0);
    11. lval* val = lval_qexpr();
    12. /* Bind to environment and delete */
    13. lenv_put(f->env, sym, val);