Lisp cyclic lists [closed]

别来无恙 提交于 2019-12-13 09:23:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!