Why require dummy values?
John David Stone 02 Apr 2002 16:55 UTC
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.
The code for the sample implementation also uses () rather than '()
to denote the empty list, which is non-R5RS.
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)))))))
-----------------------------------------------------------------------------