def multiinsertLR_starred_and_co(new, oldL, oldR, l, col): """Inserts new to the left of `oldL` and to the right of `oldR`. NB: Uses a collector func to gather new list and count L, R inserts """ if is_null(l): return col(quote(), 0, 0) if is_atom(car(l)): if is_eq(car(l), oldL): return multiinsertLR_starred_and_co( new, oldL, oldR, cdr(l), lambda newl, L, R: col( cons(new, cons(oldL, newl)), add1(L), R)) if is_eq(car(l), oldR): return multiinsertLR_starred_and_co( new, oldL, oldR, cdr(l), lambda newl, L, R: col( cons(oldR, cons(new, newl)), L, add1(R))) return multiinsertLR_starred_and_co( new, oldL, oldR, cdr(l), lambda newl, L, R: col(cons(car(l), newl), L, R)) return multiinsertLR_starred_and_co( new, oldL, oldR, car(l), lambda carnl, carL, carR: multiinsertLR_starred_and_co( new, oldL, oldR, cdr(l), lambda cdrnl, cdrL, cdrR: col( cons(carnl, cdrnl), add(carL, cdrL), add(carR, cdrR))))
def _is_atom(x): """Asks if atom, checks for primitives and non-primitives as well.""" if is_atom(x): return True if is_null(x): return False if is_eq(car(x), quote("primitive")): return True if is_eq(car(x), quote("non-primitive")): return True return False
def evcon(lines, table): """Helper to evaluate `cond` lines.""" if is_else(question_of(car(lines))): return meaning(exp=answer_of(car(lines)), table=table) if meaning(exp=uestion_of(car(lines)), table=table): return meaning(exp=answer_of(car(lines)), table=table) return evcon(cdr(lines), table)
def _lookup_in_entry_h(name, names, values, entry_f): """Helper func for `lookup_in_entry`.""" if is_null(names): return entry_f(name) if is_eq(name, car(names)): return car(values) return _lookup_in_entry_h(name, cdr(name), cdr(values), entry_f)
def leftmost(l): """Returns the leftmost atom from the list `l`.""" if is_null(l): return quote() elif is_atom(car(l)): return car(l) else: return leftmost(car(l))
def no_nums(l): """Remove all the numbers in the list `l`.""" if is_null(l): return quote() elif is_atom(car(l)): if is_number(car(l)): return no_nums(cdr(l)) else: return cons(car(l), (no_nums(cdr(l)))) else: return cons(no_nums(car(l)), no_nums(cdr(l)))
def all_nums(l): """Return all numbers from list `l`.""" if is_null(l): return quote() elif is_atom(car(l)): if is_number(car(l)): return cons(car(l), all_nums(cdr(l))) else: return all_nums(cdr(l)) else: return cons(all_nums(car(l)), all_nums(cdr(l)))
def occur_starred(a, l): """Returns the the number of occurrences of `a` in `l`.""" if is_null(l): return 0 elif is_atom(car(l)): if is_eq(car(l), a): return add1(occur_starred(a, cdr(l))) else: return occur_starred(a, cdr(l)) else: return add(occur_starred(a, car(l)), occur_starred(a, cdr(l)))
def insertR_starred(new, old, l): """Inserts `new` to the left of `old`, if `old` found in `l` for every occurrence.""" if is_null(l): return quote() elif is_atom(car(l)): if is_eq(car(l), old): return cons(new, cons(old, insertR_starred(new, old, cdr(l)))) else: return cons(car(l), insertR_starred(new, old, cdr(l))) else: return cons(insertR_starred(new, old, car(l)), insertR_starred(new, old, cdr(l)))
def question_of(line): """Gets the question from a line. Example: ((null? x) (quote('else')) Here would return (null? x), as this is the `car` of the list """ return car(line)
def evlis(args, table): """This returns a list composed of the meaning of each argument.""" if is_null(args): return quote() return cons(exp=meaning(car(args), table=table), evlis(args=cdr(args), table=table))
def apply_primitive(name, vals): """Applies the primitive function `name`, with args=`vals`.""" if is_eq(name, quote("cons")): return cons(first(vals), second(vals)) if is_eq(name, quote("car")): return car(first(vals)) if is_eq(name, quote("cdr")): return cdr(first(vals)) if is_eq(name, quote("null?")): return is_null(first(vals)) if is_eq(name, quote("eq?")): return is_eq(first(vals), second(vals)) if is_eq(name, quote("atom?")): return _is_atom(first(vals)) if is_eq(name, quote("zero?")): return is_zero(first(vals)) if is_eq(name, quote("add1")): return add1(first(vals)) if is_eq(name, quote("sub1")): return sub1(first(vals)) if is_eq(name, quote("number?")): return is_number(first(vals))
def is_pair(l): """Asks if `l` is in fact a `pair`.""" if _or(is_atom(l), _or(is_null(l), is_null(car(l)))): return False if is_null(cdr(cdr(l))): return True return False
def rempick(a, lat): """Remove the atom at position `a` from the list, `l`.""" if is_null(lat): return quote() elif is_zero(sub1(a)): return (cdr(lat)) else: return cons(car(lat), rempick(sub1(a), cdr(lat)))
def pick(a, lat): """Pick function, returns the atom at position `a` for list `lat`.""" if is_null(lat): return quote() elif is_zero(sub1(a)): return car(lat) else: return pick(sub1(a), cdr(lat))
def evens_only_and_co(l, col): """Collects, evens, the multiplication of evens and addition of odds.""" if is_null(l): return col(quote(), 1, 0) if is_atom(car(l)): if is_even(car(l)): return evens_only_and_co( cdr(l), lambda evens, p, s: col(cons(car(l), evens), multiply(car(l), p), s)) return evens_only_and_co( cdr(l), lambda evens, p, s: col(evens, p, add(car(l), s))) return evens_only_and_co( car(l), lambda car_evens, carp, cars: evens_only_and_co( cdr(l), lambda cdr_evens, cdrp, cdrs: col( cons(car_evens, cdr_evens), multiply(carp, cdrp), add(cars, cdrs))))
def arguments_of(exp): """Returns the arguments of the function an application.""" return car(cdr(cdr(exp)))
def formals_of(exp): """Gets the formals from t_lambda eval.""" return car(cdr(cdr(exp)))
def cond_lines_of(lines): """Retrieves only the `cond` lines in exp.""" if is_eq(car(car(lines)), quote("cond")): return cdr(lines) return cond_lines_of(cdr(lines))
def function_of(exp): """Returns the function of an application.""" return car(cdr(exp))
def operator(nexp): """Returns the operator from `aexp`.""" return car(cdr(aexp))
def answer_of(line): """Gets the answer of the line, if the question is Truthy.""" return car(cdr(line))
def second(pair): """Returns the second value of`pair`.""" return car(cdr(pair))
def second_sub_expr(aexp): """Returns the second sub expression from `aexp`.""" return car(cdr(cdr(aexp)))
def table_of(exp): """Gets the table from t_lambda eval.""" return car(cdr(exp))
def body_of(exp): """Gets the body from t_lambda eval.""" return car(cdr(cdr(cdr(exp))))
def first_sub_expr(aexp): """Returns the first sub expression from `aexp`.""" return car(aexp)
def initial_table(name): """Initial table helper func. NOTE: Should never get used. """ return car(quote())
def first(pair): """Returns the first value of `pair`.""" return car(pair)