I want to make circular list with common lisp [dup

2019-03-01 11:42发布

问题:

This question already has an answer here:

  • Circular list in Common Lisp 3 answers

I want to make circular list with common lisp using rplaca or rplacd.

lambda (x) (cons (car x) (cons (rplacd (cdr x) (car x))))

I made code like above, but i think it is not what I want to. How I make circular list?

回答1:

First of all, you will always get a stack overflow when you try to print a circular object and *print-circle* is nil. So, start with

(setq *print-circle* t)

Now, there are many ways to create a circular list:

(defparameter *my-circular-list* (list t))
(setf (cdr *my-circular-list*) *my-circular-list*)
==> #1=(T . #1#)

Note how the circular list is printed with #= so that it can be read:

(defparameter *my-circular-list-1* '#1=(t . #1#))

Warning: (equal *my-circular-list* *my-circular-list-1*) will hang because it will infinitely descend into the circular structures.

You can also try this:

(setq *print-circle* nil
      *print-length* 4)
(print '#1=(a . #1#))
==> (A A A A ...)
(setq *print-length* 10)
(print '#1=(a . #1#))
==> (A A A A A A A A A A ...)


标签: common-lisp