Sujet : Re: Translating circular Haskell code to lisp
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lisp comp.lang.schemeDate : 30. Aug 2024, 19:32:35
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240830101143.424@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
On 2024-08-30, B. Pym <
Nobody447095@here-nor-there.org> 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)))
The loop algorithm contains no explicitly visible side effects
whereas yours has ugly variable stepping.
Point-free one-liner:
This is the TXR Lisp interactive listener of TXR 296.
Quit with :quit or Ctrl-D on an empty line. Ctrl-X ? for cheatsheet.
TXR kind of supports IEEE 754.00000000000003 floating-point.
1> [[callf mapcar [chain [callf / sum len] (do op - @@1)] identity] '(1
2 3 4 5)]
(2.0 1.0 0.0 -1.0 -2.0)
-- TXR Programming Language: http://nongnu.org/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca