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 : 643-408-1753 (at) *nospam* kylheku.com (Kaz Kylheku)
Groupes : comp.lang.lisp
Date : 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/txr
Cygnal: Cygwin Native Application Library: http://kylheku.com/cygnal
Mastodon: @Kazinator@mstdn.ca

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