title: SML NJ - Polymorphic custom list type and summing the elements
body: <p>I'm learning about polymorphism and SML. I'm a little unsure on how to handle summing of elements while keeping the function polymorphic.  </p>
  
  <p>Say if I have this definition...</p>
  
  <pre><code>datatype 'a customList = nothing | customL of 'a * 'a customList
  </code></pre>
  
  <p>and this list...</p>
  
  <pre><code>val a = customL(2, customL(1, customL(3, nothing)))
  </code></pre>
  
  <p>I figure I need to use (op +) because of the polymorphic handling but I can't get these to work...</p>
  
  <pre><code>fun addElements (op +) nothing = 0
  |   addElements (op +) (customL(x, nothing) = x
  |   addElements (op +) (customL(x, xs)) = x + addElements xs
  </code></pre>
  
  <p>I'm also thinking that handling of "nothing" may mess up the type as well?</p>
  
  <p>Anyone able to help with getting this to work?</p>
  
  <p><strong>Update</strong></p>
  
  <p>I think I'm closer to it but I don't understand why I'm getting an error...</p>
  
  <pre><code>exception EmptyList
  
  fun addElements (op + ) nothing = raise EmptyList
  |   addElements (op + ) (customL(x, nothing)) = x
  |   addElements (op + ) (customL(x, xs)) = let val rest_of_list = addElements (op +) xs
                                         in
                                            (op + ) (x, rest_of_list)
                                         end
  </code></pre>