问题
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)
)
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.
What lisp implementations do you know that does support this (freeware, os independent)?
回答1:
All lisp implementations, including clisp
, support circular lists.
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#)
See also function LIST-LENGTH
来源:https://stackoverflow.com/questions/15536564/lisp-cyclic-lists