• Re: walk through list and add all n'th item

From B. Pym@21:1/5 to All on Fri Jul 19 03:17:36 2024
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)

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)
• From Kaz Kylheku@21:1/5 to B. Pym on Fri Jul 19 17:22:43 2024
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.

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

Or, if we want to exclude the short tuple:

(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

--- SoupGate-Win32 v1.05
* Origin: fsxNet Usenet Gateway (21:1/5)