Exercise3-18 <---> Exercise3-20
Exercise 3.19
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.
Scheme solution:
(define (is-cycle? l)
(define (test-cycle first second)
(cond ((eq? first '()) #f)
((eq? second '()) #f)
((eq? first second) #t)
((eq? (cdr first) '()) #f)
((eq? (cdr second) '()) #f)
(else
(test-cycle (cdr first) (cddr second)))))
(cond ((eq? l '()) #f)
((eq? (cdr l) '()) #f)
(else (test-cycle l (cdr l)))))
Exercise3-18 <---> Exercise3-20