I want to make circular list with common lisp [dup

2019-03-01 11:48发布

This question already has an answer here:

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?

标签: common-lisp
1条回答
Lonely孤独者°
2楼-- · 2019-03-01 12:20

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 ...)
查看更多
登录 后发表回答