Sujet : Re: walk through list and add all n'th item
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lispDate : 15. Aug 2024, 01:38:04
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <v9jf4q$l4up$1@dont-email.me>
References : 1
User-Agent : XanaNews/1.18.1.6
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)