[Haskell-cafe] Re: Datatypes - Haskell

Mattes Simeon simeon.mattes at gmail.com
Sun Feb 10 07:40:59 EST 2008


Thanks for your help. It was very useful. 

Though in comparison with C or C++ I can't figure out so clear the syntax. 
Maybe it has to do with the syntactic Sugar of each Language. I 'll give you a
similar example I saw in a book for Haskel

The following program just returns the value of the position of a datatype Tuple
which can hold one or two elements.

data Tuple a b = One a | Two a b 
tuple1 (One a)= Just a
tuple1 (Two a b) = Just a

tuple2 (One a) = Nothing
tuple2 (Two a b) = Just b

The corresponding Version in C++, which seems to be more appropriate, would be
template<class A, class B>
struct Tuple
{
   enum (One, Two) tag;
   union
   {
      A either_one;
      struct nOne
      {
	A either_two
	B two;
      };
   };
}

Am I wrong. If not, how can I use it in the corresponding function in  C++? 
I seems realy strange, and I'm confused. 

Surely a solution to this would be to use the standard types of Haskel for
tuples and check out each time if I have just a number or a tuple. But this 
is how somebody thinks in imperative languages. Functional programming is
something more, isn't it?

Sorry for beeing so naive, but although unions, enum, structure are just 
some tools in C, surely something more in C++, in Haskell they are seem to be a
standard.



More information about the Haskell-Cafe mailing list