[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.lisp

Re: association list --updating

William James

5/7/2015 7:22:00 AM

Mark McConnell wrote:

> Ken Tilton wrote:
> >
> > Jeremy Medows wrote:
> > >
> > > Hi,
> > > I am pretty new to LISP and I can't seem to find the answer to this
> > > question in any of my books.
> > >
> > > I have the following association list:
> > >
> > > ((A.1)(B.33)(C.45)(D.9)(E.1))
> > > How can I use the mapping functions and my function inc -- which
> > > is just (defun inc (x) (+ x 1)) to increment each of the second values
> > > in each dotted pair?
> > >
> > > I would like my output to look like:
> > > ((A.2)(B.34)(C.46)(D.10)(E.2))
> > >
> > > Jeremy
> >
> > You are on the right track: use a mapping function. #'mapcar is the most
> > famous, but it would make a *new* assoc. Since you are destructively
> > modifying assoc *elements*, there is no point in that (making a new list
> > would not leave the original unchanged), so you can use #'mapc to
> > iterate over the assoc (which after all is just a list).
>
> Here is another way to explain it. Say z is your association list.
> When you add 1 to each number, you probably don't want to make
> a whole new list; you probably just want to go into z, rip out
> the 1 and replace it with a 2, rip out the 33 and replace it
> with a 34, etc. To do this:
>
> (mapc #'(lambda (pair) (incf (cdr pair)))
> z)
>
> Here (incf ...) takes the ..., adds 1 to it, and stores it back
> _into the same location_ where it was.

Gauche Scheme:

(define al '((A . 1)(B . 33)(C . 45)(D . 9)(E . 1)))

(for-each (^x (inc! (cdr x))) al)

al
===>
((A . 2) (B . 34) (C . 46) (D . 10) (E . 2))

--
The pattern is the same. The gangs are almost always operated by Dutch-born
Moroccan and Turkish men. The men give their victims alcohol and drugs, use
lies to separate them from their families, gang rape them (often taking photos
for blackmail purposes), and then place the girls in prostitution.... Some of
these girls are as young as 11 or 12 years old.
http://www.liveleak.com/view?i=807_...