4

Looking at this, I see

(setq x '#1=(a #1#))

but this evaluates to (a #0). As I understand, this should be (a #1=(a #1#)). But then what syntax makes #0 mean #1=(a #1#)? In general what is # doing in the original setq form, then the returned list (a #0)?

Playing around, I see (cadr (cadr (cadr x))) always returns (a #0) which makes sense, but deepens the mystery.

4

The lisp printer needs to be set up to print Circular Objects. To do so you need to eval:

(setq print-circle t)

Then the output of eval (setq x '#1=(a #1#)) will print as #1=(a #1#) (as expected).

Read more at the Manual


If you want to output and re-read Circular Objects, it would be unwise to unroll them while printing, because this would result in an infinite loop. So there has to be some syntactic expression, for how to print and read Circular Objects.

Within Common Lisp # often starts a reader macro, which changes the input before the compiler/interpreter sees it. (Some sort of a very sophisticated preprocessor from a C programmers perspective). With that it is possible to transform those expressions into memory objects, i.e. connect last list element with first.

ELisp has no reader macros, but I guess this special case is implemented somewhere in Emacs' C code. Why Elisp does not detect Circular Obejcts by default, I do not know. Probably some legacy and performance reasons.

| improve this answer | |
  • Yes, I saw that (setq print-circle t), but once again, I had no idea what they were talking about. Learning Emacs has been an endless "guess-and-test" slog. Thanks. – 147pm Feb 27 at 15:26
  • @147pm the wiki is an insufficient source to learn from. But the Emacs and Elisp manuals are not soo bad. Also, Elisp is pretty similar to Common Lisp, so you could learn from there, too. – jue Feb 27 at 20:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.