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

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

Path: ...!weretis.net!feeder8.news.weretis.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: (loop initially ... getting non-ANSI CL warning
Date: Wed, 7 Aug 2024 07:53:18 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 68
Message-ID: <v8v95c$2bgqc$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=iso-8859-1
Injection-Date: Wed, 07 Aug 2024 09:53:18 +0200 (CEST)
Injection-Info: dont-email.me; posting-host="3618421e7cf17a9fd6ff329509e3098e";
	logging-data="2474828"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX19nAb+S8BRD62VNCfxJkN67"
User-Agent: XanaNews/1.18.1.6
Cancel-Lock: sha1:hWHJR9vk0qERRnd1ee6n7exLOy4=
Bytes: 2542

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))

(setq list '(0 0 0 2 3 4 4 5 6 6 6))

(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))

0
0
4
6
6


newLISP

(setq lst '(0 0 0 2 3 4 4 5 6 6 6))

(apply (fn (a b)(if (= a b) (println a)) b) lst 2)

0
0
4
6
6