我在List上有一個非常簡單的MergeSort實現。MergeSort函數爲什麼會發生數值限制?
/// Divide the list into (almost) equal halves
let rec split = function
| [] -> [], []
| [x] -> [x], []
| x1::x2::xs -> let xs1, xs2 = split xs
x1::xs1, x2::xs2
/// Merge two sorted lists
let rec merge xs ys =
match xs, ys with
| [], _ -> ys
| _, [] -> xs
| x::xs', y::ys' when x <= y -> x::merge xs' ys
| _, y::ys' -> y::merge xs ys'
let rec mergeSort = function
| [] -> []
| xs -> let xs1, xs2 = split xs
merge (mergeSort xs1) (mergeSort xs2)
但每當我試圖與在F#互動任何輸入測試:
let xs = mergeSort [1;4;3;2];;
我遇到了一個值限制錯誤:
error FS0030: Value restriction. The value 'xs' has been inferred to have generic type val xs : '_a list when '_a : comparison Either define 'xs' as a simple data term, make it a function with explicit arguments or, if you do not intend for it to be generic, add a type annotation.
它爲什麼會發生?什麼是解決它的簡單方法?
參見[F#值的限制的細點](http://blogs.msdn.com/b/mulambda/archive/2010/05/01/value-restriction-in-f.aspx) –
當我將代碼粘貼到'FSI'中,我沒有在'F#2.0 Interactive build 2.0.0.0'上得到錯誤消息 –
@JohnPalmer:當然不是。嘗試在某些輸入上執行該功能。 – pad