要使用emacs lisp过滤列表,应使用remove-if
函数。以下是remove-if:
remove-if is an alias for `cl-remove-if' in `cl.el'.
(remove-if PREDICATE SEQ [KEYWORD VALUE]...)
Remove all items satisfying PREDICATE in SEQ.
This is a non-destructive function; it makes a copy of SEQ if necessary
to avoid corrupting the original SEQ.
Keywords supported: :key :count :start :end :from-end
以下是remove-if
表现不像我认为的那样:
(defvar states '( ('USA 'PA) ('USA 'VA) ('USA 'CA) ))
(remove-if (lambda (row) (not (equal (cadr row) 'CA))) states)
我希望这应该返回('CA)
,但事实并非如此。而是返回nil.
nil
而不是('CA)
?感谢您的帮助!
修改
我看到如果我将states
的定义更改为不引用条目,那么我写的内容就可以了。
答案 0 :(得分:1)
不要在列表中加注引号。只需引用该列表即可:
(defvar states '( (USA PA) (USA VA) (USA CA) ))
这种情况下的返回值是:
((USA CA))