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

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

Re: How to mapcar or across a list?


From: Emanuel Berg
Subject: Re: How to mapcar or across a list?
Date: Thu, 16 Jul 2015 00:21:06 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.4 (gnu/linux)

Marcin Borkowski <mbork@mbork.pl> writes:

> so here's my problem: I have a list of Boolean
> values, and I want to `mapcar' an `or' across it
> (IOW, I want to test whether at least one of them is
> true). Of course, (apply #'or my-list) does not
> work. Of course, I can (cl-reduce (lambda (x y) (or
> x y)) my-list) -- but is there a better method?

"Better" I don't know, but how about using your
new-found skills with the backquote? (`or' itself
should be used, of course.)

    (setq boolean-list-1 '(t   nil nil nil t   nil nil))
    (setq boolean-list-2 '(nil nil nil nil nil nil nil))

    (eval `(or ,@boolean-list-1)) ; t
    (eval `(or ,@boolean-list-2)) ; nil

> BTW, my-list doesn't really exist: it is a result of
> `mapcar'ing a function taking some value and
> yielding a Boolean value, so bonus points if the
> method does not process the whole list.

Check. It is a consequence of using `or', which is
"short circuited" as it is called in other languages,
perhaps in Lisp too - I've read
"conditional evaluation" as well in AI books, but
perhaps that is something else.

Anyway:

    (or t undefined) ; t
    (or undefined t) ; error: (void-variable undefined) in eval

To verify, append "undefined" to "boolean-list-1"
(no error) - but, there will be an error (the same as
above) if appended to "boolean-list-2"!

... I suppose now would be a good time for you to hand
over them points!

-- 
underground experts united
http://user.it.uu.se/~embe8573




reply via email to

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