Rocksolid Light

Welcome to RetroBBS

mail  files  register  newsreader  groups  login

Message-ID:  

Imitation is the sincerest form of plagiarism.


devel / comp.lang.lisp / .Re: summing in a loop and iterate

SubjectAuthor
o .Re: summing in a loop and iterateRobert L.

1
.Re: summing in a loop and iterate

<sus90r$he9$1@gioia.aioe.org>

  copy mid

https://www.rocksolidbbs.com/devel/article-flat.php?id=16905&group=comp.lang.lisp#16905

  copy link   Newsgroups: comp.lang.lisp
Path: i2pn2.org!i2pn.org!aioe.org!eYeqBzNjiJ3ACJwkx5pzXw.user.46.165.242.75.POSTED!not-for-mail
From: No_spamming@noWhere_7073.org (Robert L.)
Newsgroups: comp.lang.lisp
Subject: .Re: summing in a loop and iterate
Date: Sun, 20 Feb 2022 02:28:45 -0000 (UTC)
Organization: Aioe.org NNTP Server
Message-ID: <sus90r$he9$1@gioia.aioe.org>
Mime-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Info: gioia.aioe.org; logging-data="17865"; posting-host="eYeqBzNjiJ3ACJwkx5pzXw.user.gioia.aioe.org"; mail-complaints-to="abuse@aioe.org";
User-Agent: XanaNews/1.18.1.6
X-Notice: Filtered by postfilter v. 0.9.2
 by: Robert L. - Sun, 20 Feb 2022 02:28 UTC

> > 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 or Racket:

(require srfi/1) ;; fold for Racket

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

(fold
(lambda (xs accum)
(let ((nums (drop xs 2)))
(map +
(if (eqv? 'A (second xs))
`(,@nums 0 0)
`(0 0 ,@nums))
accum)))
'(0 0 0 0)
data)

===>
'(518 25 393 9)


devel / comp.lang.lisp / .Re: summing in a loop and iterate

1
server_pubkey.txt

rocksolid light 0.9.81
clearnet tor