Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

You will be a winner today. Pick a fight with a four-year-old.


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

SubjectAuthor
* Re: Translating circular Haskell code to lispB. Pym
+- Re: Translating circular Haskell code to lispKaz Kylheku
`- Re: Translating circular Haskell code to lispB. Pym

1
Subject: Re: Translating circular Haskell code to lisp
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 17:09 UTC
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Translating circular Haskell code to lisp
Date: Fri, 30 Aug 2024 17:09:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 25
Message-ID: <vasuce$iv6j$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 30 Aug 2024 19:09:36 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="7471b6bf57a1160866342d87b5967bc4";
logging-data="621779"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18CyvKUQlu++jk+R2t2DB13"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:wuFJuYUAStznLAtZD11xCMhdqRQ=
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)
(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)

Subject: Re: Translating circular Haskell code to lisp
From: Kaz Kylheku
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Fri, 30 Aug 2024 17:32 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: 643-408-1753@kylheku.com (Kaz Kylheku)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Translating circular Haskell code to lisp
Date: Fri, 30 Aug 2024 17:32:35 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 39
Message-ID: <20240830101143.424@kylheku.com>
References: <vasuce$iv6j$1@dont-email.me>
Injection-Date: Fri, 30 Aug 2024 19:32:35 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="cbe2877523f20dda3b599fd18e2277a3";
logging-data="622811"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18H2u0Ai7iApixht+W5HRJ7RhN891Qe82U="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:qgyROFSZOHf3mIYi575DnAp5Euw=
View all headers

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

Subject: Re: Translating circular Haskell code to lisp
From: B. Pym
Newsgroups: comp.lang.lisp, comp.lang.scheme
Organization: A noiseless patient Spider
Date: Sat, 31 Aug 2024 20:32 UTC
References: 1
Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: Nobody447095@here-nor-there.org (B. Pym)
Newsgroups: comp.lang.lisp,comp.lang.scheme
Subject: Re: Translating circular Haskell code to lisp
Date: Sat, 31 Aug 2024 20:32:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 32
Message-ID: <vavukd$150iv$1@dont-email.me>
References: <vasuce$iv6j$1@dont-email.me>
Injection-Date: Sat, 31 Aug 2024 22:32:15 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="becbf8d70a038bf1db72383f6d2892a4";
logging-data="1213023"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18LWF1ToY3pPrXFKvmYygxX"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:eAOzEBUSsVaq/RtYSKfGUlmfsRU=
View all headers

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."

1

rocksolid light 0.9.8
clearnet tor