unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* built-in procedural logical operator
@ 2015-09-01 15:44 Alex Vong
  2015-09-01 16:21 ` David Kastrup
  0 siblings, 1 reply; 3+ messages in thread
From: Alex Vong @ 2015-09-01 15:44 UTC (permalink / raw)
  To: guile-user

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?
Do we have a built-in procedural logical operators so that we don't that error?
I know it is easy enough to define your own RECURSIVE-AND and RECURSIVE-OR,
I just want to know it is a bug or a feature?
Thanks!

Cheers,
Alex



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: built-in procedural logical operator
  2015-09-01 15:44 built-in procedural logical operator Alex Vong
@ 2015-09-01 16:21 ` David Kastrup
       [not found]   ` <CADrxHD8e6VoAsY5_rtD3od8iSR9VdZamDMiYQf2hNGcmq3Hdfw@mail.gmail.com>
  0 siblings, 1 reply; 3+ messages in thread
From: David Kastrup @ 2015-09-01 16:21 UTC (permalink / raw)
  To: guile-user

Alex Vong <alexvong1995@gmail.com> 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




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: built-in procedural logical operator
       [not found]   ` <CADrxHD8e6VoAsY5_rtD3od8iSR9VdZamDMiYQf2hNGcmq3Hdfw@mail.gmail.com>
@ 2015-09-01 17:13     ` Alex Vong
  0 siblings, 0 replies; 3+ messages in thread
From: Alex Vong @ 2015-09-01 17:13 UTC (permalink / raw)
  To: guile-user

2015-09-02 0:21 GMT+08:00, David Kastrup <dak@gnu.org>:
> 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.

Thanks, this is exactly what I need. I love the name "every" and
"any", they sound very appropriate.

Cheers,
Alex



^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2015-09-01 17:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-01 15:44 built-in procedural logical operator Alex Vong
2015-09-01 16:21 ` David Kastrup
     [not found]   ` <CADrxHD8e6VoAsY5_rtD3od8iSR9VdZamDMiYQf2hNGcmq3Hdfw@mail.gmail.com>
2015-09-01 17:13     ` Alex Vong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).