Deutsch   English   Français   Italiano  
<v9s7vu$2ad0a$1@dont-email.me>

View for Bookmarking (what is this?)
Look up another Usenet article

Path: ...!2.eu.feeder.erje.net!feeder.erje.net!eternal-september.org!feeder3.eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail
From: "B. Pym" <Nobody447095@here-nor-there.org>
Newsgroups: comp.lang.lisp
Subject: Re: summing in a loop and iterate
Date: Sun, 18 Aug 2024 07:31:15 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 94
Message-ID: <v9s7vu$2ad0a$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Sun, 18 Aug 2024 09:31:15 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="ba36be2ef7e015988cbf9a265c1bd11c";
	logging-data="2438154"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19zFKs1nozksEnOAUZZi2In"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:EH2HZINZDIGQRAQqB55xUBdkkvQ=
Bytes: 3915

> > Lieven Marchand wrote:
> > 
> > > Zach KS <elzacho@gmail.com> writes:
> > > 
> > > >> On 2012-06-14 13:38:46 EDT, Francogrex wrote:
> > > >> > ;;Use the iterate package
> > > >> >
> > > >> > (let ((data '(("AB" A 12 6) ("AB" G 13 4) ("AB" A 56 9)
> > > >> > 	      ("AB" A 450 10) ("AB" G 380 5))))
> > > >> >
> > > >> >   (iterate (for i in data)
> > > >> > 	   (cond ((eql (cadr i) 'A)
> > > >> > 		  (sum (caddr i) into V1)
> > > >> > 		  (sum (cadddr i) into V2))
> > > >> > 		 ((eql (cadr i) 'G)
> > > >> > 		  (sum (caddr i) into V3)
> > > >> > 		  (sum (cadddr i) into V4)))
> > > >> > 	   finally (return (list V2 V1 V4 V3))))
> > > >> >
> > > >> > * (6 12 0 0)
> > > >> > Why is it not summing all 'as expected'?
> > > >> > Also the same issue with loop. (code not provided). Am I missing
> > > >> > something trivial here? I would appreciate some help (preferably with
> > > >> > the loop facility instead of iterate.
> > > >> 
> > > > 
> > > > There is an error in your code.  You forgot to wrap
> > > > your finally clause in a set of parentheses.  This should
> > > > give you an error, unless you had previously defined
> > > > "finally", if which case you would return on the first pass
> > > > through the loop, giving you (6 12 0 0).
> > > > 
> > > > Should look like:
> > > > 
> > > > (let ((data '(("AB" A 12 6) ("AB" G 13 4) ("AB" A 56 9)
> > > >               ("AB" A 450 10) ("AB" G 380 5))))
> > > >   (iterate (for i in data)
> > > >     (cond ((eql (cadr i) 'A)
> > > >            (sum (caddr i) into V1)
> > > >            (sum (cadddr i) into V2))
> > > >           ((eql (cadr i) 'G)
> > > >            (sum (caddr i) into V3)
> > > >            (sum (cadddr i) into V4)))
> > > >     (finally (return (list V2 V1 V4 V3)))))
> > > > 
> > > > ...but you should also be using first, second, third, fourth like
> > > > Barry implicitly suggests as well.
> > > 
> > > Or use destructuring.
> > > 
> > > (let ((data '(("AB" A 12 6) ("AB" G 13 4) ("AB" A 56 9)
> > >               ("AB" A 450 10) ("AB" G 380 5))))
> > >   (loop for (s label i j) in data
> > >         when (eq label 'A)
> > >         sum i into v1 
> > >         and
> > >         sum j into v2
> > >         when (eq label 'G)
> > >         sum i into v3 
> > >         and
> > >         sum j into v4
> > >         finally (return (list v1 v2 v3 v4))))

Gauche Scheme

(use util.match) ;; match-lambda

(define data '(("AB" A 12 6) ("AB" G 13 4) ("AB" A 56 9)
               ("AB" A 450 10) ("AB" G 380 5)))

(apply map +
  (map
    (match-lambda [(_ 'A m n) (list m n 0 0)]
                  [(_ 'G m n) (list 0 0 m n)])
    data))

(518 25 393 9)



newLISP

(define data '(("AB" A 12 6) ("AB" G 13 4) ("AB" A 56 9)
               ("AB" A 450 10) ("AB" G 380 5)))

(apply map
  (cons +
    (map
      (fn (x)
        (if   (match '("AB" A ? ?) x) (append $it '(0 0))
          (if (match '("AB" G ? ?) x) (append '(0 0) $it))))
      data)))

(518 25 393 9)