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


Comments


:) :)) :( ;) :\ |) X-( B) Markup

Exercise3-19 (last edited 2010-04-20 20:56:15 by SriramDevadas)