Re: Translating circular Haskell code to lisp

Liste des GroupesRevenir à cl scheme 
Sujet : Re: Translating circular Haskell code to lisp
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lisp comp.lang.scheme
Date : 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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Date Sujet#  Auteur
30 Aug 24 * Re: Translating circular Haskell code to lisp3B. Pym
30 Aug 24 +- Re: Translating circular Haskell code to lisp1Kaz Kylheku
31 Aug 24 `- Re: Translating circular Haskell code to lisp1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal