Standard sorting functions in SML?

后端 未结 5 732
夕颜
夕颜 2020-12-11 02:21

Are there standard sorting functions in SML? The documentation on the Internet is so scarce I couldn\'t find any.

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-11 03:21

    Here is a standard quicksort

    fun qsort(func) =
        let fun sort [] = []
              | sort (lhd :: ltl) =
                    sort (List.filter (fn x => func (x, lhd)) ltl)
                  @ [lhd]
                  @ sort (List.filter (fn x => not (func(x, lhd)) ltl)
        in sort
        end
    

    Just throw in some comparator (function that takes two elements with same type and return boolean) and it will return a sort function for you If you got anymore question don't hesitate to ask. :)

提交回复
热议问题