我有一个返回数组的最小值的函数。模式匹配数组
功能有类型:
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。数组是通过索引直接访问的。如果您愿意,可以将数组转换为列表。 –