如果 'apply' 将给定的函数应用于列表的所有元素,为什么以下不起作用:
> (apply println (list "a" "b" "c"))
. . println: contract violation
  expected: output-port?
  given: "b"
  argument position: 2nd
  other arguments...:
> 
https://docs.racket-lang.org/reference/procedures.html和https://docs.racket-lang.org/guide/application.html 中给出的示例与添加 (+) 功能有关。有人可以提供其他应用示例并解释它是如何工作的。谢谢。
就像(apply + (list 1 2 3 4))等价于(+ 1 2 3 4),(apply println (list "a" "b" "c"))等价于(println "a" "b" "c")。但是,这不是println. 正如错误消息所暗示的那样,第二个参数应该是输出端口,而不是字符串。
要正确调用printlnusing apply,您必须提供有效的参数列表,例如:
(apply println (list "a"))
;; or
(apply println (list "a" (current-output-port))
下面是一些使用applywith 函数的例子,这些函数接受任意数量的参数,因此无论列表的长度如何,都可以正常工作:
(apply * (list 1 2 3 4))    ; 24
(apply list (list 1 2 3 4)) ; (list 1 2 3 4) ¹
(apply set (list 1 2 3 4))  ; (set 1 2 3 4)
¹ 好吧,您不会真正使用applywithlist因为它只会给您返回相同的列表(或者更确切地说是它的副本),但是您可以。