def make_list_eater(name):
    spec     = name[1:-1]
    unrolled = unroll.unrolling_iterable(reversed(spec))

    contract = "pair?"

    for letter in spec[1::-1]:
        if letter == 'a':
            contract = "(cons/c %s any/c)" % contract
        elif letter == 'd':
            contract = "(cons/c any/c %s)" % contract
        else:
            assert False, "Bad list eater specification"

    @expose(name, [values.W_Object])
    def process_list(_lst):
        lst = _lst
        for letter in unrolled:
            if not isinstance(lst, values.W_Cons):
                raise SchemeException("%s: expected %s given %s" % (name, contract, _lst))
            if letter == 'a':
                lst = lst.car()
            elif letter == 'd':
                lst = lst.cdr()
            else:
                assert False, "Bad list eater specification"
        return lst
    process_list.__name__ = "do_" + name
    return process_list
For generating car, cdr, caar, cadr, etc...
dataset/ETHPy150Open samth/pycket/pycket/prims/general.py/make_list_eater