Search code examples
schemehashtagside-effects

Scheme what does #= as output mean?


Hi I'm trying to learn Scheme and I was working on an example from a university website:

https://courses.cs.washington.edu/courses/cse341/05au/lectures/scheme-side-effects.html

The example is something along these lines:

(define circ '(a b))

(set-cdr! (cdr circ) circ)

and this is the output:

=> #0=(a b . #0#)

I don't understand what this means. The code defines a variable called circ as a list with 2 elements (a b).

set-cdr! mutates the cdr of this list [which is (b '())] and changes it to circ (which is (a b)).

So the output I expected here was (a (a b)) but I got this weird hashtag thing instead.

I'm using DrRacket IDE with R5RS scheme set as the language.

What does this hashtag stuff mean? Is it maybe creating a pointer to itself like (a [pointer to circ]) in which case it would be like some kind of infinite loop or something?

I mean if I do this:

(define x '(a b))

(set-cdr! x 'c)

x

=>(a . c) ; is the output

this is easy to understand as set-cdr! replaces the (b '()) with 'c and getting rid of the '() at the end is why I get a dotted pair back instead of a list. But this isn't in-line with the earlier example.

anyway if anybody cares to fill me in, let me know. Thanks in advance.


Solution

  • You are correct in thinking that the operation is the creation of a circular list.

    Drawing a list through its cons cells, this is the situation after the define:

    enter image description here

    and this is the situation after the set-cdr!:

    enter image description here

    Note that the modification is on the cdr of the cdr of circ (so on the cdr of the second cell). The notation #0=(a b . #0#) in lisp languages describes an improper list where the last cdr is equal to the list itself, producing a circular data structure (i.e. a data structure with a “loop”).