Sujet : Re: walk through list and add all n'th item
De : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lispDate : 19. Jul 2024, 18:22:43
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <20240719101449.245@kylheku.com>
References : 1
User-Agent : slrn/pre1.0.4-9 (Linux)
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/txrCygnal: Cygwin Native Application Library: http://kylheku.com/cygnalMastodon: @Kazinator@mstdn.ca