Example of Sharpsign Equal-Sign reader macro?

风流意气都作罢 提交于 2019-12-04 15:59:06

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

This way you can label an object in some s-expression and refer to it in a different place in the s-expression.

The label is #someinteger= followed by an s-expression. The integer must be unique. You can't use the label twice within a single s-expression.

The reference to a label is #someinteger#. The integer identifies the s-expression to reference. The label must be introduced, before it can be referenced. The reference can be used multiple times within an s-expression.

This is for example used in reading and printing circular lists or data structures with shared data objects.

Here a simple example:

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

reads as

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

Note also this:

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

It is one identical cons cell.

Let's try a circular list.

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

? (setf *print-circle* t)
T

Now we are constructing a list:

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

We are setting the last cdr to the first cons:

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

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

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

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

Since we have told the printer to deal with such constructs, we can try the expression from the first example:

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

Now the printer detects that there are two references to the same cons object.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!