common lisp - Lispy way of running a function multiple times -
i using function d generate random numbers collect in list , average them:
(/ (apply #'+ (list (d 6) (d 6) (d 6) (d 6) (d 6) (d 6))) 6.0) i run function (d n) i times, add returned values together, , divide i. dotimes not return value. how in common lisp?
(defun r (n f arg) "calls function f n times arg. returns arithmetic mean of results." (/ (loop repeat n sum (funcall f arg)) n)) (r 6 #'d 6) dotimes not return value.
it does:
cl-user 21 > (let ((sum 0)) (dotimes (i 10 sum) ; <- sum return value (incf sum (random 10)))) 45
Comments
Post a Comment