我有这两个功能:
let print_length = function
| [] -> Printf.printf "The list is empty"
| xs -> Printf.printf "The list has %d elements" (List.length xs)
let print_length = function
| [] -> Printf.printf "The list is empty"
| (_ :: _) as xs -> Printf.printf "The list has %d elements" (List.length xs)
在实践中,它们的行为是相同的,理论上它们应该是相同的,只要案例按顺序匹配即可。但这在 OCaml 中有保证吗?如果一些较新版本的编译器开始通过重新排列顺序来优化匹配语句怎么办?在这种情况下,只有第二个版本会产生正确的结果。我应该担心这个吗?