运行 一个函数多次的 Lispy 方式
Lispy way of running a function multiple times
我正在使用函数 d
生成随机数,我将这些随机数收集在一个列表中,然后对它们进行平均:
(/ (apply #'+ (list (d 6) (d 6) (d 6) (d 6) (d 6) (d 6))) 6.0)
我想 运行 函数 (d n)
i
次,将 returned 值加在一起,然后除以 i
。 dotimes
没有 return 值。我将如何在 Common Lisp 中执行此操作?
IRC 上一个非常善良的人给了我这个解决方案:
(loop repeat 6 collect (d 6))
(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)
dotimes does not return a value.
确实如此:
CL-USER 21 > (let ((sum 0))
(dotimes (i 10 sum) ; <- sum is the return value
(incf sum (random 10))))
45
我正在使用函数 d
生成随机数,我将这些随机数收集在一个列表中,然后对它们进行平均:
(/ (apply #'+ (list (d 6) (d 6) (d 6) (d 6) (d 6) (d 6))) 6.0)
我想 运行 函数 (d n)
i
次,将 returned 值加在一起,然后除以 i
。 dotimes
没有 return 值。我将如何在 Common Lisp 中执行此操作?
IRC 上一个非常善良的人给了我这个解决方案:
(loop repeat 6 collect (d 6))
(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)
dotimes does not return a value.
确实如此:
CL-USER 21 > (let ((sum 0))
(dotimes (i 10 sum) ; <- sum is the return value
(incf sum (random 10))))
45