all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* Testing whether a list contains at least one non-nil element
@ 2022-10-25 10:16 Heime
  2022-10-25 10:46 ` Jean Louis
                   ` (4 more replies)
  0 siblings, 5 replies; 39+ messages in thread
From: Heime @ 2022-10-25 10:16 UTC (permalink / raw)
  To: Heime via Users list for the GNU Emacs text editor


I would like to test whether a list contains at least one non-nil element?  If I do that, then
I would be able to continue a loop where a non-nil element would mean that a match was found.





^ permalink raw reply	[flat|nested] 39+ messages in thread
* RE: [External] : Re: Testing whether a list contains at least one non-nil element
@ 2022-10-28  5:07 Drew Adams
  0 siblings, 0 replies; 39+ messages in thread
From: Drew Adams @ 2022-10-28  5:07 UTC (permalink / raw)
  To: Jean Louis; +Cc: help-gnu-emacs@gnu.org

> (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))

Sure, that works.  That's the same as one of
the functions I sent without bothering with
the `let' variable:

(defun foo (xs)
  (while (and (not (car xs)) xs)
    (setq xs (cdr xs)))
  (car xs))

> (benchmark 10000000 (check-if-any-elt-is-non-nil (append (list 1) (make-
> list 1000000 nil)))) ⇒ "Elapsed time: 0.771948s"
> 
> (benchmark 10000000 (check-if-any-elt-is-non-nil (append (make-list
> 1000000 nil) (list 1) ))) ⇒ "Elapsed time: 0.744323s"

I don't see that - at all. Did you really not
quote the second arg to `benchmark'?  Anyway,
take the list creations out of your timings.
___

You might compare the times needed to create
your two lists.  `append' is not symmetric.

Dunno just how `append' is implemented in C.
But you can see from a recursive Lisp
definition how different the two list args
are treated:

(defun apnd (xs ys)
  (if (null xs)
      ys
    (cons (car xs) (apnd (cdr xs) ys))))

It cdrs down the first list, recursing, but
just conses onto the second list.  So it
takes longer with a long first list and a
short second one than with the args reversed.
It traverses only the first of its list args.
Things like this are good to keep in mind.

Compare by hand (apnd nil '(1 2 3)) with
(apnd '(1 2 3) nil).  The first is done as
soon as it starts - no recursion at all. The
second recurses once for each element of the
first list: (cons 1 (cons 2 (cons 3 nil)))

(benchmark 1000 '(append (list 1) (make-list 10000 nil)))
"Elapsed time: 2.032493s (1.942879s in 45 GCs)"

(benchmark 1000 '(append (make-list 10000 nil) (list 1)))
"Elapsed time: 4.165245s (3.944074s in 91 GCs)"


^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2022-10-30 12:49 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
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
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

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.