保持格式与语言的预期约定一致很重要。它有助于阅读代码(尤其是与其他程序员一起阅读),并且可以帮助您查看错误。
此外,您应该使用至少可以跟踪括号的编辑器。在 Emacs 中,当您将光标放在第一个左括号中时,匹配的括号会突出显示。您会发现您多了一个无用的括号。
(
defun OccurencesOfPrimes (list)
(loop for i from 2 to 100
do ( setq isPrime t)
(loop for j from 2 to i
never (zerop (mod i j))
(setq isPrime f)
(break)
)
)
(if (setq isPrime t)
(append list i)
)
) ;; <- end of defun
) ;; <- closes nothing
在 Lisp 中,括号用于计算机,而缩进用于人类。工具可以根据结构(括号)自动缩进代码,并且您期望的缩进与正在计算的缩进之间的任何差异都暗示您的代码格式错误。如果您查看表达式的缩进,您可以看到您在表单中的深度,仅此一项就可以帮助您理解代码。
符号名称是dash-separated,而不是camlCased。
您的代码,附注:
(defun occurences-of-primes (list)
;; You argument is likely to be a LIST, given its name and the way
;; you call APPEND below. But you never iterate over the list. This
;; is suspicious.
(loop
for i from 2 to 100
do
(setq is-prime t) ;; setting an undeclared variable
(loop
for j from 2 to i
never (zerop (mod i j))
;; the following two forms are not expected here according
;; to LOOP's grammar; setting IS-PRIME to F, but F is not
;; an existing variable. If you want to set to false, use
;; NIL instead.
(setq is-prime f)
;; BREAK enters the debugger, maybe you wanted to use
;; LOOP-FINISH instead, but the NEVER clause above should
;; already be enough to exit the loop as soon as its
;; sub-expression evaluates to NIL.
(break)))
;; The return value of (SETQ X V) is V, so here your test would
;; always succeed.
(if (setq is-prime t)
;; Append RETURNS a new list, without modifying its
;; arguments. In particular, LIST is not modified. Note that "I"
;; is unknown at this point, because the bindings effective
;; inside the LOOP are not visible in this scope. Besides, "I"
;; is a number, not a list.
(append list i)))
原问题
编写一个函数来计算(可能是嵌套的)列表中所有素数的出现次数。
尽管作业问题说“编写一个函数”,但并不是说你应该编写一个同时计算所有内容的大函数。你可以写一个这么大的函数,但是如果你把你的问题分解成子问题,你会得到不同的辅助函数,其中:
- 更容易理解(他们只做一件事)
- 可重复用于构建其他功能
子问题是,例如:如何确定一个数是否是素数?如何遍历树(也就是可能的嵌套列表)?如何计算
事件?
基本思想是编写一个“is-prime”函数,遍历树并在每个元素上调用“is-prime”;如果元素是素数并且以前从未见过,则将 1 添加到您的函数本地的计数器。
也可以展平输入树,得到一个列表,然后对结果进行排序
列表;您在跟踪最后一个列表的同时迭代列表
看到的值:如果该值与上一个相同,则您
已经知道这个数是否是素数;如果前面的数字不同,那么
你必须先测试这个数字是否是素数。
您还可以进一步抽象一些东西,并定义一个高阶 tree-walker 函数,该函数在树的每个叶子上调用一个函数。并编写另一个“记忆”调用的高阶函数:它围绕一个
函数 F 以便如果您使用与以前相同的参数调用 F,
它返回存储的结果而不是重新计算它。
示例
我会将上述想法结合起来,因为如果您将答案交给老师,您可能必须仔细解释每个部分的作用(如果可以,对您来说很好);这不一定是“最佳”答案,但它涵盖了很多内容。
(defun tree-walk-leaves (tree function)
(typecase tree
(null nil)
(cons
(tree-walk-leaves (car tree) function)
(tree-walk-leaves (cdr tree) function))
(t (funcall function tree))))
(defun flatten (tree &optional keep-order-p)
(let ((flat nil))
(tree-walk-leaves tree (lambda (leaf) (push leaf flat)))
(if keep-order-p
(nreverse flat)
flat)))
(defun prime-p (n)
(or (= n 2)
(and (> n 2)
(oddp n)
(loop
for d from 3 upto (isqrt n) by 2
never (zerop (mod n d))))))
(defun count-occurences-of-prime (tree)
(count-if #'prime-p (remove-duplicates (flatten tree))))
(count-occurences-of-prime '(((1)(2))(5)(3)((8)3)))
=> 4
相反,如果您不想删除重复项而是计算素数出现的多次,您可以这样做:
(count-if (memoize #'prime-p) (flatten tree))
...memoize 是:
(defun memoize (function &key (test #'equalp) (key #'identity))
(let ((hash (make-hash-table :test test)))
(lambda (&rest args)
(let ((args (funcall key args)))
(multiple-value-bind (result exists-p) (gethash args hash)
(values-list
(if exists-p
result
(setf (gethash args hash)
(multiple-value-list (apply function args))))))))))
(如果没有重复,memoize 是没用的)