0

So I need to write a procedure to organize a list. Since its hard to explain here is the example:
List:
((72 75 0) (0 0 10) (255 255 255) (0 10 10) (0 10 8)(50 100 255))

Return:
((0 0 10) (0 10 8) (0 10 10) (50 100 255) (72 75 0) (255 255 255))

procedure is called (sort-pts color-list)

first you organize it by the first number in the pair, if those are equal you look at the second number in the pair, then if those are equal you look at the third number in the pair.
Although I am really struggling with the whole thing the main concept is how do you remake the list in the procedure.

Thanks

2
Contributors
1
Reply
3
Views
7 Years
Discussion Span
Last Post by Duoas
0

You need to write a lambda (you can either name one or use letrec) to compare two colors (proper lists of three numeric elements). Once done that you can sort your colors any of the usual ways.

Remember, you can build your list by adding elements either at the front or at the back. So a list like:
(1 9 7 2)
can be sorted: [b]([/b]sort '(1 9 7 2)[b])[/b] 1 (9 7 2) --> (1 < 9) --> (cons 1 [b]([/b]sort '(9 7 2)[b])[/b]) 9 (7 2) --> (9 > 7) --> (cons [b]([/b]sort '(7 2)[b])[/b] 9) 7 (2) --> (7 > 2) --> (cons [b]([/b]sort '(2)[b])[/b] 7)) 2 () --> 2 producing the result: (1 . ((2 . 7) . 9)) Now just flatten the pairs into a proper list and you are set.

(This is just one way to do it.)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.