guile-user
[Top][All Lists]
Advanced

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

Re: built-in procedural logical operator


From: David Kastrup
Subject: Re: built-in procedural logical operator
Date: Tue, 01 Sep 2015 18:21:40 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/25.0.50 (gnu/linux)

Alex Vong <address@hidden> writes:

> Hi everyone,
>
> I am learning scheme, please bear with me if I am wrong.
>
> I try to define a new function LIST-OF-STRING?
>
>     (define (list-of-string? lst)
>       (reduce and #f (map string? lst)))
>
> and I get the error
>
>     While compiling expression:
>     ERROR: Syntax error:
>     unknown location: source expression failed to match any pattern in form 
> and
>
> It seems it is because AND is implemented as a macro,
> so I try to implement my own RECURSIVE-AND
>
>     (define (recursive-and . arg-lst)
>       (cond ((null? arg-lst) #t)
>             ((not (car arg-lst)) #f)
>             (else (apply recursive-and (cdr arg-lst)))))
>
> and now
>
>     (define (list-of-string? lst)
>       (reduce recursive-and #f (map string? lst)))
>
> works as intended.
>
> Is it a leaking implementation detail that AND is implemented as a
> macro?

No.  It cannot be a function since it stops evaluation after the first
false argument

> Do we have a built-in procedural logical operators so that we don't
> that error?

>From (srfi srfi-1):

(and ...) -> (every identity ...
(or ...) -> (any identity ...
(define (list-of-string? lst) (every string? lst))

Of course, this definition returns #t for '() while your definition
returns #f for '().  I'd argue the former is more correct.  But of
course you can just write
(define (list-of-string lst) (and (pair? lst) (every string? lst)))
if you want the latter.

-- 
David Kastrup




reply via email to

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