• Re: Finding Average without using Recusrion only using Prog

    From B. Pym@Nobody447095@here-nor-there.org to comp.lang.lisp on Thu Aug 7 17:21:48 2025
    From Newsgroup: comp.lang.lisp

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

    (define (avg xs)
    (do ((sum 0 (+ (pop! xs) sum))
    (i 0 (+ 1 i)))
    ((null? xs) (/ sum (max 1 i)))))

    (avg '())
    0
    (avg '(2))
    2
    (avg '(2 3 4))
    3
    --
    [T]he problem is that lispniks are as cultish as any other devout group and basically fall down frothing at the mouth if they see [heterodoxy].
    --- Kenny Tilton
    The good news is, it's not Lisp that sucks, but Common Lisp. --- Paul Graham --- Synchronet 3.21a-Linux NewsLink 1.2
  • From Jeff Barnett@jbb@notatt.com to comp.lang.lisp on Thu Aug 7 17:30:52 2025
    From Newsgroup: comp.lang.lisp

    On 8/7/2025 11:21 AM, B. Pym wrote:
    (defun avg (args)
    (loop for x in args
    for l upfrom 1
    summing x into tot
    finally (return (/ tot l))))

    (define (avg xs)
    (do ((sum 0 (+ (pop! xs) sum))
    (i 0 (+ 1 i)))
    ((null? xs) (/ sum (max 1 i)))))

    (avg '())
    0

    That, of course is an error. Return nil or signalling the condition
    would be more to the point. This looks like a COBOL implementation.

    (avg '(2))
    2
    (avg '(2 3 4))
    3--
    Jeff Barnett

    --- Synchronet 3.21a-Linux NewsLink 1.2