lisp nth 函数不适用于 cons-cell

标签 lisp elisp

如果我的变量是 list 和 cons-cell 的组合,如何在 lisp 中使用第 n 个函数 例如:
(setq aa '(1 2) )
(nconc aa (+ 1 2))
这返回我 (1 2 . 3)
当我说 (nth 1 aa) 它返回 2
但是当我使用 (nth 2 aa ) 它会抛出错误

最佳答案

NTH 返回第 n 个 (0, 1, 2 ...) cons 单元格的汽车。

因为你的第二个 cdr 不是一个 cons cell,所以得到它的车是一个错误。

(nthcdr 2 '(1 2 . 3))   returns 3
(last '(1 2 . 3))  returns (2 . 3)

通常最好避免不正确的列表,其中某些单元格的 cdr 不是 cons 或 NIL。

关于lisp nth 函数不适用于 cons-cell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2199272/

相关文章:

lisp - #<PACKAGE COMMON-LISP> 已锁定

plugins - 邪恶的 Emacs : is there a way to disable vim-like keys in «insert» mode?

emacs - 突出显示 eval-after-load 的错误使用

optimization - 如何让 SBCL 优化掉对 DEFINITION 的可能调用?

lisp - Vlisp autocad : condition (> a b) is true even when a is equal b. 为什么?

lisp - 如何制作 Racket 阅读器宏以将一个字符的任何实例转换为另外两个字符?

locking - 口齿不清,CLOS : adding a slot to the lock class

emacs - 有什么作用。 elisp 中的(句号)符号引用?

emacs - 为什么 re-search-backward 在 python 模式下会给出不同的结果?

emacs - 从emacs中的特定目录运行shell命令