Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>It depends on what kind of pattern matching do you mean - it is quite powerful construct and can be used in all sorts of ways. However, I'll try to explain how pattern matching works on lists. You can write for example these patterns:</p> <pre><code>match l with | [1; 2; 3] -&gt; // specific list of 3 elements | 1::rest -&gt; // list starting with 1 followed by more elements | x::xs -&gt; // non-empty list with element 'x' followed by a list | [] -&gt; // empty list (no elements) </code></pre> <p>The F# list is actually a discriminated union containing two cases - <code>[]</code> representing an empty list or <code>x::xs</code> representing a list with first element <code>x</code> followed by some other elements. In C#, this might be represented like this:</p> <pre><code>// Represents any list abstract class List&lt;T&gt; { } // Case '[]' representing an empty list class EmptyList&lt;T&gt; : List&lt;T&gt; { } // Case 'x::xs' representing list with element followed by other list class ConsList&lt;T&gt; : List&lt;T&gt; { public T Value { get; set; } public List&lt;T&gt; Rest { get; set; } } </code></pre> <p>The patterns above would be compiled to the following (I'm using pseudo-code to make this simpler):</p> <pre><code>if (l is ConsList) &amp;&amp; (l.Value == 1) &amp;&amp; (l.Rest is ConsList) &amp;&amp; (l.Rest.Value == 2) &amp;&amp; (l.Rest.Rest is ConsList) &amp;&amp; (l.Rest.Rest.Value == 3) &amp;&amp; (l.Rest.Rest.Rest is EmptyList) then // specific list of 3 elements else if (l is ConsList) &amp;&amp; (l.Value == 1) then var rest = l.Rest; // list starting with 1 followed by more elements else if (l is ConsList) then var x = l.Value, xs = l.Rest; // non-empty list with element 'x' followed by a list else if (l is EmptyList) then // empty list (no elements) </code></pre> <p>As you can see, there is no looping involved. When processing lists in F#, you would use recursion to implement looping, but pattern matching is used on individual elements (<code>ConsList</code>) that together compose the entire list.</p> <p>Pattern matching on lists is a specific case of <em>discriminated union</em> which is discussed by <em>sepp2k</em>. There are other constructs that may appear in pattern matching, but essentially all of them are compiled using some (complicated) <code>if</code> statement.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload