[Haskell-cafe] Newbie question about tuples

peterv bf3 at telenet.be
Fri Jul 13 09:03:00 EDT 2007

I'm beginning to see that my old implementation in C++ clutters my Haskell

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)? Or like Digital Mars D's "static if" statement (which is a
control-flow statement that *must* succeed at compile time)?

Tuples allow a different type for each element (they are more like
"anonymous structs"), so are not really suitable for what I want to do. 

Now in C++ when implementing this (together with fixed-size matrices), you
can get a lot of overhead because the code needs to compute many
intermediate results; it has a hard time to unroll all the loops (although I
think the latest compilers are much better). Now when implementing something
like this in Haskell, I would guess that its laziness would allow to
"interleave" many of the math operations, reordering them to be as optimal
as possible, removing many intermediate results (like processing streams).
So you would automatically get something like the C++ Expression Templates
ml... Well at least I hope so :) 

-----Original Message-----
From: haskell-cafe-bounces at haskell.org
[mailto:haskell-cafe-bounces at haskell.org] On Behalf Of Lukas Mai
Sent: Friday, July 13, 2007 09:20
To: haskell-cafe at haskell.org
Subject: Re: [Haskell-cafe] Newbie question about tuples

Am Donnerstag, 12. Juli 2007 20:14 schrieb Andrew Coppin:

> The only thing the libraries provide, as far as I can tell, is the fact
> that tuples are all Functors. (In other words, you can apply some
> function to all the elements to get a new tuple.) I think that's about
> it. I doubt you can use that to define lifting functions...

Actually, they aren't (Functors). (,) takes two type arguments, (,,)
takes three, etc.  class Functor f requires f to take one type argument.
So something like

  instance Functor (,) where ...

won't compile. Besides, what should fmap (+1) (3, 4, "foo") do?

(Somewhere in the libraries there is an
instance Functor (,) a where fmap f (x, y) = (x, f y)
but that's probably not what you expected.)

HTH, Lukas
Haskell-Cafe mailing list
Haskell-Cafe at haskell.org

No virus found in this incoming message.
Checked by AVG Free Edition. 
Version: 7.5.476 / Virus Database: 269.10.4/898 - Release Date: 12/07/2007

No virus found in this outgoing message.
Checked by AVG Free Edition. 
Version: 7.5.476 / Virus Database: 269.10.4/898 - Release Date: 12/07/2007

More information about the Haskell-Cafe mailing list