开发者

Saving the index from a real list

I'm trying to declare a function sort : real list -> int list * real list that sorts the list using mergesort and also returns an int list with the original positions of the individual numbers pre-sort.

Maybe this example will make it easier to understand:

sort [5.4,7.2,1.5,9.6] = ([2,0,1,3], [1.5,5.4,7.2,9.6]

Now the sorting of the list is pretty easy, but I'm having a hard time figuring out how to get it to remember the original position and then make an int list with it.

开发者_如何学编程

Help?


  1. Create a list of pairs that contains each item of the original list with its index (you can use ListPair.zip with a list of the indices (that you can create with List.tabulate), to do this).
  2. Sort that list.
  3. Use ListPair.unzip to turn the sorted list of pairs into a pair of lists.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜