我想制作最通用的函数,并决定使用键作为参数.我想使用,allow-other-keys因为我想使用任何键的功能.
让我演示给你看:
(defun myfunc (a &rest rest &key b &allow-other-keys)
;; Print A
(format t "A = ~a~%" a)
;; Print B if defined
(when b
(format t "B = ~a~%" b))
;; Here ... I want to print C or D or any other keys
;; ??
)
(myfunc "Value of A")
(myfunc "Value of A" :b "Value of B")
(myfunc "Value of A" :b "Value of B" :c "Value of C" :d "Value of D")
Run Code Online (Sandbox Code Playgroud)
我知道这 …
我正在寻找创建一个以变量作为值的关联列表(common lisp)。
让我用(虚拟)代码更好地解释:
(defun mylist-create (val1 val2)
(setq alist '((key1 . val1)
(key2 . val2)))
;; do other things here
;; and return the list
alist)
(format t "~a~%" (mylist-create "toto" "tata"))
Run Code Online (Sandbox Code Playgroud)
这里的问题是“val1”和“val2”不被视为变量,因为列表不是用它们的值创建的,而是用它们的名称创建的。
如何在列表创建中提取它们的价值?“setf and assoc”是添加键/值对的唯一解决方案吗?