我想用普通的Lisp制作清单 [英] I want to make circular list with common lisp
问题描述
我想使用rplaca或rplacd用通用Lisp制作循环列表。
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?
推荐答案
首先,当您尝试执行以下操作时,总是会出现堆栈溢出打印圆形对象,然后 * print-circle *
是 nil
。因此,从
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#))
警告:(等于* my-circular-list * * my-circular-list-1 *)
会挂起,因为它会无限下降到圆形结构中。
Warning: (equal *my-circular-list* *my-circular-list-1*)
will hang because it will infinitely descend into the circular structures.
您也可以尝试一下:
(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 ...)
这篇关于我想用普通的Lisp制作清单的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!