behaviour of ~& in format string Stephen Lewis (02 Jul 2005 23:46 UTC)
Re: behaviour of ~& in format string Ken Dickey (03 Jul 2005 03:33 UTC)

Re: behaviour of ~& in format string Ken Dickey 03 Jul 2005 02:37 UTC

Stephen,

You raise a good point about the inability of FORMAT to determine, between
invocations, that a new line has been started.

The reason that the reference implementation of FORMAT does not remember state
about the last character between invocations is that [1] it is not the only
function which side effects output, and [2] there is no _portable_ way to
query a Scheme port to determine it's state history with respect to newlines.

Consider the following example:

(format "abc~%")
(display "x")
(format "~&def~&ghi~%")

A "stateful" format would be correct with your sample, but in the above case
yield the (incorrect)

 abc
 xdef
 ghi

rather than the correct "unstateful" (current) srfi-48

 abc
 x
 def
 ghi

Given the amazing creativity w.r.t port implementations amoung various Scheme
runtimes, I opted NOT to try and respecify/shadow/override all output as part
of SRFI-48.  There are  just too many non-R^nRS output functions in use (in
object ports, vector ports, fifo ports, graphic ports...).

Given this lack, I chose to be "wrong" in cases where state would be required
to be "remembered" between invocations of FORMAT rather than the converse.

Note that this does accord with the Common Lisp definition for ~& :

  "Unless it can be determined that the output stream is already at
   the beginning of a line, this outputs a newline".

Traditionally, this is implemented as a call to the function FRESH-LINE, where
the text elucidates ["this" refers to being at the start of a line]:
  "(If for some reason this cannot be determined, then a newline is output
  anyway)."

[Guy Steele, Jr, _Common LISP, The Language, Second Edition_, Digital Press,
1990, pg 596, pg 579].

FRESH-LINE is implemented various Scheme dialects (e.g. MIT-Scheme, T) with
the same definition.

The CL specification, and my implementation, are conservative but may result
in a "spurious" newline--which IS allowed by the definition.

It is probably worthwhile mentioning this in the ISSUES section of SRFI-48.

I have been neglegent in this respect and shall update the text.

Thanks much for pointing this out.

-KenD

===========================================
On Saturday 02 July 2005 16:46, you wrote:
> Ken Dickey,
> I believe that '~&" should ensure that output begins on a new line. That
> is, output a newline character if (and only if) the output stream is *not*
> already at the start of a line.
> ...
> (format t "abc~%")
> (format t "~&def~&ghi~%")
...
>== I believe that the blank line between "abc" and "def" (which only occurs
> when I use srfi-48 under either Guile or Gauche) is spurious,
> Stephen Lewis