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

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

Re: [External] : Re: Testing whether a list contains at least one non-ni


From: tomas
Subject: Re: [External] : Re: Testing whether a list contains at least one non-nil element
Date: Sat, 29 Oct 2022 08:38:00 +0200

On Fri, Oct 28, 2022 at 09:09:12AM -0400, Stefan Monnier via Users list for the 
GNU Emacs text editor wrote:
> Michael Heerdegen [2022-10-28 08:20:45] wrote:
> > <tomas@tuxteam.de> writes:
> >> If you are doing it "by hand", why not indulge in Lisp's
> >> "classic elegance", like so:

[...]

> > You don't want to implement such functions in Emacs with recursion
> > because you'll easily hit `max-lisp-eval-depth' when they are called.
> 
> Indeed (and it's significantly slower than the corresponding loop
> without function calls).

I /knew/ I was missing something. The back of my brain mumbled
"does Emacs have tail recursion elimination or doesn't it?"

> > Sorry to tell you, but a loop is the preferable way in Elisp.
> 
> Luckily, since Emacs-28 you can have your cake and eat it too:
> 
>     (defun has-non-nil (lst)
>       (named-let loop ((lst lst))
>         (cond
>          ((null lst) nil)
>          ((consp lst) (or (not (null (car lst))) (loop (cdr lst))))
>          (t (error "Not a proper list! You cheater!")))))

Thanks for that :)

> Admittedly, here the `named-let` construct gives you only the
> elimination of tail-recursion, but in many other circumstances it
> also leads to quite elegant code.

Now I think of it, I saw something flashing past emacs-devel; back
then I must have thought "oh, nice, I've got to play with this..."

Now it sank in. So thanks :)

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


reply via email to

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