I would like some feedback on the appropriate way of ordering a numerical list using the most elementary scheme
functions (sorry, i realise that elementary is not well defined).
By way of background: I've been learning a bit of scheme
on my ipad, where I have only a basic implementation -- this is a nice challenge as much as a problem, as i have to figure out simple things like sorting a list for myself.
In doing this, i learnt that (append (cdr list) (car list))
is not what you might expect -- which is how i had originally wanted to implement this simple sort.
To hack around it, i discovered list-tail
and reverse
-- see the else
clause below.
(define ra-sort
(lambda (numList)
(cond
((null? numList) '())
((= (car numList) (apply max numList))
(cons (car numList) (ra-sort (cdr numList))))
(else (ra-sort (append (list-tail numList (- (length numList) 1))
(reverse (list-tail (reverse numList) 1)))))
)))
(define a '(99 1 6 12 101 2 67 -3))
> (ra-sort a)
(101 99 67 12 6 2 1 -3)
Now this seems to work, but all this repeated calling of reverse
seems inefficient -- how should it be done?
Or am i missing something? Is it possible to make things work with the output of (append (cdr a) (car a))
?
what does the notation even mean in (1 6 12 101 2 67 -3 . 99)
? and how is it useful?