Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

/earth is 98% full ... please delete anyone you can.


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

SubjectAuthor
* .Re: Translating circular Haskell code to lispRobert L.
`- Re: .Re: Translating circular Haskell code to lispPaul Rubin

1
.Re: Translating circular Haskell code to lisp

<t11p1t$8b8$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16977&group=comp.lang.lisp#16977

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!lNWn9cxTR/GpcgzEu9WJbg.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: Translating circular Haskell code to lisp
Date: Fri, 18 Mar 2022 11:05:35 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <t11p1t$8b8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="8552"; posting-host="lNWn9cxTR/GpcgzEu9WJbg.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Fri, 18 Mar 2022 11:05 UTC

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))))

Shorter and Lispier.

Gauche Scheme:

(define (diff lst)
(let* ((len 0)
(sum (fold
(lambda (n s) (set! len (+ 1 len)) (+ n s))
0 lst))
(avg (/ sum len)))
(map (cut - <> avg) lst)))

(diff '(2 3 4))
===>
(-1 0 1)

(diff '(2 4 9))
===>
(-3 -1 4)

Re: .Re: Translating circular Haskell code to lisp

<87ilsb8byw.fsf@nightsong.com>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16979&group=comp.lang.lisp#16979

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!eternal-september.org!reader02.eternal-september.org!.POSTED!not-for-mail
From: no.email@nospam.invalid (Paul Rubin)
Newsgroups: comp.lang.lisp
Subject: Re: .Re: Translating circular Haskell code to lisp
Date: Fri, 18 Mar 2022 09:25:11 -0700
Organization: A noiseless patient Spider
Lines: 30
Message-ID: <87ilsb8byw.fsf@nightsong.com>
References: <t11p1t$8b8$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain
Injection-Info: reader02.eternal-september.org; posting-host="715502d6767f816b066c4422c8c0e06a";
logging-data="6658"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19wV8rTv8xN33fosCtDwGUW"
User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/27.1 (gnu/linux)
Cancel-Lock: sha1:bVM1Gf/yYRjH7OESxoFLCjmH9XA=
sha1:v8XtsyEcIROSl1IgJMi/z8mne0g=
 by: Paul Rubin - Fri, 18 Mar 2022 16:25 UTC

"Robert L." <No_spamming@noWhere_7073.org> writes:

> Gauche Scheme:
> (define (diff lst)
> (let* ((len 0)
> (sum (fold
> (lambda (n s) (set! len (+ 1 len)) (+ n s))
> 0 lst))
> (avg (/ sum len)))
> (map (cut - <> avg) lst)))

Ugh! Just find the sum and the length using two passes over the list.
The Lisp list is already in memory so I'd want to see a benchmark before
believing that two passes are slower in a given app. The reason for the
complicated fold in Haskell is that the list can be lazily generated, so
using a single pass can dispose of the elements once they are summed,
instead of building up a potentially large list in memory.

Anyway, the Scheme version is repulsive even if you want to do just one
pass. Try:

(define (diff lst)
(define (sumlen lst sum len)
(cond ((nil? lst) (list sum len))
(#t (sumlen (cdr lst) (+ sum (car lst)) (1+ len)))))
(let* ((xy (sumlen lst 0 0))
(sum (car xy))
(len (cadr xy))
(avg (/ sum len)))
(map (lambda (a) (- a avg)) lst)))

1
server_pubkey.txt

rocksolid light 0.9.8
clearnet tor