unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: Jean Louis <bugs@gnu.support>
To: tomas@tuxteam.de
Cc: help-gnu-emacs@gnu.org, Drew Adams <drew.adams@oracle.com>
Subject: Re: [External] : Re: Testing whether a list contains at least one non-nil element
Date: Fri, 28 Oct 2022 08:19:20 +0300	[thread overview]
Message-ID: <Y1tmWLmddiuocQLZ@protected.localdomain> (raw)
In-Reply-To: <Y1tfLP7GAC2ukd+f@tuxteam.de>

* 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	other threads:[~2022-10-28  5:19 UTC|newest]

Thread overview: 39+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-10-25 10:16 Testing whether a list contains at least one non-nil element Heime
2022-10-25 10:46 ` Jean Louis
2022-10-25 10:47 ` Jean Louis
2022-10-25 10:50 ` Jean Louis
2022-10-25 12:12   ` Emanuel Berg
2022-10-26 18:56     ` Jean Louis
2022-10-27  3:54       ` [External] : " Drew Adams
2022-10-27  4:53         ` Jean Louis
2022-10-27  5:30           ` Emanuel Berg
2022-10-27 15:54           ` Drew Adams
2022-10-27 17:47             ` Elisp and CL (was: Re: [External] : Re: Testing whether a list contains at least one non-nil element) Emanuel Berg
2022-10-27 20:38             ` [External] : Re: Testing whether a list contains at least one non-nil element Jean Louis
2022-10-28  0:22               ` Emanuel Berg
2022-10-28  4:48               ` tomas
2022-10-28  5:19                 ` Jean Louis [this message]
2022-10-28  6:20                 ` Michael Heerdegen
2022-10-28 13:09                   ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-29  6:10                     ` Michael Heerdegen
2022-10-29 15:25                       ` Stefan Monnier via Users list for the GNU Emacs text editor
2022-10-30 12:49                         ` Emanuel Berg
2022-10-29  6:38                     ` tomas
2022-10-30 12:48                     ` Emanuel Berg
2022-10-29  9:20                   ` Emanuel Berg
2022-10-29  9:19                 ` Emanuel Berg
2022-10-27  4:01       ` Emanuel Berg
2022-10-25 12:05 ` Michael Heerdegen
2022-10-25 12:15   ` Emanuel Berg
2022-10-25 15:59   ` [External] : " Drew Adams
2022-10-25 17:44     ` Emanuel Berg
2022-10-26 15:39       ` Drew Adams
2022-10-26 17:43         ` Emanuel Berg
2022-10-25 17:25 ` Joost Kremers
2022-10-25 17:51   ` Emanuel Berg
2022-10-25 19:44   ` Heime
2022-10-25 20:08     ` Joost Kremers
2022-10-25 20:15     ` [External] : " Drew Adams
2022-10-25 20:19       ` Joost Kremers
2022-10-26  3:00         ` Stefan Monnier via Users list for the GNU Emacs text editor
  -- strict thread matches above, loose matches on Subject: below --
2022-10-28  5:07 Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=Y1tmWLmddiuocQLZ@protected.localdomain \
    --to=bugs@gnu.support \
    --cc=drew.adams@oracle.com \
    --cc=help-gnu-emacs@gnu.org \
    --cc=tomas@tuxteam.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).