【发布时间】:2013-11-16 16:08:03
【问题描述】:
我想验证这是否是递归函数?需要返回第 n 个元素,我的作品我只是想确定一下。
(defun nth2(n lst)
(let((count 1))
(loop
(cond((equal count n)(return (car lst))))
(setq count (+ count 1))
(setq lst (cdr lst)))))
好的。我只是尝试了这个想法。它给了我一个错误:不在名为 NIL 的块内
(defun nth2(n lst)
(let((count 1))
(cond((equal count n)(return (car lst)))
(t(setq count (+ count 1))
(nth2(count (cdr (lst))))))))
【问题讨论】:
-
不,它不是递归函数。通常,递归函数会调用自己,因此您应该在
nth2的定义中看到nth2。 -
如果我回忆起类似 (n-1) 的函数
标签: recursion common-lisp