I don't need a general purpose transformation, just some guidelines to follow when I see code like this.
General guideline: Look for a solution that uses LOOP. ;)
(defun diff3 (list)
(let ((avg (loop for element in list
sum element into sum
count t into length
finally (return (/ sum length)))))
(loop for element in list
collect (- element avg))))
I don't need a general purpose transformation, just some guidelines to follow when I see code like this.
General guideline: Look for a solution that uses LOOP. ;)
(defun diff3 (list)
(let ((avg (loop for element in list
sum element into sum
count t into length
finally (return (/ sum length)))))
(loop for element in list
collect (- element avg))))
Pascal Costanza wrote:
I don't need a general purpose transformation, just some guidelines to
follow when I see code like this.
General guideline: Look for a solution that uses LOOP. ;)
(defun diff3 (list)
(let ((avg (loop for element in list
sum element into sum
count t into length
finally (return (/ sum length)))))
(loop for element in list
collect (- element avg))))
Gauche Scheme
(define (diff3 lst)
(let ((len 0) (sum 0))
(dolist (x lst) (inc! len) (inc! sum x))
(map (cute - <> (/ sum len)) lst)))
[[callf mapcar [chain [callf / sum len] (do op - @@1)] identity] '(12 3 4 5)]
Pascal Costanza wrote:
I don't need a general purpose transformation, just some guidelines to follow when I see code like this.
General guideline: Look for a solution that uses LOOP. ;)
(defun diff3 (list)
(let ((avg (loop for element in list
sum element into sum
count t into length
finally (return (/ sum length)))))
(loop for element in list
collect (- element avg))))
Gauche Scheme
(define (diff3 lst)
(let ((len 0) (sum 0))
(dolist (x lst) (inc! len) (inc! sum x))
(map (cute - <> (/ sum len)) lst)))
(diff3 '(1 2 3 4 5))
===>
(-2 -1 0 1 2)
Sysop: | Keyop |
---|---|
Location: | Huddersfield, West Yorkshire, UK |
Users: | 388 |
Nodes: | 16 (2 / 14) |
Uptime: | 09:50:04 |
Calls: | 8,221 |
Files: | 13,122 |
Messages: | 5,872,631 |