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 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 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 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 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 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 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 expression_to_action(exp): """Converts an `exp` to an action.""" if is_atom(exp): return atom_to_action(exp)
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 is_else(x): """Asks if `x` is an else statement.""" if is_atom(x): return is_eq(x, quote("else")) return False