【发布时间】:2014-11-02 22:31:46
【问题描述】:
定义函数 'occ',它接受一个列表 L 和一个符号 A,并计算符号 A 在 L 中的出现次数。 例子: (occ '(((s) o ) d) 'f) --> 0
到目前为止我得到了什么:
(defun occ(list a)
(setq counter 0)
;Checks if the given list is has an nested list
(if (consp list)
; Breaking the list down atom by atom and recursing
(or (occ a (car list))
(occ a (cdr list)))
; checks if symbols are the same
(if(eq a list)
(setq counter(1+ counter)))))
但是我的输出一直说 Nil 而不是显示计数器值。 我不能使用 LISP 的任何高级功能。
【问题讨论】:
-
一些事情:您在每次通话时都将计数器设置为 0。你没有返回一个值。如果第一个调用返回 true,'or' 不会调用第二个参数。
标签: math lisp common-lisp