Nickolas Morales
Nickolas Morales

Reputation: 55

Incomplete Pattern Matching in f#

Consider the following code:

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

When pasted on terminal running fsharpi it gives me this error

> 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

Any suggestions? I tried using both: only TAB and space but it doesn't recognize anything after the function.

Upvotes: 1

Views: 551

Answers (1)

John Palmer
John Palmer

Reputation: 25516

This is just a warning:

if you do getThird3 [1;2] you will get a matchfailureexception.

The warning has to pick somewhere specific as a base for the warning and the function is probably as good as anywhere else.

To remove the warning I would change the match to

| _::_::l3::t -> t;;
| _ -> failwith "not a long enough list"

Upvotes: 3

Related Questions