我有以下 lisp 代码
(defun sum (vec)
"Summiert alle Elemente eines Vektors."
(apply '+ vec))
(defun square (item)
"Hilfsfunktion zum Quadrieren eines Elements."
(* item item))
(defun calcVarianz (vec)
"Berechnet die Varianz eines Vektors."
(loop with len = (length vec)
with mean = (/ (sum vec) len)
with some_func = (lambda (x) (* x x))
; causes the error
for item in vec
collecting (square (- item mean)) into squared
collecting (some_func item) into some_vector
; some_func cannot be found
finally (return (/ (sum squared) (- len 1)))))
效果很好(计算向量的方差)。
现在,我想知道是否可以将
sum
和 square
函数定义为 loop
构造中的 lambda,但一路上陷入困境。例如,这可能吗
with sum = (lambda (x) ...)
出现错误
The function COMMON-LISP-USER::SOME_FUNC is undefined.
[Condition of type UNDEFINED-FUNCTION]
我在这里缺少什么?
您可以使用
apply
,替换
collecting (some_func item) into some_vector
与
collecting (apply some_func (list item)) into some_vector