[plt-scheme] What are "and" and "or" ?

From: Marco Morazan (morazanm at gmail.com)
Date: Fri Feb 15 13:21:59 EST 2008

Thank you for the responses. I was wondering if there existed "and" or
"or" primitives that evaluated all its arguments hidden somewhere
behind the scenes. There is not which makes it a bit tricky to explain
to new CS students why "and" and "or" can not be passed around.

Cheers,

Marco

On Fri, Feb 15, 2008 at 12:04 PM, Carl Eastlund <cce at ccs.neu.edu> wrote:
> 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
>


Posted on the users mailing list.