Пример #1
0
def problem_A16(grades):
    tests1 = [
        (
            """(define (square x) (* x x))
                 (define (f x y)
                  (let ((a (+ 1 (* x y)))
                        (b (- 1 y)))
                    (+ (* x (square a))
                       (* y b)
                       (* a b))))
                 (f 3 4)""",
            456,
        ),
    ]
    tests2 = [
        (
            """(define x 3)
                 (define y 4)

                 (let ((x (+ y 2))
                      (y (+ x 1)))
                  (cons x y))""",
            Pair(6, 4),
        ),
        ("""(let ((x 'hello)) x)""", "hello"),
    ]
    if check_func(scheme_eval, tests1, comp=scheme_equal):
        return True
    if check_func(scheme_eval, tests2, comp=scheme_equal):
        return True
Пример #2
0
def problem_2(grades):
    tests1 = [
        ('(a . b)', Pair('a', 'b')),
        ('(a)', Pair('a', nil)),
        ('(a b . c)', Pair('a', Pair('b', 'c')))
    ]
    tests2 = [
        ('(a b . c d)', 'Error'),
        ('((a b) (c (d (e f))))',
            pairify([['a', 'b'], ['c', ['d', ['e', 'f']]]])),
        ('(a . (b . (c . (d . ()))))',
            pairify(['a', 'b', 'c', 'd'])),
        ('(. . 2)', 'Error'),
        ('(2 . 3 4 . 5)', 'Error'),
    ]
    if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal):
        return True
    if check_func(catch_syntax_error(read_line), tests2, comp=scheme_equal):
        return True
Пример #3
0
def problem_2(grades):
    tests1 = [
        ("(a . b)", Pair("a", "b")),
        ("(a)", Pair("a", nil)),
        ("(a b . c)", Pair("a", Pair("b", "c"))),
    ]
    tests2 = [
        ("(a b . c d)", "Error"),
        ("((a b) (c (d (e f))))",
         pairify([["a", "b"], ["c", ["d", ["e", "f"]]]])),
        ("(a . (b . (c . (d . ()))))", pairify(["a", "b", "c", "d"])),
        ("(. . 2)", "Error"),
        ("(2 . 3 4 . 5)", "Error"),
        ("(2 (3 . 4) 5)", Pair(2, Pair(Pair(3, 4), Pair(5, nil)))),
    ]
    if check_func(catch_syntax_error(read_line), tests1, comp=scheme_equal):
        return True
    if check_func(catch_syntax_error(read_line), tests2, comp=scheme_equal):
        return True
Пример #4
0
def pairify(lst):
    if not lst:
        return nil
    if type(lst) is not list:
        return lst
    return Pair(pairify(lst[0]), pairify(lst[1:]))
Пример #5
0
def pairify(lst):  # parify从函数的名字就可以听出,是将lst Pair化
    if not lst:
        return nil  # 这个nil是全局唯一一份的nil
    if type(lst) is not list:
        return lst
    return Pair(pairify(lst[0]), pairify(lst[1:]))