带有 with 的 Lambda 函数

问题描述 投票:0回答:1

我有以下 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]

我在这里缺少什么?

lisp common-lisp
1个回答
0
投票

您可以使用

apply
,替换

collecting (some_func item) into some_vector

collecting (apply some_func (list item)) into some_vector
© www.soinside.com 2019 - 2024. All rights reserved.