(defun avg (args)
(loop for x in args
for l upfrom 1
summing x into tot
finally (return (/ tot l))))
(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
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 '())
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 '())
[[callf / sum len] '(1 2 3 4)]2.5
"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)3.0
summing x into max
counting x into cnt
finally (pprint (/ max cnt)))
[[callf / sum len] '(1 2 3 4 5)]
(defun avg (args)
(loop for x in args
for l upfrom 1
summing x into tot
finally (return (/ tot l))))
(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))Jeff Barnett
2
(avg '(2 3 4))
3--
| Sysop: | Keyop |
|---|---|
| Location: | Huddersfield, West Yorkshire, UK |
| Users: | 715 |
| Nodes: | 16 (2 / 14) |
| Uptime: | 36:20:57 |
| Calls: | 12,109 |
| Files: | 15,006 |
| Messages: | 6,518,360 |