Ordered list in Scheme

Started by
11 comments, last by deffer 17 years, 10 months ago
Quote:Original post by petemurray
At the moment, it is separating the components i.e
(split (list -2 20 100 -5 20))
Gives: (list (list 20 100 20) -5 -2)


Actually, it's:
(cons (list 20 100 20) (list -5 -2))
which is exactly the same, it's just Dr.Scheme's interpreter that confuses matter.

Now sort each part recursively and merge the outputs.
Advertisement
Nothing seems to be working for me.
I just can't see the overall picture of how it works.

(define [split source]
(split2 source (first source) empty empty))

(define [split2 source elem lessList greqList]
(cond [(empty? source)
(append lessList greqList)]
[(< elem (first source))
(split2 (rest source)
(first source)
(cons (first source) lessList)
greqList)]
[else
(split2 (rest source)
(first source)
lessList
(cons (first source) greqList))]
))


I change elem to first source in the recursive tail part.
THought it worked at first...

(split (list -2 50 3))
gave me the result (50 3 -2)
and reversing it... (-2 3 50).

But no :(... more tests proved that it doesn't work. *sob*
i just can't see how to do it. and i thought i was getting the hang of things.. :(
Quote:Original post by deffer
quicksort  sourceList:   split sourceList into two parts (resLess, resGrEq),      which contain, accordingly, elements (<) and (>=) to (first sourceList)      do not put (first sourceList) on any of them! (might lead to infinite recursion)   append (resLess, (first sourceList), resGrEq);



I don't know how else I can put it.
In my oppinion, you should be able to get it from this description, especially that I already provided you with splitting routine.

Also, AP has already posted full working version, so I think it's time you think it out on your own.

Good luck!
~def

This topic is closed to new replies.

Advertisement