Search code examples
lispcircular-list

Why order of quote character "'" in relation to '#1=' matters in circular lists?


I am experimenting with circular lists and have written a flawed graph viewer. So when I try to draw 2 different graphs

(graph-viewer:graph '#1=(1 2 3 #1#) "a")
(graph-viewer:graph #1='(1 2 3 #1#) "b")

I get who different pictures with the latter version having a quote symbol included in the graph.


Solution

  • You need to think how the reader does its job.

    When it sees #1=, it knows to store whatever comes next and re-use it on #1# - think about it as a "binding" for the "variable 1".

    When it sees '<blah>, it reads it as (quote <blah>).

    Thus, when it sees '#1=(1 2 3 #1#), it reads it as

    (quote (1 2 3 *))
           ^      |
           |      |
           +------+
    

    (quote is outside of "binding of 1")

    while #1='(1 2 3 #1#) is read as

    (quote (1 2 3 *))
    ^             |
    |             |
    +-------------+
    

    (quote is inside the "binding of 1").

    The "arrow" in the pics above is the reference, i.e., * points along the arrow.