def make_rat(n, d): # return cons(n, d) g = gcd(abs(n), abs(d)) sign = 1 if n < 0 and d >= 0 or d < 0 and n >= 0: sign = -1 return cons(int(abs(n / g) * sign), int(abs(d / g)))
def make_interval(a, b): return cons(a, b)
def make_segment(start, end): return cons(start, end)
def midpoint_segment(segment): return cons( average(x_point(start_segment(segment)), x_point(end_segment(segment))), average(y_point(start_segment(segment)), y_point(end_segment(segment))))
def make_point(x, y): return cons(x, y)
def make_rectangle_2(left_top, x, y): return cons(left_top, make_point(x_point(left_top) + x, y_point(left_top) - y))
def make_rectangle(left_top, right_bottom): return cons(left_top, right_bottom)
def append(list1, list2): if null(list1): return list2 return cons(car(list1), append(cdr(list1), list2))
from util_pair import cons, car, cdr, list, null, print_list x = cons(1, cons(2, cons(3, cons(4, None)))) print_list(x) def list_ref(items, n): if (n == 0): return car(items) return list_ref(cdr(items), n - 1) squares = list([1, 4, 9, 16, 25]) print(list_ref(squares, 3)) def length(items): if (null(items)): return 0 return 1 + length(cdr(items)) odds = list([1, 3, 5, 7]) print(length(odds)) def append(list1, list2): if null(list1): return list2
def make_rat(n, d): # return cons(n, d) g = gcd(n, d) return cons(int(n / g), int(d / g))
from util_pair import cons, car, cdr, list, print_list x = cons(1, cons(2, cons(3, cons(4, None)))) print_list(x) one_through_four = list([1, 2, 3, 4]) print_list(one_through_four) print(car(one_through_four)) print_list(cdr(one_through_four)) print(car(cdr(one_through_four))) print_list(cons(10, one_through_four)) print_list(cons(5, one_through_four))
def make_rat(n, d): return cons(n, d)