在普通的lisp中使用& allow-other-keys

时间:2015-11-25 13:30:02

标签: common-lisp

我想制作最通用的函数,并决定使用键作为参数。 我想使用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")

我知道rest是剩余的args,但它有一个数组。它不绑定值cd,甚至可以像关联列表一样构建它们(即像(cdr (assoc 'c rest))那样)

您有线索或解决方案吗?或者我可能走向错误的方向?

提前致谢

1 个答案:

答案 0 :(得分:8)

从什么时候开始一个数组?标准说 list 。对于关键字参数,这是属性列表。请参阅getf以访问属性列表的元素。

还可以使用DESTRUCTURING-BIND来访问该属性列表的内容:

CL-USER 15 > (defun foo (a &rest args &key b &allow-other-keys)
               (destructuring-bind (&key (c nil c-p) ; var default present?
                                         (d t   d-p)
                                         (e 42  e-p)
                                    &allow-other-keys)
                   args
                 (list (list :c c c-p)
                       (list :d d d-p)
                       (list :e e e-p))))
FOO

; c-p, d-p, e-p  show whether the argument was actually present
; otherwise the default value will be used

CL-USER 16 > (foo 10 :b 20 :d 30)
((:C NIL NIL) (:D 30 T) (:E 42 NIL))

但是已经可以在参数列表中完成同样的工作......