• Re: Finding Average without using Recusrion only using Prog

    From Kaz Kylheku@21:1/5 to steve g on Fri Aug 9 23:30:54 2024
    On 2024-08-09, steve g <sgonedes1977@gmail.com> wrote:
    "B. Pym" <No_spamming@noWhere_7073.org> writes:

    < > (defun avg (args)
    < > (loop for x in args
    < > for l upfrom 1
    < > summing x into tot
    < > finally (return (/ tot l))))





    Gauche Scheme

    (use gauche.collection) ;; fold2

    (define (add&count n sum cnt) (values (+ sum n) (+ cnt 1)))

    (define (avg nums)
    (apply /
    (values->list
    (fold2
    add&count
    0 0
    nums))))

    (avg '(20 30 40 50 60 70 80))
    ===>
    50


    (loop for x in '(1 2 3 4 5)
    summing x into max
    counting x into cnt
    finally (pprint (/ max cnt)))

    [[callf / sum len] '(1 2 3 4 5)]
    3.0

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