使用elisp remove-if过滤列表

时间:2014-07-06 02:21:05

标签: emacs filter functional-programming elisp

要使用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.

  1. 为什么上面的示例返回nil而不是('CA)
  2. 感谢您的帮助!

    修改

    我看到如果我将states的定义更改为不引用条目,那么我写的内容就可以了。

1 个答案:

答案 0 :(得分:1)

不要在列表中加注引号。只需引用该列表即可:

(defvar states '( (USA PA) (USA VA) (USA CA) ))

这种情况下的返回值是:

((USA CA))