Sujet : Re: Finding Average without using Recusrion only using Prog
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lispDate : 17. Jun 2024, 02:09:20
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240616170235.690@kylheku.com>
References : 1 2
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-06-16, HenHanna <
HenHanna@devnull.tb> wrote:
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 '())
This is the TXR Lisp interactive listener of TXR 294.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
Evidence of amphoric Lisp macros was recently found in ancient clay
jars.
1> [[callf / sum len] '(1 2 3 4)]
2.5
callf: return a function which:
- applies the second and subsequent functions given to callf (here sum
and len) to its arguments.
- applies the first argument of callf (here /) to the resulting
values.
- returns the resulting value
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca