1

考虑以下代码:

let list1 = [1; 2; 3; 4; 5];;
let getThird3 = function 
  |[] ->[];
  | _::_::l3::t -> t;;
getThird3 list1;

当粘贴在运行 fsharpi 的终端上时,它给了我这个错误

> let list1 = [1; 2; 3; 4; 5];;

val list1 : int list = [1; 2; 3; 4; 5]

> let getThird3 = function 
-  |[] ->[];
-  | _::_::l3::t -> t;;

let getThird3 = function 
----------------^^^^^^^^

/Users/nickolasmorales/stdin(17,17): warning FS0025: Incomplete pattern matches on this expression. For example, the value '[_;_]' may indicate a     case not covered by the pattern(s).

val getThird3 : _arg1:'a list -> 'a list

有什么建议么?我尝试同时使用两者:仅 TAB 和空格,但在函数之后它不识别任何内容。

4

1 回答 1

3

这只是一个警告:

如果你这样做getThird3 [1;2],你会得到一个匹配失败异常。

警告必须选择特定的地方作为警告的基础,并且function可能与其他任何地方一样好。

要删除警告,我会将匹配更改为

| _::_::l3::t -> t;;
| _ -> failwith "not a long enough list"
于 2016-02-04T01:53:10.617 回答