f#中不完整的模式匹配

时间:2016-02-04 01:25:21

标签: compilation f# f#-interactive f#-3.0 runtime-compilation

请考虑以下代码:

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和空格,但它在功能之后无法识别任何内容。

1 个答案:

答案 0 :(得分:3)

这只是一个警告:

如果你getThird3 [1;2],你会得到matchfailureexception。

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

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

| _::_::l3::t -> t;;
| _ -> failwith "not a long enough list"
相关问题