On this page:
write
display
print
fprintf
printf
format
print-pair-curly-braces
print-mpair-curly-braces
print-unreadable
print-graph
print-struct
print-box
print-vector-length
print-hash-table
print-honu
current-write-relative-directory
port-write-handler
port-display-handler
port-print-handler
global-port-print-handler
Version: 4.1

12.5 Writing

(write datum [out])  void?

  datum : any/c

  out : output-port? = (current-output-port)

Writes datum to out, normally in such a way that instances of core datatypes can be read back in. If out has a handler associated to it via port-write-handler, then the handler is called. Otherwise, the default printer is used (in write mode), as configured by various parameters.

See The Printer for more information about the default printer. In particular, note that write may require memory proportional to the depth of the value being printed, due to the initial cycle check.

(display datum [out])  void?

  datum : any/c

  out : output-port? = (current-output-port)

Displays datum to out, similar to write, but usually in such a way that byte- and character-based datatypes are written as raw bytes or characters. If out has a handler associated to it via port-display-handler, then the handler is called. Otherwise, the default printer is used (in display mode), as configured by various parameters.

See The Printer for more information about the default printer. In particular, note that display may require memory proportional to the depth of the value being printed, due to the initial cycle check.

(print datum [out])  void?

  datum : any/c

  out : output-port? = (current-output-port)

Writes datum to out, normally the same way as write. If out has a handler associated to it via port-print-handler, then the handler is called. Otherwise, the handler specified by global-port-print-handler is called; the default handler uses the default printer in write mode.

The rationale for providing print is that display and write both have relatively standard output conventions, and this standardization restricts the ways that an environment can change the behavior of these procedures. No output conventions should be assumed for print, so that environments are free to modify the actual output generated by print in any way.

(fprintf out form v ...)  void?

  out : output-port?

  form : string?

  v : any/c

Prints formatted output to out, where form is a string that is printed directly, except for special formatting escapes:

The form string must not contain any ~ that is not one of the above escapes, otherwise the exn:fail:contract exception is raised. When the format string requires more vs than are supplied, the exn:fail:contract exception is raised. Similarly, when more vs are supplied than are used by the format string, the exn:fail:contract exception is raised.

Examples:

  > (fprintf (current-output-port)

             "~a as a string is ~s.~n"

             '(3 4)

             "(3 4)")

  (3 4) as a string is "(3 4)".

(printf form v ...)  void?

  form : string?

  v : any/c

The same as (fprintf (current-output-port) form v ...).

(format form v ...)  string?

  form : string?

  v : any/c

Formats to a string. The result is the same as

  (let ([o (open-output-string)])

    (fprintf o form v ...)

    (get-output-string o))

Examples:

  > (format "~a as a string is ~s.~n" '(3 4) "(3 4)")

  "(3 4) as a string is \"(3 4)\".\n"

(print-pair-curly-braces)  boolean?

(print-pair-curly-braces on?)  void?

  on? : any/c

A parameter that control pair printing. If the value is true, then pairs print using { and } instead of ( and ). The default is #f.

(print-mpair-curly-braces)  boolean?

(print-mpair-curly-braces on?)  void?

  on? : any/c

A parameter that control pair printing. If the value is true, then mutable pairs print using { and } instead of ( and ). The default is #t.

(print-unreadable)  boolean?

(print-unreadable on?)  void?

  on? : any/c

A parameter that controls printing values that have no readable form (using the default reader), including structures that have a custom-write procedure (see prop:custom-write); defaults to #t. See The Printer for more information.

(print-graph)  boolean?

(print-graph on?)  void?

  on? : any/c

A parameter that controls printing data with sharing; defaults to #f. See The Printer for more information.

(print-struct)  boolean?

(print-struct on?)  void?

  on? : any/c

A parameter that controls printing structure values in vector or prefab form; defaults to #t. See The Printer for more information. This parameter has no effect on the printing of structures that have a custom-write procedure (see prop:custom-write).

(print-box)  boolean?

(print-box on?)  void?

  on? : any/c

A parameter that controls printing box values; defaults to #t. See Printing Boxes for more information.

(print-vector-length)  boolean?

(print-vector-length on?)  void?

  on? : any/c

A parameter that controls printing vectors; defaults to #f. See Printing Vectors for more information.

(print-hash-table)  boolean?

(print-hash-table on?)  void?

  on? : any/c

A parameter that controls printing hash tables; defaults to #f. See Printing Hash Tables for more information.

(print-honu)  boolean?

(print-honu on?)  void?

  on? : any/c

A parameter that controls printing values in an alternate syntax. See Honu for more information.

(current-write-relative-directory)

  (or/c (and/c path? complete-path?) false/c)

(current-write-relative-directory path)  void?

  path : (or/c (and/c path-string? complete-path?) false/c)

A parameter that is used when writing compiled code that contains pathname literals, including source-location pathnames for procedure names. When not #f, paths that syntactically extend the parameter’s value are converted to relative paths; when the resulting compiled code is read, relative paths are converted back to complete paths using the current-load-relative-directory parameter (if it is not #f, otherwise the path is left relative).

(port-write-handler out)  (any/c output-port? . -> . any)

  out : output-port?

(port-write-handler out proc)  void?

  out : output-port?

  proc : (any/c output-port? . -> . any)

(port-display-handler out)  (any/c output-port? . -> . any)

  out : output-port?

(port-display-handler out proc)  void?

  out : output-port?

  proc : (any/c output-port? . -> . any)

(port-print-handler out)  (any/c output-port? . -> . any)

  out : output-port?

(port-print-handler out proc)  void?

  out : output-port?

  proc : (any/c output-port? . -> . any)

Gets or sets the port write handler, port display handler, or port print handler for out. This handler is call to output to the port when write, display, or print (respectively) is applied to the port. Each handler takes a two arguments: the value to be printed and the destination port. The handler’s return value is ignored.

The default port display and write handlers print Scheme expressions with Scheme’s built-in printer (see The Printer). The default print handler calls the global port print handler (the value of the global-port-print-handler parameter); the default global port print handler is the same as the default write handler.

(global-port-print-handler)  (any/c output-port? . -> . any)

(global-port-print-handler proc)  void?

  proc : (any/c output-port? . -> . any)

A parameter that determines global port print handler, which is called by the default port print handler (see port-print-handler) to print values into a port. The default value uses the built-in printer (see The Printer) in write mode.