Skip to content

Instantly share code, notes, and snippets.

@calebsmith
Created November 10, 2015 02:08
Show Gist options
  • Save calebsmith/e30e6e863a575cef1fd9 to your computer and use it in GitHub Desktop.
Save calebsmith/e30e6e863a575cef1fd9 to your computer and use it in GitHub Desktop.
Floyd's Cycle Detection Algorithm
(define (contains-cycle? lst)
(let inner ((a (nil-safe-cdr lst))
(b (nil-safe-cdr (nil-safe-cdr lst))))
(cond ((or (not (pair? a))
(not (pair? b))) #f)
((or (eq? a b)
(eq? a (nil-safe-cdr b))) #t)
(else (inner (nil-safe-cdr a) (nil-safe-cdr (nil-safe-cdr b)))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment