小编job*_*ach的帖子

减少Common Lisp中的循环列表

我一直在使用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)

我也有一个无限循环.

为什么会这样?有没有办法解决这个问题,而没有明确使用循环结构,如LOOPDO?我正在使用SBCL,但尝试使用其他实现(CLISP,ECL),它们都有同样的问题.

reduce list sbcl common-lisp circular-list

3
推荐指数
1
解决办法
170
查看次数

标签 统计

circular-list ×1

common-lisp ×1

list ×1

reduce ×1

sbcl ×1