我一直在使用Common-lisp(SBCL)中的循环列表,并在尝试调用REDUCE这样的列表时遇到以下问题.
首先,我们创建一个列表:
CL-USER> (defvar *foo* (list 1 1 1 1))
*foo*
Run Code Online (Sandbox Code Playgroud)
当然,现在我们可以做到
CL-USER> (reduce #'+ *foo*)
4
Run Code Online (Sandbox Code Playgroud)
要么
CL-USER> (reduce #'+ *foo* :end 3)
3
Run Code Online (Sandbox Code Playgroud)
但是,如果我们创建一个循环列表:
CL-USER> (setf *print-circle* t)
CL-USER> (setf (cdr (last *foo*)) *foo*)
CL-USER> *foo*
#1=(1 1 1 1 . #1#)
Run Code Online (Sandbox Code Playgroud)
显然(reduce #'+ *foo*)现在永远不会回来.
但是当我尝试的时候
CL-USER> (reduce #'+ *foo* :end 3)
...
Run Code Online (Sandbox Code Playgroud)
我也有一个无限循环.
为什么会这样?有没有办法解决这个问题,而没有明确使用循环结构,如LOOP或DO?我正在使用SBCL,但尝试使用其他实现(CLISP,ECL),它们都有同样的问题.