[lnkForumImage]
TotalShareware - Download Free Software

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


 

Forums >

comp.lang.lisp

Re: (loop initially ... getting non-ANSI CL warning

William James

1/7/2016 7:06:00 AM

Ken Tilton wrote:

> Mirko.Vukovic@gmail.com wrote:
> > Hi,
> >
> > I am still in a bit of shock that my first loop attempt works, with
> > one legalistic exception.
> >
> > This is my simple code to show duplicates in a list:
> >
> > (defun show-duplicates (list)
> > (loop
> > initially with ref = (first list)
>
> That is a blank initially clause. CLisp is being nice and coping with
> that and pretending you just wrote (loop with ref = (first list)....
>
> > for entry in (rest list) do
> > (if (equal ref entry)
> > (print entry))
>
> Using when when when is suitable makes code more readable.
>
> > (setf ref entry)))
>
> Loop is pretty powerful so you should have a nervous feeling when you
> find yourself setfing key iteration variables.
>
> It looks as if you are assuming a sorted list and simply printing out
> elements the same as the preceding element:
>
> (loop for (a b) on list
> when (eql a b)
> do (print b))
>
> You get an extra iteration on the last element and nil so an input list
> of '(nil nil) would print an extra duplicate. If that is a problem you can:
>
> (loop for a in list
> for b in (cdr list) ; this will run out first and halt the loop
> when (eql a b)
> do (print b))

MatzLisp (Ruby):

[2,2,3,4,4,2].each_cons(2){|a,b| p b if a==b}
===>
2
4

--
When I was a Revolutionary Marxist, we were all in favour of as much
immigration as possible. It wasn't because we liked immigrants, but because we
didn't like Britain.
www.dailymail.co.uk/news/article-2301743/How-invasion-immigrants-corner-England-mockery-PMs-promise-close-door.html