Sujet : Re: Translating circular Haskell code to lisp
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp comp.lang.schemeDate : 31. Aug 2024, 22:32:15
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vavukd$150iv$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
B. Pym wrote:
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)
Since it's "cute" instead of "cut", (/ sum len) is done only once.
"Cute is a variation of cut that evaluates expr-or-slots before
creating the procedure."