Determine if a loop is in the list

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.


There are no comments yet.

Authentication required

You must log in to post a comment.

Login
(define (last-pair x)
  (if (null? (mcdr x))
      x
      (last-pair (mcdr x))))

(define (make-cycle x)
  (set-mcdr! (last-pair x) x)
  x)

(define z (make-cycle (mlist 'a 'b 'c)))


(check-equal? (cycle? z) #t)
(check-equal? (cycle? (mlist 1 2 3)) #f)