【发布时间】:2012-10-01 09:27:23
【问题描述】:
当我尝试执行以下递归函数时,我在 clisp 中收到“-程序堆栈溢出”提示,我相信该函数会返回列表中最常见的元素:
(defun greater-member (lst)
(cond ((null (cdr lst))
(cons (car lst) (count-if #'(lambda (x) (eql x (car lst))) lst)))
((>= (count-if #'(lambda (x) (eql x (car lst))) lst)
(count-if #'(lambda (x) (eql x (car (remove (car lst) lst)))) lst))
(greater-member (remove (car (remove (car lst) lst)) lst)))
(t (greater-member (remove (car lst) lst)))))
例如 greater-number 应该返回如下:
>(greater-number '(a a a b b b b c))
(b . 4)
请问,是什么原因导致溢出?我已经摆脱了所有的小语法错误 通过在 clisp 中重复执行 greater-number,该函数似乎在逻辑上成立。
【问题讨论】:
-
为什么不使用 TRACE、STEP 或打印参数来调试函数?
-
其实我没听说过这些功能。我会看看他们。
-
这本书解释了基础知识:cs.cmu.edu/~dst/LispBook
-
谢谢 - 我现在已经意识到了这个错误,使用 TRACE。也谢谢你的链接。
-
(count-if #'(lambda (x) (eql x (car lst))) lst) is (count (car lst) lst)
标签: recursion lisp common-lisp stack-overflow clisp