B. Pym
2024-08-25 10:04:49 UTC
You want to bounce the arguments down into the recursive call, not a
list of them as one argument, right?
(defun ncall (f n &rest data)
(loop repeat n
for result = (apply f data) then (funcall f result)
finally (return result)))
(define (feedback func init bottom-i :optional (top-i #f))list of them as one argument, right?
(defun ncall (f n &rest data)
(loop repeat n
for result = (apply f data) then (funcall f result)
finally (return result)))
(let ((result init))
(if top-i
(do ((i bottom-i (+ 1 i)))
((> i top-i) result)
(set! result (func result i)))
(dotimes (i bottom-i)
(set! result (func result))))
result))
;; factorial
(feedback (lambda (prod i) (* i prod)) 1 1 5)
===>
120
(feedback (lambda (prod) (* 2 prod)) 1 8)
===>
256
;; Newton square root
(let ((x 99.0))
(feedback
(lambda (guess) (/. (+ guess (/. x guess)) 2.0))
1.0 8))
===>
9.9498743710662
(sqrt 99)
===>
9.9498743710662