I want to create function which sorts list. For example I have this list:
x1, x2, x3 .... xn
or
1, 2, 3, 4, 5, 6
Usually when we talk about sorting, we refer to ordering the items by some characteristic of the item contents, not the item position in the list. I would call your situation permuting, but perhaps some people might dispute that usage, too. :-)
Here's how you might approach the problem:
head
and tail
, if you want.tail
list, and interleave it with the head
list.Another approach:
rev
).rev
, keeping track of the element traversed each time. When they meet in the middle, stop.Here's a demonstration of the second approach (requires SRFI 1 to be loaded):
(define (zippy lst)
(if (null? lst)
'()
(let recur ((lst lst)
(rev (pair-fold cons '() lst)))
(cond ((eq? lst (car rev)) (list (car lst)))
((eq? (cdr lst) (car rev)) (list (car lst) (caar rev)))
(else (cons* (car lst) (caar rev)
(recur (cdr lst) (cdr rev))))))))