On 2008-03-31, fishmacs <·········@gmail.com> wrote:
Hi,
> Apparently and is not a normal function like +, -, ...
AND is a macro (because it may decide not to evaluate some of its arguments).
See http://www.lispworks.com/documentation/HyperSpec/Body/m_and.htm
> I can write (and t t nil), but if the parameters are packaged into a list,
> how can I do?
One thing to do is to (let your Lisp) write exactly that (using a macro your
self).
--
Oyvin
On Mon, 31 Mar 2008 21:41:24 +0800, fishmacs wrote:
> Apparently and is not a normal function like +, -, ...
Correct; AND is a macro.
> I can write (and t t nil), but if the parameters are packaged into a list,
> how can I do?
One way is to use EVERY, specifying the IDENTITY function as a predicate.
(every #'identity '(t t nil)) => nil
Another way is to use macro expansion:
(let ((list '(t t nil))) `(and ,@list)) => (and t t nil)
(eval *) => nil
--
Dave Seaman
Court affirms Judge Yohn's ruling.
<http://www.ipsnews.net/news.asp?idnews=41761>
fishmacs <·········@gmail.com> writes:
> Apparently and is not a normal function like +, -, ...
> I can write (and t t nil), but if the parameters are packaged into a list,
> how can I do?
Check out the functions EVERY and SOME.
--
(espen)
fishmacs <·········@gmail.com> writes:
> Apparently and is not a normal function like +, -, ...
> I can write (and t t nil), but if the parameters are packaged into a list,
> how can I do?
Indeed, AND and OR are macros, not functions. This allows to shortcut:
(and nil (drop-the-bomb)) ; won't drop it.
(or t (shoot-in-your-foot)) ; won't hurt you either.
You can use the functions EVERY and SOME instead:
(every (function identity) list-of-bools)
(some (function identity) list-of-bools)
(every (function not) list-of-bools)
(some (function not) list-of-bools)
--
__Pascal Bourguignon__
From: fishmacs
Subject: Re: (apply #'and '(t t nil)) not work
Date:
Message-ID: <ur6drvt73.fsf@gmail.com>
···@informatimago.com (Pascal J. Bourguignon) writes:
> Indeed, AND and OR are macros, not functions. This allows to shortcut:
But SBCL said AND is a closure?
On Mon, 31 Mar 2008 22:55:44 +0800, fishmacs wrote:
> ···@informatimago.com (Pascal J. Bourguignon) writes:
>> Indeed, AND and OR are macros, not functions. This allows to shortcut:
> But SBCL said AND is a closure?
SBCL 1.0.5 and the Common Lisp HyperSpec both say AND is a macro.
--
Dave Seaman
Court affirms Judge Yohn's ruling.
<http://www.ipsnews.net/news.asp?idnews=41761>
fishmacs <·········@gmail.com> writes:
> ···@informatimago.com (Pascal J. Bourguignon) writes:
>
>> Indeed, AND and OR are macros, not functions. This allows to shortcut:
> But SBCL said AND is a closure?
Of course, macros are functions! They're code making code.
But if you know how to ask, you'll see clearly AND is fbound to a
macro:
S/CL-USER[42]> (describe 'and)
AND is an external symbol in #<PACKAGE "COMMON-LISP">.
Macro-function: #<FUNCTION (MACRO-FUNCTION AND) {957F0C5}>Its associated name (as in FUNCTION-LAMBDA-EXPRESSION) is (MACRO-FUNCTION AND).
The macro's arguments are: (&REST FORMS)
On Sun, Mar 9, 2008 11:40:31 PM [-1] it was compiled from:
SYS:SRC;CODE;DEFBOOT.LISP Created: Saturday, March 3, 2007 11:49:26 AM [-1]
Documentation on the method-combination:NIL
S/CL-USER[43]> (symbol-function 'and)
#<CLOSURE (SB-C::&OPTIONAL-DISPATCH (LAMBDA (&REST SB-C::ARGS))) {99B040D}>
S/CL-USER[44]> (macro-function 'and)
#<FUNCTION (MACRO-FUNCTION AND) {957F0C5}>
S/CL-USER[45]>
--
__Pascal Bourguignon__ http://www.informatimago.com/
COMPONENT EQUIVALENCY NOTICE: The subatomic particles (electrons,
protons, etc.) comprising this product are exactly the same in every
measurable respect as those used in the products of other
manufacturers, and no claim to the contrary may legitimately be
expressed or implied.
f> Apparently and is not a normal function like +, -, ...
f> I can write (and t t nil), but if the parameters are packaged into a
f> list, how can I do?
indeed, AND is not a function, it's a special operator, pretty close to IF
or WHEN -- it stops evaluating as soon as it get first NIL (this is called
short circuiting sometimes).
there is a function called EVERY, it does exactly what you need:
CL-USER> (every #'identity '(t t nil))
NIL
CL-USER> (every #'identity '(t t t))
T
of course you can define your own function -- to get rid of #'indetity
parameter, or allowing it to agruments directly. i.e.
(defun my-and (&rest args) (every #'identity args))
CL-USER> (my-and t t nil)
NIL
CL-USER> (apply #'my-and '(t t nil))
NIL
this looks just like AND, but it does not short-circuit evaluation.
From: fishmacs
Subject: Re: (apply #'and '(t t nil)) not work
Date:
Message-ID: <uve33vtag.fsf@gmail.com>
"Alex Mizrahi" <········@users.sourceforge.net> writes:
> of course you can define your own function -- to get rid of #'indetity
> parameter, or allowing it to agruments directly. i.e.
>
> (defun my-and (&rest args) (every #'identity args))
>
> CL-USER> (my-and t t nil)
> NIL
> CL-USER> (apply #'my-and '(t t nil))
> NIL
>
> this looks just like AND, but it does not short-circuit evaluation.
Thanks all responses. But I do need short-circuit evalution.
I wrote a macro(very silly):
(defmacro andl (lst)
`(and ,@lst))
if type (andl '(1)) in the REPL, I get the error:
EVAL: variable QUOTE has no value
fishmacs wrote:
> I wrote a macro(very silly):
> (defmacro andl (lst)
> `(and ,@lst))
> if type (andl '(1)) in the REPL, I get the error:
> EVAL: variable QUOTE has no value
The variable lst in the macro is bound to (quote (1)), not (1), in
that example.
If you plan on applying something short circuiting to a list of
values, the evaluation has already been done (in the creation of the
list), so the short circuiting nature isn't going to buy you anything.
At the risk of confusing you, if you absolutely need short circuiting
with apply, this will work:
CL-USER(3): (defvar *foo* (list (lambda () t) (lambda () t) (lambda ()
nil) (lambda () (write-line "Death") t)))
*FOO*
CL-USER(4): (every #'funcall *foo*)
NIL
But there is a strong likelyhood that you don't need to do something
that drastic.
fishmacs <·········@gmail.com> writes:
> "Alex Mizrahi" <········@users.sourceforge.net> writes:
>
>> of course you can define your own function -- to get rid of #'indetity
>> parameter, or allowing it to agruments directly. i.e.
>>
>> (defun my-and (&rest args) (every #'identity args))
>>
>> CL-USER> (my-and t t nil)
>> NIL
>> CL-USER> (apply #'my-and '(t t nil))
>> NIL
>>
>> this looks just like AND, but it does not short-circuit evaluation.
> Thanks all responses. But I do need short-circuit evalution.
> I wrote a macro(very silly):
> (defmacro andl (lst)
> `(and ,@lst))
> if type (andl '(1)) in the REPL, I get the error:
> EVAL: variable QUOTE has no value
And who said EVERY and SOME don't do short circuit?
(every (function print) '(t t t nil 1 2 3 4))
prints:
T
T
T
NIL
returns:
NIL
(some (function print) '(nil nil nil t 1 2 3 4))
prints:
NIL
NIL
NIL
T
returns:
T
--
__Pascal Bourguignon__
??>> this looks just like AND, but it does not short-circuit evaluation.
f> Thanks all responses. But I do need short-circuit evalution.
short-circuiting only makes sense when you do evaluation.
if you have a list,it's contents are already evaluated, hence
short-circuiting doesn't make sense.
if you post some bigger sample of what you want to achieve perhaps we can
point what is best way of implementing this.
f> I wrote a macro(very silly):
f> (defmacro andl (lst)
f> `(and ,@lst))
f> if type (andl '(1)) in the REPL, I get the error:
f> EVAL: variable QUOTE has no value
sure it does not. because it is a macro, and it's contents are not
evaluated.
i advice not to use macros or EVAL unless you _absolutely_ want to. keep in
mind you can do 99.99% of stuff w/o macros and eval -- macros just make some
code more elegant, eval can help with extreme dynamism.
you absolutely do not need to write macros or use something advanced when
you're just writing regular code.
From: fishmacs
Subject: Re: (apply #'and '(t t nil)) not work
Date:
Message-ID: <uwsnhacc0.fsf@gmail.com>
In fact, what I want to do is predict if all numbers in a list are prime,
so I need short-circuit to break the loop if anyone not prime.
And thank you all, (every #'primep ...) worked great. I found it is a very
nice and helpful place here!
fishmacs wrote:
> In fact, what I want to do is predict if all numbers in a list are prime,
> so I need short-circuit to break the loop if anyone not prime.
> And thank you all, (every #'primep ...) worked great. I found it is a very
> nice and helpful place here!
Just wait until you put a parens on its own line, the honeymoon will be
over fast.
hth, kenny
ps. They love CamelCase as well. k
--
http://smuglispweeny.blogspot.com/
http://www.theoryyalgebra.com/
"In the morning, hear the Way;
in the evening, die content!"
-- Confucius