Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #232: Ionization from the air-conditioning


comp / comp.lang.lisp / Re: Translating circular Haskell code to lisp

SubjectAuthor
o Re: Translating circular Haskell code to lispB. Pym

1
Subject: Re: Translating circular Haskell code to lisp
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Tue, 18 Jun 2024 05:44 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (B. Pym)
Newsgroups: comp.lang.lisp
Subject: Re: Translating circular Haskell code to lisp
Date: Tue, 18 Jun 2024 05:44:10 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 27
Message-ID: <v4r6r9$173na$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Tue, 18 Jun 2024 07:44:11 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="697d3a9c5fdd7f23be611d02815f2a34";
logging-data="1281770"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1/Wlb03kp0ODhz0gLJdxX0w"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:VELecN3NxabqeLXPD7scbWzGluk=
View all headers

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 :optional (seen '()) (sum 0) (cnt 0))
(if (null? lst)
(let1 avg (/ sum cnt)
(map (cut - <> avg) (reverse seen)))
(let1 x (car lst)
(diff3 (cdr lst) (cons x seen) (+ sum x) (+ cnt 1)))))

(diff3 '(1 2 3 4 5))
===>
(-2 -1 0 1 2)

1

rocksolid light 0.9.8
clearnet tor