Common lisp 多利斯估计误差
我是个有问题的普通人。我有以下两个功能 辅助函数:Common lisp 多利斯估计误差,common-lisp,Common Lisp,我是个有问题的普通人。我有以下两个功能 辅助函数: (defun make-rests (positions rhythm) "now make those positions negative numbers for rests" (let ((resultant-rhythm rhythm)) (dolist (i positions resultant-rhythm) (setf (nth i resultant-rhythm) (* (nth i result
(defun make-rests (positions rhythm)
"now make those positions negative numbers for rests"
(let ((resultant-rhythm rhythm))
(dolist (i positions resultant-rhythm)
(setf (nth i resultant-rhythm) (* (nth i resultant-rhythm) -1)))))
(defun test-return-rhythms (rhythms)
(let ((positions '((0 1) (0)))
(result nil))
(dolist (x positions (reverse result))
(push (make-rests x rhythms) result))))
和一个主要功能:
(defun make-rests (positions rhythm)
"now make those positions negative numbers for rests"
(let ((resultant-rhythm rhythm))
(dolist (i positions resultant-rhythm)
(setf (nth i resultant-rhythm) (* (nth i resultant-rhythm) -1)))))
(defun test-return-rhythms (rhythms)
(let ((positions '((0 1) (0)))
(result nil))
(dolist (x positions (reverse result))
(push (make-rests x rhythms) result))))
当我运行(测试返回节奏(1/4 1/8))
时,它的计算结果是:((1/4-1/8)(1/4-1/8))
然而,我期望:(测试返回节奏(1/41/8))
评估为:(-1/4-1/8)(-1/41/8))
我做错了什么?您的
make rest
实现具有破坏性
CL-USER> (defparameter *rhythm* '(1/4 1/4 1/4 1/4))
*RHYTHM*
CL-USER> (make-rests '(0 2) *rhythm*)
(-1/4 1/4 -1/4 1/4)
CL-USER> *rhythm*
(-1/4 1/4 -1/4 1/4)
因此,如果您运行测试,第二次迭代将看到(-1/4-1/8)
,并且(makerest'(0)-(1/4-1/8))
返回(1/4-1/8)
。在make rest
中使用let
不会复制列表,它只是创建一个引用列表的新绑定。在中使用复制列表
,或者首先编写一个非破坏性版本:
(defun make-rests (positions rhythm)
(loop for note in rhythm
for i from 0
collect (if (member i positions) (* note -1) note)))
啊,请注意,这个函数最好命名为toggle rests
。