我是 lisp 语言的新手。仅仅 3 天。 我有一项任务,在编写代码方面需要帮助。
编写一个接受三个参数的 Lisp 函数:一个称为 aList 的整数列表、一个称为 min 的整数和一个称为 max 的整数。该函数返回 aList 中落在 min <= x <=max 范围内的元素数,其中 x 是 aList 的元素。如果 aList 为空,则该函数应返回 0。
这样做正确吗?
(defun countem(aList min max)
(if (> min max)
(cons min (aList (add1 min) max))))
最佳答案
- 安装 sbcl
- 在 REPL 中输入您的代码并试一试
引用代码查找 aList 中落在 min <= x <=max 范围内的元素数:
(defun countem-1 (aList min max)
(let ((count 0))
(dolist (el aList)
(if (and (>= el min) (<= el max))
(setf count (1+ count))))
(print count)))
或者你的意思是递归方式:
(defvar count 0)
(defun countem-2 (aList min max)
(if (<= min max)
(progn
(setf count (1+ count))
(countem-2 aList (1+ min) max))
(print count))
关于lisp - 通用 Lisp 编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23282637/