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: Fri, 28 Oct 2022 06:48:44 +0200

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

Cheers
-- 
t

Attachment: signature.asc
Description: PGP signature


reply via email to

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