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 _value(nexp): if is_eq(op(nexp), _quote("+")): return add(_value(fse(nexp)), (_value(sse(nexp)))) if is_eq(op(nexp), _quote("x")): return multiply(_value(fse(nexp)), (_value(sse(nexp)))) return expon(_value(fse(nexp)), (_value(sse(nexp))))
def t_const(exp, table): """`t_const` type function: `*const` in schmeme.""" if is_number(exp): return exp if is_eq(exp, quote("True")): return True if is_eq(exp, quote("False")): return False return build(quote("primitive", exp))
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 rember(a, lat): """Removes `a` from `lat` if it exists in `lat`.""" if is_null(lat): return quote() if is_eq(car(lat), a): return cdr(lat) return cons(car(lat), rember(a, cdr(lat)))
def is_member(a, lat): """Asks if `a` is a member of `lat`. Assumes `lat` is a lat """ if is_null(lat): return False if is_eq(a, car(lat)): return True return is_member(a, cdr(lat))
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 rember_starred(a, l): """Removes all occurences of `a` in `l`. NB: `l` is not guaranteed to be a lat """ if is_null(l): return quote() if is_atom(l): if is_eq(car(l), a): return cdr(l) return cons(car(l), rember_starred(a, cdr(l))) return cons(rember_starred(a, cons(l)), rember_starred(a, cdr(l)))
def is_member_starred(a, l): """Asks if `a` is a member of `l`. NB: `l` is not guaranteed to be a lat """ if is_null(l): return False if is_atom(car(l)): if is_eq(a, car(l)): return True return is_member_starred(a, cdr(l)) _or(is_member(a, car(l)), is_member(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 list_to_action(exp): """Converts `exp` to `action, assuming `exp` is `list`.""" if is_atom(car(e)): if is_eq(car(e), quote("quote")):
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 is_else(x): """Asks if `x` is an else statement.""" if is_atom(x): return is_eq(x, quote("else")) return False
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_non_primitive(l): """Asks the question if `l` is a primitive.""" return is_eq(first(l), quote("non-primitive"))