Skip to content

Instantly share code, notes, and snippets.

@rgm
Last active January 4, 2016 13:19
Show Gist options
  • Save rgm/8627518 to your computer and use it in GitHub Desktop.
Save rgm/8627518 to your computer and use it in GitHub Desktop.
Floyd's algorithm for SICP exercise 3.19
(define (has-cycle? ls)
(define (has-cycle-h tortoise hare)
(cond ((null? hare) #f)
((null? (cdr hare)) #f)
((eq? (car tortoise) (car hare)) #t)
(else
(has-cycle-h (cdr tortoise) (cddr hare)))))
(cond ((null? ls) #f)
(else (has-cycle-h ls (cdr ls)))))
@rgm
Copy link
Author

rgm commented Jan 26, 2014

Purty. Note the complete absence of set!.

(Courtesy of http://en.literateprograms.org/Floyd's_cycle-finding_algorithm_(Scheme))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment