Lispy多次运行函数的方式

And*_*nir 0 common-lisp

我正在使用一个函数d来生成随机数,我将其收集在列表中,然后对它们求平均值:

 (/ (apply #'+ (list (d 6) (d 6) (d 6) (d 6) (d 6) (d 6))) 6.0)
Run Code Online (Sandbox Code Playgroud)

我想运行函数(d n) i次数,将返回的值一起添加,然后除以i.dotimes不返回值.我将如何在Common Lisp中执行此操作?

Rai*_*wig 8

(defun r (n f arg)
  "Calls the function F N times with ARG. Returns
the arithmetic mean of the results."
  (/ (loop repeat n sum (funcall f arg))
     n))

(r 6 #'d 6)
Run Code Online (Sandbox Code Playgroud)

dotimes不返回值.

它确实:

CL-USER 21 > (let ((sum 0))
               (dotimes (i 10 sum)        ; <- sum is the return value
                 (incf sum (random 10))))
45
Run Code Online (Sandbox Code Playgroud)