help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Basic questions about elisp


From: Francis Moreau
Subject: Re: Basic questions about elisp
Date: Thu, 5 Nov 2009 06:25:03 -0800 (PST)
User-agent: G2/1.0

On Nov 5, 1:59 pm, David Kastrup <d...@gnu.org> wrote:
> Francis Moreau <francis.m...@gmail.com> writes:
>
> >   (defmacro x-nconc (l e)
> >     `(if (null ,l) (setq ,l ,e) (nconc ,l ,e)))
>
> Actually, that's pretty stupid

thanks

> since it is exactly the same as
> (defmacro (l e) `(setq ,l (nconc ,l ,e)))
>
> It is less obscure to use nconc in the same manner as append, namely
> using the return value (and accepting the side-effect for efficiency's
> sake).  And then you don't need your personal macro and get more
> readable code.

yes, I actually missed the use of the return value.

>
> The usual iteration would be something like
>
> (while lst
>   <do something with (car lst)>
>   <maybe append something to lst>
>   (setq lst (cdr lst)))
>
> If you really want efficiency, you'll not append something to lst in
> order to avoid quadratic behavior.  Then you'd rather do something like
>
> (let ((iter lst) app)
>   (while
>     (progn
>        (while iter
>           <do something with (car iter)>
>           <maybe (push something app)>
>           (setq lst (cdr lst)))
>         app)
>      (setq iter (nreverse app) app nil)))
>

hmm, it's probably my lack of elisp usage/knowledge, but it's terribly
hard to read for something really simple to achieve...

BTW, is '(setq lst (cdr lst)))' correct ?

> Note that this does not change the original list at all.  If that's not
> what is desired, you can write the last setq as
>
> (setq iter (nreverse app) lst (nconc lst iter) app nil)
>
> This still is suboptimal since the nconc will repeatedly traverse the
> same elements.

Lists are really implemented as simply linked list ?

Thanks


reply via email to

[Prev in Thread] Current Thread [Next in Thread]