Next: Compilation Up: Support Facilities Previous: Output Printing

# Data Sharing in Input and Output

MzScheme can read and print graphs, S-expressions with shared structure (e.g., a cycle). Graphs are described by tagging the shared structure once with `#n=` (using some decimal integer n with no more than eight digits) and then referencing it later with `#n#` (using the same number n). For example, the following S-expression describes the infinite list of ones: ``

      #0=(1 . #0#)

If this graph is entered into MzScheme's `read`-`eval`-`print` loop, MzScheme's compiler will loop forever, trying to compile an infinite expression. In contrast, the following expression defines `ones` to the infinite list of ones, using `quote` to hide the infinite list from the compiler: `

      (define ones (quote #0=(1 . #0#)))

` A tagged structure can be referenced multiple times. Here, `v` is defined to be a vector containing the same `cons` cell in all three slots: `

      (define v #(#1=(cons 1 2) #1# #1#))

` A tag `#n=` must appear to the left of all references `#n#`, and all references must appear in the same top-level S-expression as the tag. By default, MzScheme's printer will display a value without showing the shared structure: `

      #((1 . 2) (1 . 2) (1 . 2))

`

Graph reading and printing are controlled with the `read-accept-graph` and `print-graph` Boolean parameters (see section 9.4.1.4). Graph reading is enabled by default, and graph printing is disabled by default. However, when the printer encounters an graph containing a cycle, graph printing is automatically enabled (temporarily). When graph reading is disabled and a graph is provided as input, the `exn:read` exception is raised.

If the n in a `#n=` form or a `#n#` form contains more than eight digits, the `exn:read` exception is raised. If a `#n#` form is not preceded by a `#n=` form using the same n, the `exn:read` exception is raised. If two `#n=` forms are in the same expression for the same n, the `exn:read` exception is raised.

Next: Compilation Up: Support Facilities Previous: Output Printing