根据值从plist打印键?

时间:2011-05-09 19:17:16

标签: lisp common-lisp

如何基于给定的值迭代并打印plist的键?

示例:

; plist
(defun my-list() (list :a "hi" :b "no" :c "go"))

; from that list i want to iterate and print out keys based on values like:
for each x in ("hi" "go") print x

; hoping for:
ac

我是lisp的新手 - 谢谢你: - )

2 个答案:

答案 0 :(得分:13)

这样的东西
(loop for (key value) on my-list by #'cddr
      when (member value '("hi" "go") :test #'equal)
      do (princ key))

第一行在列表上移动一个模式。

答案 1 :(得分:4)

你可以使用循环宏:

(loop
   for (key value . rest) on list
   by #'cddr
   when (find value '("foo" "bar") :test #'string=)
   do (princ key))