Lisp循环列表 [英] Lisp cyclic lists
问题描述
我们从Lisp那里得到了作业,我需要使用循环"列表(我不知道什么是正确的命名方式). 循环"列表是指列表,其中最后一个cons的cdr
指向同一列表的第一个.
We have been given homework from lisp where I need to use "cyclic" list (I don't know what is the right naming for this). By "cyclic" list, I mean list, where cdr
of the last one cons points to the very first one of the same list.
(Value1 . PointerValue2) (Value2 . PointerValue3) (Value3 . PointerValue1)
我们被教导要使用以下方法创建此类列表:
We have been taught to create such a lists with:
(defun cykl (l)
(setf (cdr (last l)) l)
)
我使用的Lisp软件( Lispbox )不支持这种列表.我也曾在Debian上尝试过
The Lisp software (Lispbox) I use does not support this kind of lists. I have also tried clisp on Debian but it has crashed after creating such a list.
您知道哪些支持Lisp的实现(免费软件,操作系统无关)吗?
What lisp implementations do you know that does support this (freeware, os independent)?
推荐答案
All lisp implementations, including clisp
, support circular lists.
当您说崩溃"时,您可能是指堆栈溢出错误(或内存不足错误),当您尝试打印(记住"read-eval-PRINT"循环?)时,您总是会得到此错误;当 *print-circle*
是nil
.将其设置为t
会强制Lisp使用#n#表示法:
When you say "crashed", you probably mean a stack overflow error (or an out-of-memory error), which you will always get when you try to print (remember the 'read-eval-PRINT' loop?) a circular structure when *print-circle*
is nil
. Setting it to t
forces Lisp to use the #n# notation:
[1]> (defparameter l (list 1 2 3))
L
[2]> l
(1 2 3)
[3]> (setq *print-circle* t)
T
[4]> (setf (cdr (last l)) l)
#1=(1 2 3 . #1#)
另请参见函数列表长度
这篇关于Lisp循环列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!