CS61A_lab14_macro
(define-macro (switch expr cases) (cons 'cond (map (lambda (case) (cons (eq? (eval expr) (car case)) (cdr case))) cases)) )
这段代码是一个用于 Scheme 语言的宏定义,可以将一系列的条件分支语句转化为 Scheme 的 cond 表达式。
下面是具体的解释:
1. `(define-macro (switch expr cases) ...)`:定义一个名为 switch 的宏,宏的参数为 expr 和 cases。
2. `(cons 'cond ...)`:使用 Scheme 的 cons 函数将一个符号 'cond 和接下来的参数拼接在一起,以生成一个 cond 表达式。
3. `(map (lambda (case) (cons (eq? (eval expr) (car case)) (cdr case))) cases)`:使用 map 函数遍历 cases 中的每个分支,将其转化为一个 (condition . expression) 对,其中 condition 为一个布尔值,用于判断当前的表达式是否符合该分支的条件,而 expression 则为当前分支的表达式。
4. `(eq? (eval expr) (car case))`:eval 函数会求解表达式,将其结果返回。这里将 expr 作为参数传递给 eval 函数,以获取它的值。然后将其与当前分支的第一个元素(即该分支的条件)比较,以判断当前表达式是否符合该分支的条件。
5. `(cdr case)`:取出当前分支的第二个元素,即该分支的表达式,作为返回值。
最终,这个宏将根据传入的 expr 和 cases 参数生成一个 Scheme 的 cond 表达式,将 expr 的值与每个分支的条件进行比较,并执行符合条件的分支的表达式。
(eval expr) 是一个 Scheme 内置函数,用于求解并执行 Scheme 代码中的表达式 expr。它接收一个 Scheme 表达式作为参数,将其求值并返回结果。在这个宏定义中,我们使用 (eval expr) 来获取 switch 宏的第一个参数 expr 的值,从而对其进行条件判断,并执行符合条件的分支的表达式。
(switch x ((1) 'one) ((2) 'two) ((3) 'three) (else 'other)) ;宏替换之后 (cond ((eq? x 1) 'one) ((eq? x 2) 'two) ((eq? x 3) 'three) (else 'other))
如果前面有[ ` ] 才需要用逗号进行插值

def num_splits(s, d): """Return the number of ways in which s can be partitioned into two sublists that have sums within d of each other. >>> num_splits([1, 5, 4], 0) # splits to [1, 4] and [5] 1 >>> num_splits([6, 1, 3], 1) # no split possible 0 >>> num_splits([-2, 1, 3], 2) # [-2, 3], [1] and [-2, 1, 3], [] 2 >>> num_splits([1, 4, 6, 8, 2, 9, 5], 3) 12 """ "*** YOUR CODE HERE ***" length = len(s) tot = 0 def helper(first, second, index): nonlocal tot if index == length: if abs(first - second) <= d: tot += 1 return else: helper(first + s[index], second, index + 1) helper(first, second + s[index], index + 1) helper(0, 0, 0) return tot//2
This is a Python function named `num_splits` that takes two arguments: a list `s` and an integer `d`. The function returns the number of ways in which the list `s` can be partitioned into two sublists that have sums within `d` of each other.
The function uses a recursive helper function named `helper` that takes three arguments: `first`, `second`, and `index`. The `first` and `second` arguments represent the sums of the two sublists that are being constructed, and the `index` argument represents the current index in the list `s` that is being considered.
The `helper` function is called recursively with two different arguments: one in which the current element of `s` is added to the `first` sublist and the other in which it is added to the `second` sublist. The function keeps track of the total number of valid splits using a variable named `tot`.
At the end of the function, the total number of valid splits is divided by two and returned. This is because each valid split is counted twice (once for each possible order of the two sublists), so dividing by two gives the correct number of unique splits.
The function includes several examples in its docstring, which can be used to test the function.

def insert(link, value, index): """Insert a value into a Link at the given index. >>> link = Link(1, Link(2, Link(3))) >>> print(link) <1 2 3> >>> insert(link, 9001, 0) >>> print(link) <9001 1 2 3> >>> insert(link, 100, 2) >>> print(link) <9001 1 100 2 3> >>> insert(link, 4, 5) IndexError """ if index == 0: temp = Link(link.first, link.rest) link.first = value link.rest = temp elif link.rest is Link.empty: raise IndexError else: link.reset = insert(link.rest, value, index-1)

浙公网安备 33010602011771号