【发布时间】:2021-06-21 21:17:59
【问题描述】:
在尝试“减少”apply 函数时,正确的理解是否正确?
例如(apply func args)
- 删除
(apply和匹配的)。 - 插入
func作为args的第一个元素,如有必要,将外部引号移至一级。
这是一个应用程序:
; (add-pairs '((1 2)(3 4))) --> (3 7)
(define (add-pairs ps)
(if (null? ps) nil
(cons (+ (car (car ps)) (cadr (car ps))) (add-pairs (cdr ps)))))
(apply add-pairs '(((1 2) (3 4))))
xxxxxxx x
; Remove the "(apply " and matching ")"
add-pairs '(((1 2) (3 4)))
------------^
; Insert the function into the args, moving the quote in one level if required.
(add-pairs '((1 2) (3 4)))
以上是显示apply 如何添加的准确方法,还是我遗漏了什么?
【问题讨论】:
-
apply通常用于动态计算参数时,因此无需删除括号,也无需移动引号。 -
例如
(apply + list-of-numbers) -
@Barmar 怎么样:
(apply + '(1 2))-->(+ 1 2)那里去掉了一层括号,不是吗? -
不要从语法上考虑它,了解它做什么。
-
它只是将一个列表传播到函数的参数列表中。
标签: scheme lisp racket apply sicp