Beispiel #1
0
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))))
Beispiel #2
0
def union(set1, set2):
    """Gathers the union between `set1` and `set2`."""
    if is_null(set1):
        return set2

    if is_member(car(set1), set2):
        return union(cdr(set1))

    return cons(car(set1), union(cdr(set1), set2))
Beispiel #3
0
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)))
Beispiel #4
0
def is_set(lat):
    """Checks if `lat` is a set,  returns True if so, else False."""
    if is_null(lat):
        return True

    if is_member(car(lat), cdr(lat)):
        return False

    return is_set(cdr(lat))
Beispiel #5
0
def make_set(lat):
    """Makes `lat` into `set`."""
    if is_null(lat):
        return quote()

    if is_member(car(lat), cdr(lat)):
        return make_set(cdr(lat))

    return cons(car(lat), make_set(cdr(lat)))
Beispiel #6
0
def intersect(set1, set2):
    """Gathers the intersect between `set1` and `set2`."""
    if is_null(set1):
        return quote()

    if is_member(car(set1), set2):
        return cons(car(set1), intersect(cdr(set1), set2))

    return intersect(cdr(set1))
Beispiel #7
0
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)
Beispiel #8
0
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
Beispiel #9
0
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)))
Beispiel #10
0
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)))
Beispiel #11
0
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)))
Beispiel #12
0
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)))
Beispiel #13
0
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)))
Beispiel #14
0
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)))
Beispiel #15
0
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))
Beispiel #16
0
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))
Beispiel #17
0
def length(lat):
    """Returns the length of the lateral list `l`."""
    if is_null(lat):
        return 0

    else:
        return add1(length(cdr(lat)))
Beispiel #18
0
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)))
Beispiel #19
0
def t_lambda(exp, table):
    """`t_lambda` type function: `*lambda` in schmeme.

    Since for non-primitive functions, the args & func body
    need to be remembered, this is simply `cdr(exp)`
    """
    return build(quote("non-primitive"),
                 cons(table, cdr(exp)))
Beispiel #20
0
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)
Beispiel #21
0
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))
Beispiel #22
0
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))))
Beispiel #23
0
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))
Beispiel #24
0
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))
Beispiel #25
0
def is_intersect(set1, set2):
    """Asks if `set1` is an intersect of `set2`."""
    if is_null(set1):
        return False

    return _or(is_member(car(set1), set2), is_intersection(cdr(set1), set2))
Beispiel #26
0
def function_of(exp):
    """Returns the function of an application."""
    return car(cdr(exp))
Beispiel #27
0
def body_of(exp):
    """Gets the body from t_lambda eval."""
    return car(cdr(cdr(cdr(exp))))
Beispiel #28
0
def formals_of(exp):
    """Gets the formals from t_lambda eval."""
    return car(cdr(cdr(exp)))
Beispiel #29
0
def arguments_of(exp):
    """Returns the arguments of the function an application."""
    return car(cdr(cdr(exp)))
Beispiel #30
0
def answer_of(line):
    """Gets the answer of the line, if the question is Truthy."""
    return car(cdr(line))