首页 > 解决方案 > 显示整个列表的弹出功能

问题描述

我正在尝试通过函数显示列表中的一个值来显示整个列表而不是一个值。下面的代码使用 elisp。

(defun element-i (L number)
  (if (not L) nil
      (if (< (length L) number) nil
          (dotimes (i number L)
            (pop L)))))

标签: lisp

解决方案


您正在L从中返回,这是从中弹出元素dotimes后的剩余列表。number

如果只想获取元素,则应返回Lusing的第一个元素car

(defun element-i (L number)
  (cond ((null L) nil)
        ((< (length L) number) nil)
        (t (dotimes (i number (car L))
             (pop L)))))

我还建议cond在有多个条件要测试时使用,而不是嵌套if. 并null在测试列表时使用谓词;not应该用于逻辑上下文。


推荐阅读