previous up next     contents index
Next: Macros Up: Additional Facilities Previous: Output Printing

Graphs

 

        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 number n) and then referencing it later with #n#. For example, this S-expression descibes 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, this 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 S-expression as the tag. By default, MzScheme's printer will display without showing the shared structure:
 
  #((1 . 2) (1 . 2) (1 . 2)) 

Graph reading and printing are controlled with  read-accept-graph and  print-graph; these procedures take zero arguments to return the current mode, or one Boolean argument to set the mode. Graph reading is on by default, and graph printing is off by default. However, when the printer encounters an graph containing a   cycle, graph printing is automatically enabled (temporarily). When graph reading is off and a graph is provided as input, the  exn:read:unsupported exception is raised.



PLT