Common Lisp:如何将关键字参数传递给另一个函数?
Common Lisp: how to pass keyword arguments to another function?
这里是 Common Lisp 新手。我无法理解在 Lisp 函数中传递的参数。例如,想象一下 Common Lisp(比如 SBCL)中的以下函数定义:
(defun foo (x <&-keyword goes here> args)
(let ((v (make-hash-table args)))
(setf (gethash "foo" v) x)
v))
我的问题是:在这种情况下,是否可以指定 &-keyword 将 foo
中的关键字参数传递给 make-hash-table
?我已经尝试将 &rest
作为 &-关键字,它总是给我以下警告:
; caught WARNING:
; The function MAKE-HASH-TABLE is called with odd number of keyword arguments.
我还阅读了关于 &allow-other-keys
作为可能的 &-关键字的信息,但似乎没有找到 foo
中的 make-hash-table
如何使用其他键。因此,我很难过。关键字参数可以传递给 Common Lisp 中的内部函数调用吗?如果是,怎么做?
你要找的是apply
:
(defun foo (x &rest mht-arguments)
(let ((v (apply #'make-hash-table mht-arguments)))
(setf (gethash "foo" v) x)
v))
另请参阅 ,了解如何将其与 &key
相结合。
这里是 Common Lisp 新手。我无法理解在 Lisp 函数中传递的参数。例如,想象一下 Common Lisp(比如 SBCL)中的以下函数定义:
(defun foo (x <&-keyword goes here> args)
(let ((v (make-hash-table args)))
(setf (gethash "foo" v) x)
v))
我的问题是:在这种情况下,是否可以指定 &-keyword 将 foo
中的关键字参数传递给 make-hash-table
?我已经尝试将 &rest
作为 &-关键字,它总是给我以下警告:
; caught WARNING:
; The function MAKE-HASH-TABLE is called with odd number of keyword arguments.
我还阅读了关于 &allow-other-keys
作为可能的 &-关键字的信息,但似乎没有找到 foo
中的 make-hash-table
如何使用其他键。因此,我很难过。关键字参数可以传递给 Common Lisp 中的内部函数调用吗?如果是,怎么做?
你要找的是apply
:
(defun foo (x &rest mht-arguments)
(let ((v (apply #'make-hash-table mht-arguments)))
(setf (gethash "foo" v) x)
v))
另请参阅 &key
相结合。