13.5 Writing
procedure
datum : any/c out : output-port? = (current-output-port)
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.
> (write 'hi) hi
> (write (lambda (n) n)) #<procedure>
> (define o (open-output-string)) > (write "hello" o) > (get-output-string o) "\"hello\""
procedure
datum : any/c out : output-port? = (current-output-port)
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.
procedure
datum : any/c out : output-port? = (current-output-port) quote-depth : (or/c 0 1) = 0
The optional quote-depth argument adjusts printing when the print-as-expression parameter is set to #t. In that case, quote-depth specifies the starting quote depth for printing datum.
The rationale for providing print is that display and write both have specific output conventions, and those conventions restrict the ways that an environment can change the behavior of display and write 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.
procedure
datum : any/c out : output-port? = (current-output-port)
Added in version 6.1.1.8 of package base.
procedure
datum : any/c out : output-port? = (current-output-port)
procedure
datum : any/c out : output-port? = (current-output-port) quote-depth : (or/c 0 1) = 0
The println function is not equivalent to println in other languages, because println uses printing conventions that are closer to write than to display. For a closer analog to println in other languages, use displayln.
Added in version 6.1.1.8 of package base.
procedure
out : output-port? form : string? v : any/c
~n or ~% prints a newline character (which is equivalent to \n in a literal format string)
~a or ~A displays the next argument among the vs
~s or ~S writes the next argument among the vs
~v or ~V prints the next argument among the vs
~.‹c› where ‹c› is a, A, s, S, v, or V: truncates display, write, or print output to (error-print-width) characters, using ... as the last three characters if the untruncated output would be longer
~e or ~E outputs the next argument among the vs using the current error value conversion handler (see error-value->string-handler) and current error printing width
~c or ~C write-chars the next argument in vs; if the next argument is not a character, the exn:fail:contract exception is raised
~b or ~B prints the next argument among the vs in binary; if the next argument is not an exact number, the exn:fail:contract exception is raised
~o or ~O prints the next argument among the vs in octal; if the next argument is not an exact number, the exn:fail:contract exception is raised
~x or ~X prints the next argument among the vs in hexadecimal; if the next argument is not an exact number, the exn:fail:contract exception is raised
~~ prints a tilde.
~‹w›, where ‹w› is a whitespace character (see char-whitespace?), skips characters in form until a non-whitespace character is encountered or until a second end-of-line is encountered (whichever happens first). On all platforms, an end-of-line can be #\return, #\newline, or #\return followed immediately by #\newline.
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.
> (fprintf (current-output-port) "~a as a string is ~s.\n" '(3 4) "(3 4)") (3 4) as a string is "(3 4)".
(let ([o (open-output-string)]) (fprintf o form v ...) (get-output-string o))
> (format "~a as a string is ~s.\n" '(3 4) "(3 4)") "(3 4) as a string is \"(3 4)\".\n"
parameter
(print-pair-curly-braces on?) → void? on? : any/c
parameter
(print-mpair-curly-braces on?) → void? on? : any/c
parameter
(print-unreadable on?) → void? on? : any/c
parameter
(print-graph) → boolean?
(print-graph on?) → void? on? : any/c
parameter
(print-struct) → boolean?
(print-struct on?) → void? on? : any/c
parameter
(print-vector-length on?) → void? on? : any/c
parameter
(print-hash-table on?) → void? on? : any/c
parameter
(print-boolean-long-form on?) → void? on? : any/c
parameter
(print-reader-abbreviations on?) → void? on? : any/c
parameter
(print-as-expression on?) → void? on? : any/c
parameter
→ (or/c +inf.0 0 (and/c exact-integer? (>/c 3))) (print-syntax-width width) → void? width : (or/c +inf.0 0 (and/c exact-integer? (>/c 3)))
parameter
→
(or/c (and/c path? complete-path?) (cons/c (and/c path? complete-path?) (and/c path? complete-path?)) #f) (current-write-relative-directory path) → void?
path :
(or/c (and/c path-string? complete-path?) (cons/c (and/c path-string? complete-path?) (and/c path-string? complete-path?)) #f)
procedure
(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)
procedure
(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)
procedure
(port-print-handler out)
→ ((any/c output-port?) ((or/c 0 1)) . ->* . any) out : output-port? (port-print-handler out proc) → void? out : output-port? proc : (any/c output-port? . -> . any)
A port print handler optionally accepts a third argument, which corresponds to the optional third argument to print; if a procedure given to port-print-handler does not accept a third argument, it is wrapped with a procedure that discards the optional third argument.
The default port display and write handlers print Racket expressions with Racket’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.
procedure
→ (->* (any/c output-port?) ((or/c 0 1)) any) (global-port-print-handler proc) → void?
proc :
(or/c (->* (any/c output-port?) ((or/c 0 1)) any) (any/c output-port? . -> . any))
A global port print handler optionally accepts a third argument, which corresponds to the optional third argument to print. If a procedure given to global-port-print-handler does not accept a third argument, it is wrapped with a procedure that discards the optional third argument.