Esempio n. 1
0
def square_list(list):
    if utility.is_null(list):
        return utility.list()
    else:
        return utility.cons(
            square(utility.car(list)),
            square_list(utility.cdr(list)))
Esempio n. 2
0
 def same_parity_iter(a):
     """
     :type a: FunctionType | object
     :return:
     """
     if is_null(a):
         return None
     else:
         b = car(a)  # type: int
         if is_even(x - b):
             return cons(b, same_parity_iter(cdr(a)))
         else:
             return same_parity_iter(cdr(a))
Esempio n. 3
0
def same_parity(x, *z):
    """
    :type x: int
    :type z: object
    :rtype: FunctionType
    """
    y = list(*z)

    def same_parity_iter(a):
        """
        :type a: FunctionType | object
        :return:
        """
        if is_null(a):
            return None
        else:
            b = car(a)  # type: int
            if is_even(x - b):
                return cons(b, same_parity_iter(cdr(a)))
            else:
                return same_parity_iter(cdr(a))

    return cons(x, same_parity_iter(y))
Esempio n. 4
0
def make_interval(a, b):
    return utility.cons(a, b)
Esempio n. 5
0
def make_rectangle(p, q):
    return utility.cons(p, q)
Esempio n. 6
0
def make_segment(p, q):
    return utility.cons(p, q)
Esempio n. 7
0
def make_point(x, y):
    return utility.cons(x, y)
Esempio n. 8
0
from utility import cons, list, append, print_list

if __name__ == '__main__':
    x = list(1, 2, 3)
    y = list(4, 5, 6)

    print_list(append(x, y))
    print_list(cons(x, y))
    print_list(list(x, y))
Esempio n. 9
0
def make_rectangle(width, height):
    return utility.cons(width, height)
Esempio n. 10
0
def subsets(l):
    if is_null(l):
        return list(list())
    rest = subsets(cdr(l))
    return append(rest, map(lambda x: cons(car(l), x) , rest))
Esempio n. 11
0
def make_rat(n, d):
    if d < 0:
        return make_rat(-n, -d)
    g = gcd(n, d)
    return utility.cons(n // g, d // g)
Esempio n. 12
0
 def iter(things, answer):
     if is_null(things):
         return answer
     else:
         iter(cdr(things), cons(answer, square(car(things))))