lisp - 显示整个列表的弹出功能
问题描述
我正在尝试通过函数显示列表中的一个值来显示整个列表而不是一个值。下面的代码使用 elisp。
(defun element-i (L number)
(if (not L) nil
(if (< (length L) number) nil
(dotimes (i number L)
(pop L)))))
解决方案
您正在L
从中返回,这是从中弹出元素dotimes
后的剩余列表。number
如果只想获取元素,则应返回L
using的第一个元素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
应该用于逻辑上下文。
推荐阅读
- fetch-api - 使用 Fetch API 和 PHP 文件获取文本响应
- highlight - 突出价值差异
- ios - 无法使用 Delphi 10.4.2 for iOS iPhone APP 更改启动屏幕
- python - 按批次条件计算相关系数
- node.js - 为什么我的第一个查询很慢但是很快?
- python - CNN-LSTM 错误初始化时间分布
- json - React - JSON,有条件的
- javascript - 为什么我的变量在使用 .toString() 后返回 NaN
- reactjs - 状态不更新 React Hook
- java - 我无法使用 Android 的 PdfDocument 创建两页 pdf