Sujet : Re: applying f to f n times - best argument order?
De : jbb (at) *nospam* notatt.com (Jeff Barnett)
Groupes : comp.lang.lispDate : 25. Aug 2024, 20:50:21
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vafudd$220kj$1@dont-email.me>
References : 1
User-Agent : Mozilla Thunderbird
On 8/25/2024 4:04 AM, B. Pym wrote:
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))
Isn't this where you usually jump in and loudly proclaim "SHORTER!"? You might as well repeat it hear since you are not shorter a significant amount of the time.
Off topic question: Any job offers recently?
;; 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
-- Jeff Barnett