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

Liste des GroupesRevenir à cl lisp 
Sujet : Re: walk through list and add all n'th item
De : Nobody447095 (at) *nospam* here-nor-there.org (B. Pym)
Groupes : comp.lang.lisp
Date : 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)
 


Date Sujet#  Auteur
19 Jul 24 * Re: walk through list and add all n'th item3B. Pym
19 Jul 24 +- Re: walk through list and add all n'th item1Kaz Kylheku
15 Aug 24 `- Re: walk through list and add all n'th item1B. Pym

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal