【发布时间】:2014-12-11 05:02:47
【问题描述】:
我遇到了一些 lisp 代码的问题。这个函数只是应该反转一个基本列表。 我只能使用原语,它们被定义为 " defun, cond, cons, car, cdr, null, eq, listp, atom, symbolp, +, - , "
在传递的例子中 (1 2 3 4) 我回来了 (((4 3) 2) 1)
(defun reverse2 (l)
(cond
((eq nil (cdr l)) (car l) )
(t (cons(reverse2 (cdr l)) (cons (car l) nil)))))
请告诉我如何改进。这不是家庭作业,我只是为了明天的期末考试而做这个练习。
【问题讨论】:
-
--不是 Common Lisp 中的运算符。你的意思是-? -
如果您可以使用
append而不是cons,那么您的方法将有效;但它不在您允许的原语列表中。