1

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.

  • Can you clarify what you expect as output? As it is based on your question and comments on the one answer here, I don't know what it is you're wanting. – jdphenix Feb 04 '16 at 04:42
  • BTW, you're missing a second semicolon on the last line (`getThird3 list1;` should be `getThird3 list1;;`). That might be why, even when you correct your function so it handles lists with one or two items as input, you're still not seeing the results. F# doesn't need semicolons between expressions, but the F# interactive interpreter uses **double** semicolons between expressions so it knows when you're done with input. (Because **single** semicolons are a valid operator, used in lists). – rmunn Feb 04 '16 at 10:24

1 Answers1

3

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"
John Palmer
  • 25,356
  • 3
  • 48
  • 67