Rocksolid Light

News from da outaworlds

mail  files  register  groups  login

Message-ID:  

BOFH excuse #208: Your mail is being routed through Germany ... and they're censoring us.


comp / comp.lang.lisp / Re: walk through list and add all n'th item

SubjectAuthor
* Re: walk through list and add all n'th itemB. Pym
+- Re: walk through list and add all n'th itemKaz Kylheku
`- Re: walk through list and add all n'th itemB. Pym

1
Subject: Re: walk through list and add all n'th item
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 19 Jul 2024 03:17 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
Subject: Re: walk through list and add all n'th item
Date: Fri, 19 Jul 2024 03:17:36 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 24
Message-ID: <v7clse$2qjma$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Fri, 19 Jul 2024 05:17:36 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="364020230052eda8729452d34971b1ed";
logging-data="2969290"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19Q7H90CpmAPiYpWx4cZvYu"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:DcLABNs9PnJenGK1ESH+JWmFq/8=
View all headers

> Wow: loop macro rulez, somehow..
> just one correction:
> (loop for (a b c d) on (list 1 2 3 4 5 6 7 8) by #'cddddr
> sum a into a-sum
> sum b into b-sum
> sum c into c-sum
> sum d into d-sum
> finally (return (list a-sum b-sum c-sum d-sum)))
> Yeah, i might consider this code as beautiful
> olivier

Gauche Scheme

(use gauche.lazy)
(use util.match)

(define data (lrange 1 57))

(match (lslices data 4)
[((a b c d) ...)
(map (cut fold + 0 <>)
(list a b c d))])

(378 392 406 420)

Subject: Re: walk through list and add all n'th item
From: Kaz Kylheku
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Fri, 19 Jul 2024 17:22 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
Subject: Re: walk through list and add all n'th item
Date: Fri, 19 Jul 2024 17:22:43 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 42
Message-ID: <20240719101449.245@kylheku.com>
References: <v7clse$2qjma$1@dont-email.me>
Injection-Date: Fri, 19 Jul 2024 19:22:43 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="329fa895c5d6908477a80b789f62a073";
logging-data="3237795"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX18hvO7MGLDOp+L2Z+CYBHl+Fa80JLEU65o="
User-Agent: slrn/pre1.0.4-9 (Linux)
Cancel-Lock: sha1:qPHcJi+gb+ZMo71s1f9uaYgodyE=
View all headers

On 2024-07-19, B. Pym <Nobody447095@here-nor-there.org> wrote:
>> Wow: loop macro rulez, somehow..
>> just one correction:
>> (loop for (a b c d) on (list 1 2 3 4 5 6 7 8) by #'cddddr
>> sum a into a-sum
>> sum b into b-sum
>> sum c into c-sum
>> sum d into d-sum
>> finally (return (list a-sum b-sum c-sum d-sum)))
>> Yeah, i might consider this code as beautiful
>> olivier
>
> Gauche Scheme
>
> (use gauche.lazy)
> (use util.match)
>
> (define data (lrange 1 57))
>
> (match (lslices data 4)
> [((a b c d) ...)
> (map (cut fold + 0 <>)
> (list a b c d))])
>
> (378 392 406 420)

Wrong answer: you silently cut off 57 because it wasn't followed by
three more numbers, resulting in 378 rather than 435.

1> (flow 1..58 (tuples 4 @1 0) transpose (mapcar sum))
(435 392 406 420)

Or, if we want to exclude the short tuple:

2> (flow 1..58 (tuples 4) (remove-if (lopip len (< 4))) transpose (mapcar sum))
(378 392 406 420)

--
TXR Programming Language: http://nongnu.org/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

Subject: Re: walk through list and add all n'th item
From: B. Pym
Newsgroups: comp.lang.lisp
Organization: A noiseless patient Spider
Date: Wed, 14 Aug 2024 23:38 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
Subject: Re: walk through list and add all n'th item
Date: Wed, 14 Aug 2024 23:38:04 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 51
Message-ID: <v9jf4q$l4up$1@dont-email.me>
References: <v7clse$2qjma$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Thu, 15 Aug 2024 01:38:04 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="8dd797abfa9c4845a90c0b3910c2697b";
logging-data="693209"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+enmGnU0bFtZ939ItJCZk6"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:8OxMRFc1wdn+2XQ0oXcoaW9Dp44=
View all headers

B. Pym wrote:

> > Wow: loop macro rulez, somehow..
> > just one correction:
> > (loop for (a b c d) on (list 1 2 3 4 5 6 7 8) by #'cddddr
> > sum a into a-sum
> > sum b into b-sum
> > sum c into c-sum
> > sum d into d-sum
> > finally (return (list a-sum b-sum c-sum d-sum)))
> > Yeah, i might consider this code as beautiful
> > olivier
>
> Gauche Scheme
>
> (use gauche.lazy)
> (use util.match)
>
> (define data (lrange 1 57))
>
> (match (lslices data 4)
> [((a b c d) ...)
> (map (cut fold + 0 <>)
> (list a b c d))])
>
> (378 392 406 420)

newLISP

(define data (sequence 1 56))

(map (curry apply +) (transpose (explode data 4)))

===>
(378 392 406 420)

Another way.

;; Using "apply" for "reduce". The 3rd argument tells
;; apply how many items to process at a time.
(apply
(fn (sums) (map + sums $args))
(cons '(0 0 0 0) data)
5)

--->
(378 392 406 420)

1

rocksolid light 0.9.8
clearnet tor