• Re: applying f to f n times - best argument order?

    From B. Pym@21:1/5 to Kaz Kylheku on Sun Aug 25 10:04:49 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Jeff Barnett@21:1/5 to B. Pym on Sun Aug 25 12:50:21 2024
    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

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)
  • From Madhu@21:1/5 to All on Mon Aug 26 10:34:31 2024
    memo: there's no glory in trolling WJ

    * Jeff Barnett <vafudd$220kj$1@dont-email.me> :
    Wrote on Sun, 25 Aug 2024 12:50:21 -0600:

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)