Discussion:
Finding Average without using Recusrion only using Prog
(too old to reply)
B. Pym
2024-06-16 04:39:19 UTC
Permalink
(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
HenHanna
2024-06-16 18:28:23 UTC
Permalink
Post 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
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 '())
HenHanna
2024-06-16 19:48:24 UTC
Permalink
Post by HenHanna
Post 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  '())
(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))
Kaz Kylheku
2024-06-17 00:09:20 UTC
Permalink
Post by HenHanna
Post 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
steve g
2024-08-09 21:45:42 UTC
Permalink
"B. Pym" <***@noWhere_7073.org> writes:

< > (defun avg (args)
< > (loop for x in args
< > for l upfrom 1
< > summing x into tot
< > finally (return (/ tot l))))
Post by B. Pym
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)
summing x into max
counting x into cnt
finally (pprint (/ max cnt)))
Kaz Kylheku
2024-08-09 23:30:54 UTC
Permalink
Post by steve g
< > (defun avg (args)
< > (loop for x in args
< > for l upfrom 1
< > summing x into tot
< > finally (return (/ tot l))))
Post by B. Pym
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)
summing x into max
counting x into cnt
finally (pprint (/ max cnt)))
2> [[callf / sum len] '(1 2 3 4 5)]
3.0

steve g
2024-08-09 21:51:54 UTC
Permalink
"B. Pym" <***@noWhere_7073.org> writes:

< > (defun avg (args)
< > (loop for x in args
< > for l upfrom 1
< > summing x into tot
< > finally (return (/ tot l))))
Post by B. Pym
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
(defun avg-with-prog (lst)
(prog ((avg 0) (cnt 0))
START
(setq avg (+ avg (pop lst)))
(incf cnt)
(if (endp lst)
(GO END)
(GO START))
END
(print (/ avg cnt))))
Loading...