Skip to content

Instantly share code, notes, and snippets.

@greghendershott
Last active November 6, 2015 15:31
Show Gist options
  • Save greghendershott/a0810b16b2127039b77d to your computer and use it in GitHub Desktop.
Save greghendershott/a0810b16b2127039b77d to your computer and use it in GitHub Desktop.
;; A "Rackety" version of http://stackoverflow.com/a/33557880/343414
#lang racket
(require rackunit)
(define (unwind xs)
(let loop ([right '()] [turtle xs] [hare xs])
(match* (hare turtle)
[((list) ts ) (interleave right ts)]
[((list x) (cons t ts)) (cons t (interleave right ts))]
[((list* _ _ hs) (cons t ts)) (loop (cons t right) ts hs)])))
(define (interleave xs ys)
(match* (xs ys)
[((list) ys ) ys]
[(xs (list) ) xs]
[((cons x xs) (cons y ys)) (cons x (cons y (interleave xs ys)))]))
(check-equal? (unwind '())
'())
(check-equal? (unwind '(1 2 3))
'(2 1 3))
(check-equal? (unwind '(1 1 2 3 5 8 13))
'(3 2 5 1 8 1 13))
(check-equal? (unwind '(7 8 2 9 5 6))
'(2 9 8 5 7 6))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment