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: Jean Louis
Subject: Re: [External] : Re: Testing whether a list contains at least one non-nil element
Date: Fri, 28 Oct 2022 08:19:20 +0300
User-agent: Mutt/2.2.7+37 (a90f69b) (2022-09-02)

* tomas@tuxteam.de <tomas@tuxteam.de> [2022-10-28 07:50]:
> On Thu, Oct 27, 2022 at 11:38:57PM +0300, Jean Louis wrote:
> > * Drew Adams <drew.adams@oracle.com> [2022-10-27 18:54]:
> > > In the worst case for your code, the first element IS non-nil and
> > > you spend forever doing useless stuff.  In no case is your code as
> > > efficient as just testing each list element, starting at the
> > > beginning, and STOPPING as soon as you find a non-nil element.
> > 
> > Of course I agree with the thought in general.
> > 
> > Is it this below?
> > 
> > (defun check-if-any-elt-is-non-nil (list)
> >   (let (there-is)
> >     (while (and list (not there-is))
> >       (when (car list) (setq there-is t))
> >       (setq list (cdr list)))
> >     there-is))
> 
> If you are doing it "by hand", why not indulge in Lisp's
> "classic elegance", like so:
> 
>   (defun has-non-nil (lst)
>     (cond
>      ((null lst) nil)
>      ((consp lst) (or (not (null (car lst))) (has-non-nil (cdr lst))))
>      (t (error "Not a proper list! You cheater!"))))

✔️ Beautiful 

-- 
Jean

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns

In support of Richard M. Stallman
https://stallmansupport.org/



reply via email to

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