小编use*_*836的帖子

CLISP - 扭转一个简单的列表

我必须反转简单(单维)列表的元素.我知道有一个内置的反向功能,但我不能用它.

这是我的尝试:

(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)

有帮助吗?

lisp clisp common-lisp

2
推荐指数
1
解决办法
2363
查看次数

标签 统计

clisp ×1

common-lisp ×1

lisp ×1