0
我正在学习Common Lisp,并且我一直在尝试编写一个函数来颠倒列表。这是功能,但每次我试图运行它,我得到“段错误(核心转储)”尝试颠倒列表时出现分段错误
(defun reverseList(x)
(if (cdr x)
(cons (reverseList x) (car x))
(car x)))
(reverseList '(1 2 3))
我正在学习Common Lisp,并且我一直在尝试编写一个函数来颠倒列表。这是功能,但每次我试图运行它,我得到“段错误(核心转储)”尝试颠倒列表时出现分段错误
(defun reverseList(x)
(if (cdr x)
(cons (reverseList x) (car x))
(car x)))
(reverseList '(1 2 3))
我认为这会工作:(!原文如此)
(define reverseList(lambda(x)(
if (null? (cdr x))
(cons (car x) '())
(append (reverseList (cdr x)) (cons (car x) '()))
)
)
)
你有一个计算器,因为你的代码无限地调用'reverseList'和相同的列表。它不是终止的。 – 2014-12-07 16:00:21
啊,谢谢。我打算通过它cdr x。 – CountBale 2014-12-07 16:02:45