(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)]
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 461 |
Nodes: | 16 (2 / 14) |
Uptime: | 24:48:46 |
Calls: | 9,360 |
Calls today: | 5 |
Files: | 13,542 |
Messages: | 6,084,203 |
Posted today: | 2 |