[R6RS] End of file object

William D Clinger will at ccs.neu.edu
Thu Mar 30 10:32:55 EST 2006


Anton wrote:
> I'm not sure that extending COND is the right solution

Me neither.  That was off the cuff.  I wouldn't have
posted it if Mike hadn't asked me to.

> A similar effect could be achieved with a procedure, which has the 
> benefit of being composable and usable in other contexts, e.g.:
> 
> (define test-eof
>    (lambda (x)
>      (if (eof-object? x) #f x)))


> BTW, is there not an efficiency issue here, i.e. might it be easier for 
> a compiler to optimize the case where read-char returns #f directly?

I don't see an efficiency issue here.  With libraries,
compilers will be free to generate inline code for things
like test-eof, and will routinely transorm things like

    (cond
       ((test-eof (read-char p))
        => (lambda (ch) ...))
       ...)

into

    (let ((temp (let ((x (read-char p)))
                  (if (eof-object? x) #f x))))
      (if temp
          ((lambda (ch) ...) temp)
          (cond ...)))

which A-normalizes to

    (let* ((x (read-char p))
           (t1 (eof-object? x)))
           (temp (if t1 #f x)))
      (if temp
          ((lambda (ch) ...) temp)
          (cond ...)))

which rewrites to some equivalent of

    (let* ((x (read-char p))
           (t1 (eof-object? x))
           (temp (if t1 #f x)))
      (if t1
          (if #f 
              ((lambda (ch) ...) #f)
              (cond ...))
          (if x
              ((lambda (ch) ...) x)
              (cond ...))

which simplifies to

    (let ((x (read-char p)))
      (if (eof-object? x)
          (cond ...)
          ((lambda (ch) ...) x)))

which will probably be just as efficient as

    (let ((x (read-char p)))
      (if x
          ((lambda (ch) ...) x)
          (cond ...)))

Will



More information about the R6RS mailing list