Sujet : Re: applying f to f n times - best argument order?
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 25. Aug 2024, 12:04:49
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vaevk0$1rhmq$1@dont-email.me>
User-Agent : XanaNews/1.18.1.6
Kaz Kylheku wrote:
You want to bounce the arguments down into the recursive call, not a
list of them as one argument, right?
Is this what you are looking for:
(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))
(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