Lisp - 如何检查列表是否为点对?

זאב*_*כהן 4 lisp predicate list common-lisp

如何检查lisp中的列表是否为点对?

CL-USER 20 : 3 > (dotted-pair-p (cons 1 2))
T

CL-USER 20 : 3 > (dotted-pair-p '(1 2))
NIL

CL-USER 20 : 3 > (dotted-pair-p '(1 2 3))
NIL
Run Code Online (Sandbox Code Playgroud)

我试过检查是否length=2但有错误:

CL-USER 28 : 1 > (= (length (cons 2 3)) 2)
Error: In a call to LENGTH of (2 . 3), tail 3 is not a LIST.
Run Code Online (Sandbox Code Playgroud)

nix*_*gle 9

"点对符号"中的lisp列表类似于:

(1 . ()).
Run Code Online (Sandbox Code Playgroud)

由于这是家庭作业,我会让你把它归结为合乎逻辑的结论.相比

(LIST 1 2) => (1 . (2 . ()))
Run Code Online (Sandbox Code Playgroud)

(CONS 1 2) => (1 . 2).
Run Code Online (Sandbox Code Playgroud)

这两者有什么不同?你怎么能用谓词来区分?

请记住,所有正确的lisp列表都以空列表结尾.问问自己,你如何获得一对利弊的第二个元素?那里的解决方案应该很明确.