use*_*836 2 lisp clisp common-lisp
我必须反转简单(单维)列表的元素.我知道有一个内置的反向功能,但我不能用它.
这是我的尝试:
(defun LISTREVERSE (LISTR)
(cond
((< (length LISTR) 2) LISTR) ; listr is 1 atom or smaller
(t (cons (LISTREVERSE (cdr LISTR)) (car LISTR))) ; move first to the end
)
)
Run Code Online (Sandbox Code Playgroud)
输出非常接近,但是错了.
[88]> (LISTREVERSE '(0 1 2 3))
((((3) . 2) . 1) . 0)
Run Code Online (Sandbox Code Playgroud)
所以我尝试使用append而不是cons:
(t (append (LISTREVERSE (cdr LISTR)) (car LISTR)))
Run Code Online (Sandbox Code Playgroud)
但得到了这个错误:
*** - APPEND: A proper list must not end with 2
Run Code Online (Sandbox Code Playgroud)
有帮助吗?
我可以给你一些指示,因为这看起来像家庭作业:
cons它位于累加器的开头.当输入列表为空时,返回累加器另外,上述解决方案是尾递归的.