Macro sample reader with an even Sharpign badge?

I saw that it was used once, but could not understand what he was doing. The link says that it is

#n=object reads as any object has an object as its printed representation. However, this object is marked with n, an unsigned decimal integer is required, for possible reference syntax # n #. Label volume is an expression that is read by the most external call to read; in this expression, the same label may not be displayed twice.

What reads to me like 56 randomly selected English words ... Can you show an example of when this can be used?

+2
lisp common-lisp
Sep 29
source share
1 answer

In Common Lisp, it is used by the reader and printer.

Thus, you can mark an object in some s-expression and refer to it elsewhere in the s-expression.

Label #someinteger= followed by an s-expression. An integer must be unique. You cannot use a label twice in the same s-expression.

Link to the label #someinteger# . An integer identifies the s expression for the reference. A label must be entered before it can be referenced. A link can be used several times in an s-expression.

This, for example, is used to read and print circular lists or data structures with shared data objects.

Here is a simple example :

 ? '(#1=(1 . 2) (#1#)) 

read like

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

Pay attention also to this:

 ? (eq (first *) (first (second *))) T 

This is one identical cons cell.

Try the circular list .

Make sure the printer deals with circular lists and does not print them forever ...

 ? (setf *print-circle* t) T 

Now we create a list:

 ? (setf l1 (list 1 2 3)) (1 2 3) 

We install the last cdr in the first cons:

 ? (setf (cdr (last l1)) l1) #1=(1 2 3 . #1#) 

As you can see above, the printed list gets the label, and the last cdr is a link to this label.

We can also enter a circular list directly using the same notation. The reader understands this:

 ? '#1=(1 2 3 . #1#) #1=(1 2 3 . #1#) 

Since we said that the printer deals with such constructions, we can try the expression from the first example:

 ? '(#1=(1 . 2) (#1#)) (#1=(1 . 2) (#1#)) 

The printer now detects two references to the same cons object.

+8
Sep 29 2018-12-12T00:
source share



All Articles