list - 获取列表中的第 N 个元素作为列表而不是 LISP 中的数字?

标签 list lisp

假设你有

(setq list '(1 2 3 4 5))

当使用Nth返回第一个元素时,结果是一个数字:

(nth 0 list)
Result: 1

有没有办法将结果作为列表返回或将结果转换为列表?如:

Result: (1)

最佳答案

人们总是可以将任何对象变成 list使用 list功能:

(defparameter *list* '(1 2 3 4 5))
(list (nth 0 *list*))
==> (1)

但是,如果您希望获得包含数字的参数的切片,您需要意识到如果不修改参数是不可能的*列表*:

(defparameter *tail* (nthcdr 2 *list*))
*tail*
==> (3 4 5)

以下截断两者 *tail* *list*:

(setf (cdr *tail*) nil)
*tail*
==> (3)
*list*
==> (1 2 3)

关于list - 获取列表中的第 N 个元素作为列表而不是 LISP 中的数字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43894314/

相关文章:

html - 带有 justify-content :space-evenly, 的 100% 宽度 flex 水平列表如何将元素符号居中放置在空格中?

emacs - 如何强制 Emacs 不在特定窗口中显示缓冲区?

emacs - 如何使 swank 与 stumpw 一起工作?

python - 如何检查是否到达列表末尾?

python - 如何随机匹配多个相同长度列表中的元素?

c# - 按键/属性将列表分组为子列表而不更改列表顺序(只需按属性切入列表)

lisp - 学习 Lisp。似乎无法从一个函数中获取值并在另一个函数中使用它

functional-programming - Lisp - 在 n 个列表中拆分列表

lisp - 从列表列表中提取项目?

c# - 鉴于定义了强制转换运算符,如何将 List<Foo> 隐式转换为 List<Bar> ?