[Haskell-cafe] Newbie question about tuples

Jonathan Cast jcast at ou.edu
Fri Jul 13 10:21:25 EDT 2007

On Friday 13 July 2007, peterv wrote:
> I'm beginning to see that my old implementation in C++ clutters my Haskell
> design.
> You see, in C++ I can write:
> // A vector is an array of fixed-length N and elements of type T
> template<typename T, int N> struct Vector
> {
>   T Element[N];
>   friend T dot(const Vector& a, const Vector& b)
>   {
>      T result = 0;
>      for( int i=0; i<N; ++i )
>      {
>         result += a.Element[i] * b.Element[i];
>      }
>      return result;
>   }
> };
> So basically a wrapper around a fixed-size array of any length.
> Implementations of (+), (-), dot, length, normalize, etc... then work on
> vectors of any size, without the overhead of storing the size, and with
> compile-time checking that only vectors of the same size can be used,
> etc... This also fits in nicely when creating a Matrix<T,N,M> class.
> I don't think Haskell has something like a "fixed-length array" or constant
> expressions that *must* be resolved at compile-time (like the N in the C++
> template)?

I'm surprised no one has posted anything on type-level programming yet.  You 
might google for that.  And GHC 6.8 will have true type-level functions (with 
guaranteed termination, of course) which will help.  But I'm sure a good 
google will turn up a clearer explanation than I can provide; I've never 
needed or wanted to understand the type-level stuff.

Jonathan Cast

More information about the Haskell-Cafe mailing list