How scheme code usually deal with cycles in list processing recursive functions?

自作多情 提交于 2020-03-28 06:39:48

问题


My question was marked as duplicate, and closed

How to deal with cycles in recursive functions in scheme?

I'm asking again:

I have this code:

(define x (list 1 2 3))
(set-cdr! (cddr x) x)
(define (list? x)
  (and (pair? x) (or (null? (cdr x)) (list? (cdr x)))))

(display (list? x))
(newline)

and function list? freezes when it find cycle (because it's infinite loop). How scheme code usually work with cycles like that? I'm not asking how to detect cycles. I'm asking: how this is done in scheme code?

来源:https://stackoverflow.com/questions/60809809/how-scheme-code-usually-deal-with-cycles-in-list-processing-recursive-functions

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