guile-user
[Top][All Lists]
Advanced

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

Re: About the primitive macros `and' and `or'


From: Klaus Schilling
Subject: Re: About the primitive macros `and' and `or'
Date: Fri, 25 Dec 2009 09:21:38 +0100 (CET)

From: Yi DAI <address@hidden>
Subject: About the primitive macros `and' and `or'
Date: Fri, 25 Dec 2009 13:18:01 +0800

> Hi,
> 
> 
> I don't see the point why Scheme provides the general `and' and `or' as
> primitive macros (which does stand in our way when we wanna (apply and
> things) instead of primitive procedures. For efficiency? I don't think there
> would be much compared to the following definitions:
> 
> (define (gand . l)  ; 'g'eneral 'and'
>   (define (iand l)
>     (if (null? l)
>         #t
>         (let ((b (car l))
>               (bs (cdr l)))
>           (cond ((null? bs) b)
>                 (b (iand bs))
>                 (else #f)))))
>   (iand l))
> 
> (define (gor . l)  ; 'g'eneral 'or'
>   (define (ior l)
>     (if (null? l)
>         #f
>         (let ((b (car l))
>               (bs (cdr l)))
>           (cond ((null? bs) b)
>                 (b b)
>                 (else (ior bs))))))
>   (ior l))

As a function, gor needs to have its arguments evaluated before the
actual application takes place, moreover, the order of evaluation is
not specified.

Hence 

(define x 0)
(gor (= x 0) (/ x))

gives a numerical overflow error,
whereas

(define x 0)
(or (= x 0) (/ x))

returns true.

If the terms to be evaluated contain non-commutable side effects,
things get out of control, whereas in the macro `or', you may rely on
the order of the side effects.

Klaus Schilling 




reply via email to

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