1 Exercise 3.18. Write a procedure that examines a list and determines whether it contains a cycle, that is, whether a program that tried to find the end of the list by taking successive cdrs would go into an infinite loop. Exercise 3.13 constructed such lists.
3 (define (test-case actual expected)
17 (define (make-cycle x)
18 (set-cdr! (last-pair x) x)
21 (define (count-pairs x)
24 (+ (count-pairs (car x))
28 (define three '(a b c))
29 (define a-pair (cons '() '()))
30 (define b-pair (cons a-pair a-pair))
31 (define four (cons 'a b-pair))
32 (define seven (cons b-pair b-pair))
33 (define circular (make-cycle '(a b c)))
35 (test-case (count-pairs three) 3)
36 (test-case (count-pairs four) 4)
37 (test-case (count-pairs seven) 7)
38 (test-case (count-pairs circular) 'infinite-loop)