Post by HenHannaPost by B. Pym(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
(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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @***@mstdn.ca