我有一個返回數組的最小值的函數。模式匹配數組
功能有類型:
min : int array -> int
它的實現:
let rec min a = match a with
| [] -> 1000000000
| x :: [] -> x
| x :: xs -> let ms = min xs in if x < ms then x else ms;;
不過,我得到這個錯誤:
Found min with unexpected type:
Wrong type int list -> int.
所以,我怎麼能匹配模式的數組?
「1000000000」會給某些輸入提供不正確的結果,您應該找到一種不涉及幻數的方法。 – coredump
您正在以錯誤的方式訪問IMO。您可以使用數組語法對陣列進行模式匹配。但是這裏沒有head :: tail pattern。數組是通過索引直接訪問的。如果您願意,可以將數組轉換爲列表。 –