我的小组和我正在尝试创建一份缺点列表。它可能看起来像这样
((2 100032) (4 32413) (6 2131251) ... (232 12))
不幸的是,我们不断收到#34;错误的类型参数"。这可能是一件简单的事情,但我也想知道我们是否在做缺点。
任何回复都表示赞赏:
(defun find-diffs ()
(let ((diffs (list))
(index 0)
(previous 0))
(while (< index (length first-ten-million-primes))
; Add the difference to the list of diffs.
(setq diff (- (aref first-ten-million-primes index) previous))
; We only want to bother recording it if the index is above zero and
; the difference is odd.
(if (and (> index 0) (evenp diff))
(setq diffs
; Can we find this one in our list of diffs?
(if (cdr (assoc diff diffs))
; Yes
; ERROR happens when we call this statement
(setq diffs
(append (cons diff (1+ (car (cdr (assq diff diffs)))))
(assq-delete-all diff diffs)))
; No
(setq diffs (plist-put diffs diff 1)))))
; Set previous value to this one.
(setq previous (aref first-ten-million-primes index))
; Increment the index.
(setq index (1+ index)))
diffs)
评论(setq diffs)将解决它,但我没有看到我们如何设置变量有什么问题。
谢谢!
回溯很大,所以我只发布第一部分。我的团队仍然对于什么是错误感到困惑,所以任何回复都会受到赞赏。
调试器Backtrace:
Debugger entered--Lisp error: (wrong-type-argument listp 1)
append((2 . 1) nil)
(setq diffs (append (cons diff 1) diffs))
(if (cdr (assoc diff diffs)) (setq diffs (append (cons diff (1+ (car (cdr (assq diff diffs)))))
答案 0 :(得分:1)
您的问题出在:
(append (cons diff (1+ (car (cdr (assq diff diffs)))))
(assq-delete-all diff diffs)))
cons
向上创建(DIFF . N)
形式的单元格,该单元格不是正确的列表。它是一对两个元素而不是两个元素的列表。两个元素的列表的格式为(DIFF N)
,这是(DIFF . (N . nil))
的简写。
我没有尝试了解您的总体目标是什么,但有两种方法可以解决您的问题:要么替换
(append (cons diff FOO)
...)
与
(append (list diff FOO)
...)
或
(cons (cons diff FOO)
...)