Alexander Ryan Baggett
Alexander Ryan Baggett

Reputation: 2397

Differences between 2 types of List patterns

So, I am learning F# and been studying how lists are often used in pattern matching. I am saw 2 different List patterns and I am trying to figure out the difference between them.

So lets say we are doing pattern matching on a list of chars like so

let charlist = ['x';'y';'z']

and we were matching it like so,

match charlist with

what would be difference between these 2 cases?

| [a;b;c] ->

| a::b::c::[] ->

Upvotes: 1

Views: 118

Answers (1)

John Palmer
John Palmer

Reputation: 25516

Of course, you can just ask the compiler:

> match charlist with           
- |[a;b;c] -> ()                
- |a::b::c::[] -> ()            
- | _ -> ();;                   

  |a::b::c::[] -> ()
  -^^^^^^^^^^^

/home/john/stdin(13,2): warning FS0026: This rule will never be matched

As the second rule is never matched, you know that it will only match when the first does, so that the second one is matches a subset of the first rule. Reversing the order produces the same error message. As a result, we know that they are identical.

Upvotes: 4

Related Questions