Re: Why require dummy values?
Scott G. Miller 02 Apr 2002 19:26 UTC
On Tue, Apr 02, 2002 at 10:55:50AM -0600, John David Stone wrote:
> The sample implementation SRFI-28 appears to require that a dummy
> value be supplied whenever the ~% or ~~ escape sequence is used in a format
> string, so that, for instance
>
> (format "~~~a" #\b)
>
> crashes -- the format string contains two escape sequences but only one
> corresponding value. I suspect that ~% and ~~ should not be counted as
> ``value-requiring escape sequences'' in the sense intended in the
> specification.
Yes, this is a bug in both the spec and the implementation.
>
> The code for the sample implementation also uses () rather than '()
> to denote the empty list, which is non-R5RS.
The consequence of writing it on a lax scheme system.
>
> I propose instead the following sample implementation of SRFI-28.
> It depends on SRFI-6 and SRFI-23.
>
> -----------------------------------------------------------------------------
> (define format
> (lambda (format-string . objects)
> (let ((buffer (open-output-string)))
> (let loop ((format-list (string->list format-string))
> (objects objects))
> (cond ((null? format-list) (get-output-string buffer))
> ((char=? (car format-list) #\~)
> (if (null? (cdr format-list))
> (error 'format "Incomplete escape sequence")
> (case (cadr format-list)
> ((#\s)
> (if (null? objects)
> (error 'format "No value for escape sequence")
> (begin
> (write (car objects) buffer)
> (loop (cddr format-list) (cdr objects)))))
> ((#\a)
> (if (null? objects)
> (error 'format "No value for escape sequence")
> (begin
> (display (car objects) buffer)
> (loop (cddr format-list) (cdr objects)))))
> ((#\%)
> (display #\newline buffer)
> (loop (cddr format-list) objects))
> ((#\~)
> (display #\~ buffer)
> (loop (cddr format-list) objects))
> (else
> (error 'format "Unrecognized escape sequence")))))
> (else (display (car format-list) buffer)
> (loop (cdr format-list) objects)))))))
I like this implementation. The error checking is far superior to the
version I hastily wrote.
I'll submit the changes to the spec today.
Scott