5.2 列表判断函数

下面这些判断函数用于检测一个 Lisp 对象是否为 atom原子、是否为 cons 单元、是否为列表,或是否为特殊对象 nil。(其中许多判断函数可以用其他函数定义,但因为使用非常频繁,所以单独提供它们是值得的。)

Function: consp object

如果 object 是一个 cons 单元,该函数返回 t,否则返回 nilnil 不是 cons 单元,尽管它 一个列表。

Function: atom object

如果 object 是一个原子,该函数返回 t,否则返回 nil。除 cons 单元以外的所有对象都是原子。符号 nil 既是原子,也是列表;它是 Lisp 中唯一同时具备这两种属性的对象。

(atom object) ≡ (not (consp object))
Function: listp object

如果 object 是 cons 单元或 nil,该函数返回 t;否则返回 nil

(listp '(1))
     ⇒ t
(listp '())
     ⇒ t
Function: nlistp object

该函数与 listp 相反:如果 object 不是列表,则返回 t,否则返回 nil

(listp object) ≡ (not (nlistp object))
Function: null object

如果 objectnil,该函数返回 t,否则返回 nil。该函数与 not 完全等价,但为了代码清晰:当 object 被视为布尔真值时使用 not(参见 条件组合结构 中的 not),其他情况则使用 null

(null '(1))
     ⇒ nil
(null '())
     ⇒ t
Function: proper-list-p object

如果 objectproper list正规列表,该函数返回其长度;否则返回 nil(see 列表与 Cons 单元)。正规列表除了满足 listp 外,还必须既不是循环列表,也不是点列表。

(proper-list-p '(a b c))
    ⇒ 3
(proper-list-p '(a b . c))
    ⇒ nil

emacs

Emacs

org-mode

Orgmode

Donations

打赏

Copyright

© 2025 Jasper Hsu

Creative Commons

Creative Commons

Attribute

Attribute

Noncommercial

Noncommercial

Share Alike

Share Alike