Sysop: | Amessyroom |
---|---|
Location: | Fayetteville, NC |
Users: | 40 |
Nodes: | 6 (0 / 6) |
Uptime: | 11:03:35 |
Calls: | 291 |
Files: | 910 |
Messages: | 76,430 |
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)
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)]