Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I think that the using a discriminated union together with a mutable record is a good approach. Discriminated unions are essential for pattern matching. An alternative to using mutable record would be to create a union case with mutable <em>reference cells</em>:</p> <pre><code>// Note: using ´ instead of ' to avoid StackOverflow syntax confusion type LinkedList&lt;´T&gt; = | None | Node of (LinkedList&lt;´T&gt; ref) * 'T * (LinkedList&lt;´T&gt; ref) </code></pre> <p>This may lead to slightly simpler code. For example the <code>insert</code> function would look like this (I didn't try it, but I think it should be correct):</p> <pre><code>let insert x l = match l with | None -&gt; Node(ref None, x, ref None) | Node(prev, v, next) as node -&gt; match !prev with | None -&gt; prev := Node(ref None, x, ref node) !prev | Node(_, _, prevNextRef) -&gt; prevNextRef := Node(ref (!node.Prev), x, ref node) prev := !prevNextRef !prevNextRef </code></pre> <p>However, I don't think this makes the code much more succinct (maybe even slightly less readable). In any case, you could define active pattern to distinguish among the three cases in the <code>insert</code> function using a single <code>match</code> expression. The following is for your original data structure. This is simply an extractor of the elements stored in a record: </p> <pre><code>let (|NodeInfo|) node = (node.Prev, node.Element, node.Next) </code></pre> <p>For more info, see <a href="http://msdn.microsoft.com/en-us/library/dd233248.aspx" rel="noreferrer">Active Patterns at MSDN</a>. Then, the <code>insert</code> function would look like this:</p> <pre><code>let insert x l = match l with | None -&gt; Node({ Prev=None; Element=x; Next=None }) | Node(NodeInfo(None, _, _) as node) -&gt; let new_node = { Prev=None; Element=x; Next=Node(node)} node.Prev &lt;- Node(new_node) Node(new_node) | Node(NodeInfo(Node(prev_node), _, _) as node) -&gt; let new_node = { Prev=node.Prev; Element=x; Next=Node(node)} node.Prev &lt;- Node(new_node) prev_node.Next &lt;- Node(new_node) Node(prev_node) </code></pre> <p><strong>[EDIT]</strong> Actually, the same thing could be done using patterns for extracting elements of a record, but I think that active patterns may make the code slightly nicer.</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