Discussion:
applying macros
(too old to reply)
B. Pym
2024-09-19 07:32:45 UTC
Permalink
(defun average-function (list)
(assert list (list)
"List should not be empty.")
(loop for i from 0
for item in list
sum item into result
finally (return (/ result i))))
It's shorter in Gauche Scheme.

(define (average-function lst)
(let1 n 0
(/ (fold (lambda (x sum) (inc! n) (+ x sum)) 0 lst)
n)))
B. Pym
2024-09-19 08:45:16 UTC
Permalink
Post by B. Pym
(defun average-function (list)
(assert list (list)
"List should not be empty.")
(loop for i from 0
for item in list
sum item into result
finally (return (/ result i))))
It's shorter in Gauche Scheme.
(define (average-function lst)
(let1 n 0
(/ (fold (lambda (x sum) (inc! n) (+ x sum)) 0 lst)
n)))
Another way.

(define (average-function lst)
(let1 n 0
(/ (@ fold x sum : (inc! n) (+ x sum) : 0 lst)
n)))

Given:

(define-syntax @-aux
(syntax-rules (:)
;; No parameters given; use "cut".
[(_ func () ((e0 e ...)) : stuff ...)
(func (cut e0 e ...) stuff ...)]
[(_ func vars (e0 e ...) : stuff ...)
(func (lambda vars e0 e ...) stuff ...)]
[(_ func vars (e0 e ...) expr more ...)
(@-aux func vars (e0 e ... expr) more ...)]
[(_ func vars () : expr more ...)
(@-aux func vars (expr) more ...)]
[(_ func (vars ...) () var more ...)
(@-aux func (vars ... var) () more ...)]))

(define-syntax @
(syntax-rules ()
[(_ func stuff ...)
(@-aux func () () stuff ...)]))

Loading...