This is not a sort operation, more like a shuffle; here is another way to solve it. First, define an interleave procedure that alternates between two lists, returning a single list:
(define (interleave l1 l2) (cond ((empty? l1) l2) ((empty? l2) l1) (else (cons (first l1) (interleave l2 (rest l1))))))
Now we take the initial list and split-at in the middle (this is the procedure corresponding to Racket); finally, we alternate the two resulting lists, reversing the tail:
(define (zippy lst) (let-values (((head tail) (split-at lst (quotient (length lst) 2)))) (interleave head (reverse tail))))
The above IMHO implementation is a little intuitive, and if you work with Racket, it does not require external libraries. It works as expected:
(zippy '(1 2 3 4 5 6)) => '(1 6 2 5 3 4)
source share