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

From: Matthias Felleisen (matthias at ccs.neu.edu)
Date: Fri Feb 15 14:19:54 EST 2008

Marco, I have struggled with this issue for years. It would have been
easy to add and& and or& primitives to the teaching languages for HtDP.

But to what end?

HtDP stands for two meta-guidelines on intro courses:

  -- less is more, so make a small language for teaching
     and don't hesitate to make it different from the real thing

  -- there must be a straight path from teaching languages
     to real ones.

Consider and/or to be something that does just that. They
must be there, and yet, there is no need to hide this ugliness
of strict languages just so we can write

  andmap l = foldl and true l

and friends. Students need to know that the 'real' thing is
ugly, no matter what.

-- Matthias






On Feb 15, 2008, at 1:21 PM, Marco Morazan wrote:

> 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
>>
> _________________________________________________
>   For list-related administrative tasks:
>   http://list.cs.brown.edu/mailman/listinfo/plt-scheme



Posted on the users mailing list.