[plt-scheme] What are "and" and "or" ?
The logical forms like and, or, if, and so on are syntax (macros or
built-in). Why? Consider the following:
(define (singleton-list? x)
(and (pair? x) (null? (cdr x))))
(singleton-list? 5)
If and were a function, this would first evaluate its arguments:
(pair? 5) => false
(null? (cdr 5)) => error: cannot take the cdr of 5
But, as programmers and logicians, we know that after (pair? 5)
returns false, we don't need to evaluate (null? (cdr 5)), and that
error shouldn't matter. So (and ...) is a macro that stops the first
time it hits false, and (or ...) is a macro that stops the first time
it hits a true value.
You can always write a function that compares its arguments with and,
if you need to pass it around, as long as you don't need or expect it
to have the same "short-circuiting" behavior.
On Fri, Feb 15, 2008 at 11:57 AM, Marco Morazan <morazanm at gmail.com> wrote:
> Dear All,
>
> Are "and" and "or" primitives?
>
> I see the following behavior (v370p1):
>
> > +
> #<primitive:+>
> > <
> #<primitive:<>
> > or
> or: bad syntax in: or
> > and
> and: bad syntax in: and
> > (and #t #t)
> #t
> > (or #f #f)
> #f
> >
>
> So, why can I not pass and AND or around like + and * ?
>
> Thanks,
>
> Marco