• #### Re: Finding Average without using Recusrion only using Prog

From B. Pym@21:1/5 to All on Sun Jun 16 04:39:19 2024
(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
0 0
nums))))

(avg '(20 30 40 50 60 70 80))
===>
50

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to B. Pym on Sun Jun 16 11:28:23 2024
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
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 '())

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From HenHanna@21:1/5 to HenHanna on Sun Jun 16 12:48:24 2024
On 6/16/2024 11:28 AM, HenHanna 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
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  '())

(define-macro (ave x)
`(/ (+ ,@ (map (lambda (n) `(+ ,@ (make-list n 1))) (cadr x)))
(+ ,@ (map (lambda (n) 1) (cadr x)))))

(print (ave '(1)))
(print (ave '(1 2 3)))

gosh> (macroexpand '(ave '(1 2 3)))
==> (/ (+ (+ 1) (+ 1 1) (+ 1 1 1)) (+ 1 1 1))

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to HenHanna on Mon Jun 17 00:09:20 2024
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
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.
[[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: @Kazinator@mstdn.ca

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to steve g on Fri Aug 9 23:30:54 2024
On 2024-08-09, steve g <sgonedes1977@gmail.com> wrote:
"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
0 0
nums))))

(avg '(20 30 40 50 60 70 80))
===>
50

(loop for x in '(1 2 3 4 5)
summing x into max
counting x into cnt
finally (pprint (/ max cnt)))

[[callf / sum len] '(1 2 3 4 5)]
3.0

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)