Deutsch English Français Italiano |
<v4nas9$5tka$1@dont-email.me> View for Bookmarking (what is this?) Look up another Usenet article |
Path: ...!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: HenHanna <HenHanna@devnull.tb> Newsgroups: comp.lang.lisp Subject: Re: Finding Average without using Recusrion only using Prog Date: Sun, 16 Jun 2024 11:28:23 -0700 Organization: A noiseless patient Spider Lines: 39 Message-ID: <v4nas9$5tka$1@dont-email.me> References: <v4lq9k$3t78r$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sun, 16 Jun 2024 20:28:26 +0200 (CEST) Injection-Info: dont-email.me; posting-host="15d315eb6699321768c126a81b6bef90"; logging-data="194186"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+u5dCV9O9OSYBSb33O26ou9riPMbQPcNQ=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:V6cMatV4ptc7ves7tR9Ht36qVwc= Content-Language: en-US In-Reply-To: <v4lq9k$3t78r$1@dont-email.me> Bytes: 1800 On 6/15/2024 9:39 PM, B. Pym wrote: >> (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 Nice... Here's a more boring version: (define (ave x) (let ((L (length x))) (if (> L 0) (/ (fold + 0 x) L)))) (define (Pave x) (format #t "~% ~S ~S ~%" x (ave x))) (Pave '(1 2 3)) (Pave '(1 2 3 4)) (Pave '(1)) (Pave '())