Friday, July 24, 2009

Ex-3.18

(define (has-cycle? list)
(define (already-in-list? x l)
(cond
((null? l) #f)
((eq? (car l) x) #t)
(else (already-in-list? x (cdr l)))))
(let ((cache nil))
(define (has-cycle-aux? list)
(cond
((null? list) #f)
((already-in-list? (car list) cache)
#t)
(else
(set! cache (cons (car list) cache))
(has-cycle-aux? (cdr list)))))
(has-cycle-aux? list)))

No comments:

Post a Comment