huangzworks / SICP-answers

我的 SICP 解题集
http://sicp.readthedocs.org/
882 stars 183 forks source link

3.18 exist a bug #63

Closed INotWant closed 4 years ago

INotWant commented 4 years ago

The following cases are not considered:

(define l2 (list 1 2))
(set-car! (last-pair l2) l2)

My solution:

(define (have-cycle? x)
    (define (help x path)
        (if (not (pair? x))
            #f
            (or 
                (not (false? (memq x path)))
                (help (car x) (cons x path))
                (help (cdr x) (cons x path)))))
    (help x '()))
INotWant commented 4 years ago

sorry, I read the question wrong!