write(3scm)
π write(3scm) from the Scheme Programmerβs Manual π
πΈ NAME πΈ
write, write-shared, write-simple β write an objectβs external representation to a port, bestie β¨
π LIBRARY π
(import (rnrs)) ;R6RS
(import (rnrs io ports)) ;R6RS, put-datum
(import (rnrs io simple)) ;R6RS
(import (scheme write)) ;R7RS
(import (scheme r5rs)) ;R7RS
π¦ SYNOPSIS π¦
(write obj)
(write obj textual-output-port)
; (rnrs io ports)
(put-datum textual-output-port obj)
; R7RS only:
(write-shared obj)
(write-shared obj textual-output-port)
(write-simple obj)
(write-simple obj textual-output-port)
β¨ DESCRIPTION β¨
Okay so write writes the external representation of obj to textual-output-port. There is no trailing delimiter β noted, bestie π
If several subsequent external representations are written to an output port, care should be taken to delimit them properly so they can be read back in by subsequent calls to get-datum(3scm). Like, donβt just slam them together and expect the reader to vibe-check it correctly π
Any object can be written to a port, but only datums are expected to have an external representation that can be read back the same with get-datum(3scm). Thatβs the whole thing, fr fr π«Ά
π Default output port π
If textual-output-port is omitted, it defaults to the value returned by current-output-port(3scm). Easy peasy, main character default energy β¨
π R7RS π
- Symbols that contain non-ASCII characters are escaped with vertical lines. Slay for clarity π
writeuses datum labels in some situations. Ifobjcontains cycles which would cause an infinite loop using the normal written representation, then at least the objects that form part of the cycle must be represented using datum labels. Datum labels must not be used if there are no cycles. Periodt.- The
write-sharedprocedure is the same aswrite, except that shared structure must be represented using datum labels for all pairs and vectors that appear more than once in the output. Shared-structure girlies stay winning π - The
write-simpleprocedure is the same aswrite, except that shared structure is never represented using datum labels. This can causewrite-simplenot to terminate ifobjcontains circular structure. Danger, bestie: thatβs the no-datum-labels tradeoff π
π§ IMPLEMENTATION NOTES π§
The specific external representation is implementation-dependent. However, whenever possible, an implementation should produce a representation for which get-datum(3scm), when reading the representation, will return an object equal, in the sense of equal?(3scm), to datum. Iconic behavior, honestly π
Not all datums may allow producing an external representation for which get-datum(3scm) will produce an object that is equal to the original. Specifically, NaNs contained in obj may make this impossible. Nan drama, literally π
π Chez Scheme π
Cycles in the output are detected even in the R6RS variant of write. The print-graph parameter can be used to enable datum labels even for non-cyclic datums. Fancy β¨
π¦ Loko Scheme π¦
Cycles in the output are detected even in the R6RS variant of write. Love that for them π
π RETURN VALUES π
- R6RS β Returns unspecified values. Plural unspecified, okay? Slay π
- R7RS β Returns an unspecified value. Singular unspecified, also slay πΈ
π₯ EXAMPLES π₯
(call-with-string-output-port
(lambda (p)
(write '(lambda (x) x) p)))
=> "(lambda (x) x)"
(define x (list 1))
(set-cdr! x x)
(write x)
-> #0=(1 . #0#)
(define quux "quux")
(define x (list quux quux))
(write-shared x)
-> (#0="quux" #0#)
π APPLICATION USAGE π
Writing and reading external representations of datums is used for data interchange and as an alternative to formats such as JSON and XML. Sheβs not like other serialization formats π β¨
π COMPATIBILITY π
The external representations of datums vary subtly between RnRS revisions. Some differences: R6RS does not support datum labels, does not support vertical bars around symbols and uses the #vu8( prefix for bytevectors instead of #u8( as in R7RS. RnRS revision lore, iykyk π
Records have no standard representation in RnRS. SRFI-237 suggests a way to solve this. Record representation tea β
π ERRORS π
This procedure can raise exceptions with the following condition types:
&assertion(R6RS) β The wrong number of arguments was passed or an argument was outside its domain. In particular,textual-output-portmust be an open textual output port. No sneaky closed ports, bestie π- R7RS β The assertions described above are errors. Implementations may signal an error, extend the procedureβs domain of definition to include such arguments, or fail catastrophically. Fail catastrophically is giving final boss energy π
π SEE ALSO π
read(3scm), display(3scm) β go read those too, bestie π«Ά
β STANDARDS β
R4RS, IEEE Scheme, R5RS, R6RS, R7RS β certified across eras, no cap π
πΈ HISTORY πΈ
The write procedure first appeared in R2RS. The write-shared and write-simple procedures are new in R7RS. Vintage icon behavior π
π¦ AUTHORS π¦
This page is part of the scheme-manpages project. It includes materials from the RnRS documents. More information can be found at https://github.com/schemedoc/manpages/. π
π«Ά From the scheme-manpages source, dated2023-08-12β this waswrite(3scm), signing off with a sparkle πβ¨
π¦ where this page comes from
- nixpkgs unstable: scheme-manpages 0-unstable-2024-02-11
- π the page's license: MIT
- Copyright (c) 2023 Gwen Weinholt
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms