title: Pattern Matching a List in SML
body: <p>Lets say I create a student record object which is a tuple that consists of (studentID,name,midtermScore,finalScore). I can then use pattern matching to create functions that use this object. For example, a function that returns a composite score based on the student record:</p>
  <pre><code>fun score (studentID,name,midtermScore,finalScore) = 
       ( 0.4 * (midtermScore) ) + ( 0.6 * (finalScore) )
  </code></pre>
  <p>Now lets say I wanted to create another function which operates on a whole <em>list</em> of these student record objects, this function would take such a list, and return a new list containing the studentID of each record object, and its composite score. For example:</p>
  <pre><code>fun scores ( [studentID,name,midtermScore,finalScore] ) = 
       map(fn x =&gt; (studentID,score(x)))
  </code></pre>
  <p>I can also implement this function in other ways syntactically which also use pattern matching, but the problem I'm having is while the code compiles, it never generates the bindings I'm looking for. For example, the above <strong>scores</strong> function generates these bindings:</p>
  <blockquote>
  <p>val scores = fn : 'a list -&gt; ('b * 'c * real * real) list -&gt; ('a * real) list</p>
  </blockquote>
  <p>Whereas what I'm trying to achieve is this:</p>
  <blockquote>
  <p>val scores = fn : ('a * 'b * real * real) list -&gt; ('a * real) list</p>
  </blockquote>
  <p>I know whats causing this discrepancy is in the way that I'm pattern matching the list of student record objects as a parameter to the <strong>scores</strong> function.</p>
  <p>Could someone explain semantics wise why I'm getting the bindings that I'm getting, and how I would need to modify the <strong>scores</strong> function in order to generate the desired bindings?</p>