(define depth-count
(lambda (l)
(let ((visited '())
(counter 0))
(let iter ((l l))
(cond ((pair? l)
(if (memq l visited)
(set! counter (+ 1 counter))
(begin
(set! visited (cons l visited))
(iter (car l))
(iter (cdr l)))))
(else '()))) counter)))
恕我直言,那个 else 分支是不必要的或只是错误的,但是该代码似乎可以工作,但我不确定..当我有..让我们说
(define l0 '(a b c))
(set-car! l0 l0)
(set-car! (cdr l0) l0)
(depth-count l0)
它应该返回 2,对吧?那是正确的吗?