object *replace_atom(object *sexp, object *with) { if (sexp->type == CONS) { object *list = cons(replace_atom(first(sexp), with), NULL); sexp = second(sexp); // Recurse through the list. while (sexp != NULL && sexp->type == CONS) { append(list, replace_atom(first(sexp), with)); sexp = second(sexp); } return list; } else { object* tmp = with; while (tmp != NULL && tmp->type == CONS) { object *item = first(tmp); object *atom = first(item); object *replacement = first(second(item)); if (strcmp(name(atom), name(sexp)) == 0) return replacement; tmp = second(tmp); } return sexp; } }
object *replace_atom (object *sexp, object *with) { if(sexp->type == CONS){ object *list = cons(replace_atom(car(sexp), with),NULL); sexp = cdr(sexp); while (sexp != NULL && sexp->type == CONS){ append(list,replace_atom(car(sexp), with)); sexp = cdr(sexp); } return list; }else{ object* tmp = with; while (tmp != NULL && tmp->type == CONS) { object *item = car(tmp); object *atom = car(item); object *replacement = car(cdr(item)); if(strcmp(name(atom),name(sexp)) == 0) return replacement; tmp = cdr(tmp); } return sexp; } }
object *fn_lambda (object *args, object *env) { object *lambda = car(args); args = cdr(args); object *list = interleave((((lambda_object *) (lambda))->args),args); object* sexp = replace_atom((((lambda_object *) (lambda))->sexp),list); return eval(sexp,env); }
object *fn_lambda(object *args, object *env) { // Lambda objects hold two lists, the parameters and the function. object *lambda = first(args); args = second(args); // Extract the list of arguments object *list = interleave((((lambda_object *) (lambda))->args), args); // Extract the function S-Expression object* sexp = replace_atom((((lambda_object *) (lambda))->sexp), list); return eval(sexp, env); }