【发布时间】:2015-11-06 12:02:52
【问题描述】:
我在 ocaml 中创建了一个函数(合并排序),但是当我使用它时,列表是倒置的。
另外,我想计算系统进行计算所花费的时间,我该怎么做呢?
let rec merge l x y = match (x,y) with
| ([],_) -> y
| (_,[]) -> x
| (h1::t1, h2::t2) ->
if l h1 h2
then h1::(merge l t1 y)
else h2::(merge l x t2);;
let rec split x y z = match x with
| [] -> (y,z)
| x::resto -> split resto z (x::y);;
let rec mergesort l x = match x with
| ([] | _::[]) -> x
| _ -> let (pri,seg) = split x [] []
in merge l (mergesort l pri) (mergesort l seg);;
mergesort (>) [2;6;1;8];;
- : int list = [8; 6; 2; 1]
【问题讨论】: