Why does a set (or list) in Racket print C # 0 # as a single piece of data?

I am writing a Racket program that makes extensive use of kits. Considering the output of the program, some of the sets include # 0 # as the only piece of data. What causes this?

+5
source share
1 answer

#0# used in a Racket printer to describe cyclic data structures or shared memory objects. Basically, the way it works is that there is an object that is tagged, say, #0= , and then when you see #0# , it refers to that object.

So for example:

 #0=(1 . #0#) 

A list of infinite length containing only 1 s. He does this because the created data structure is only a pair of minus, where the first element is 1 and the second element points to itself.

You can have any number of them to create more complex chart structures. For instance:

 #0=(#1=(1 . #0#) . #1#) 

Finally, you do not have to have a circular data structure. Suppose x was the following data structure:

 '(#0=#&42 . #0#) 

(wehre #& is a mutable field), mutation of an element in the first element of a pair additionally mutates an element in the second part of a pair. So, if this pair was set to the variable x , then:

 > x '(#0=#&42 . #0#) > (set-box! (car x) 43) > (car x) '#&43 > (cdr x) '#&43 > x '(#0=#&43 . #0#) 

Usually you cannot write this directly to code directly in the code, but you can build it with read .

 > (define x (read) #0=(1 . #0#) > x #0='(1 . #0#) > (car x) 1 > (cadr x) 1 > (cdr x) #0=1(1 . #0#) 

You can also change the reader to just put it in the code directly from read-accept-graph when reading in the module source code.

+5
source

All Articles