Skip to content

Instantly share code, notes, and snippets.

@dingbat
Created November 25, 2012 06:42
Show Gist options
  • Select an option

  • Save dingbat/4142657 to your computer and use it in GitHub Desktop.

Select an option

Save dingbat/4142657 to your computer and use it in GitHub Desktop.
(define (uncycle lst) (uncycle2 lst lst)) ; recursive handle
(define (uncycle2 lst orig) (if (null? lst)
'() ; means it wasn't actually a cycle, but we forgive
(if (eq? (cdr lst) orig) ; base case. (if the cdr is the list itself)
(list (car lst)) ; return just the element previous
(cons (car lst) ; otherwise, recurse. add the prev element
(uncycle2 (cdr lst) orig))))) ; onto the uncycle'd next part of the list
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment