I have been trying to make a procudure to sort a list of lists into
ascending numberical value. For example, if the list of lists is [[0 1]
[1 1] [0 3] [4 0]] I would like the outcome to be [[4 0] [0 3] [1 1] [0
1]] or [[0 1] [1 1] [0 3] [4 0]] - it doesn't matter.
The procedure I have written below doesn't actaully run, but I thoght it
might let you know how I am attempting to do this.
In plain English, what I am trying to do is:
1) take the head and the head and tail of a list, compare them and add
the bigger to a new list.
2) delete the item added to the new list from the old list and repeat
the process until the new head of the list has been compared to
something. Original list will not be empty and the new one will be full.
The full list should be copied inot the empty one sothe process can be
started again.
3) Repeat that process again until the elements are in correct order.
4) That's about it.
Unfourtantly it won't run. I'm worried that it may be failing when it
gets to the last element in a list and attempts to compare it, the head,
with the head of the nonexistant tail.
Am I going about this the right way? A friend reccomended I used the
syssort command, but it seems quite complicated.
Thanks for any help you can give!
David FitzGerald.
define heur_crossing_list(crossing_list) -> heur_list;
lvars heur_list;
lvars heur_list2;
lvars templist;
crossing_list -> templist;
lvars sublist1;
lvars sublist2;
repeat length(crossing_list) times
repeat length(crossing_list) times
hd(templist) -> sublist1;
hd(tl(templist)) -> sublist2;
more(sublist1, sublist2) -> big;
delete(big, templist);
big <> heur_list2 -> heur_list2;
endrepeat;
heur_list2 -> templist;
endrepeat;
enddefine;
define more(sublist1,sublist2)->bigger;
if addup(sublist1) > addup(sublist2)
then sublist1 -> bigger
else sublist2 -> bigger
endif;
enddefine;
define addup(list)-> sum;
if list = []
then 0 -> sum
else hd(list) + addup(tl(list)) -> sum
endif
enddefine;
I've been calling it using :
heur_crossing_list([[1 4] [1 2] [1 6] [1 3] [1 1] [1 5]]) ->heur_list =>
Thanks again.
David.
|