title: Haskell: Between a list and a tuple
body: <p>I want a function <code>+++</code> that adds two mathematical vectors.</p>
  
  <p>I could implement vectors as <code>[x, y, z]</code> and use:</p>
  
  <pre><code>(+++) :: (Num a) =&gt; [a] -&gt; [a] -&gt; [a]
  (+++) = zipWith (+)
  </code></pre>
  
  <p>And thus accomodate any <em>n</em>-dimensional vector (so this would work for <code>[x, y]</code> too).</p>
  
  <p>Or I could implement vectors as <code>(x, y, z)</code> and use:</p>
  
  <pre><code>type Triple a = (a, a, a)
  
  merge :: (a -&gt; b -&gt; c) -&gt; Triple a -&gt; Triple b -&gt; Triple c
  merge f (a, b, c) (x, y, z) = (f a x, f b y, f c z)
  
  (+++) :: (Num a) =&gt; Triple a -&gt; Triple a -&gt; Triple a
  (+++) = merge (+)
  </code></pre>
  
  <p>Of course this is slightly more complex but it when I implement all the other vector functions, that is irrelevant (50 lines instead of 40).</p>
  
  <p>The problem with the list approach is that I can add a 2D vector with a 3D vector. In that case, <code>zipWith</code> would simply chop off the 3D vector's <code>z</code> component. While that might make sense (more likely it should expand the 2D vector to <code>[x, y, 0]</code>), for other functions I'm thinking it could be problematic to have either happen silently. The problem with the tuple approach is it limits the vector to 3 components.</p>
  
  <p>Intuitively, I would think that it would make more sense to represent vectors as <code>(x, y, z)</code>, since a mathematical vector has a fixed number of components and it doesn't really make sense to cons (prepend) a component to a vector.</p>
  
  <p>On the other hand, although it's very unlikely that I will need anything other than 3D vectors, it doesn't seem quite right to limit it to that.</p>
  
  <p>I guess what I want is functions that take two lists of equal length, or better, functions that operate on tuples of arbitrary size.</p>
  
  <p>Any <em>suggestions</em>, in terms of practicality, scalability, elegance, etc.?</p>