[Haskell-cafe] Re: Datatypes - Haskell

2008-02-10 Thread Mattes Simeon
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
templateclass 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.

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe


[Haskell-cafe] Datatypes - Haskell

2008-02-09 Thread Mattes Simeon
Hello to everybody

I am an new user of Haskel and generally in functional programming and I could
say that I am very impressed from this Language. Though I can't understand the
use of datatypes. 

Let's take a firly simple situtation

e.g. data Pair a b = Pair a b

i.e. an new type with name Pair parameterized over the types a,b with one
Constructor named Paid which take two values of type a,b

a more complex one would be
data Either a b = Left a | Right b

i.e a new type named Either parameterized over the types a, b and two
Constructors 1. Left which take one value of type a and 2. Right which takes one
value of type b

I consider that the definitions above are well formulated. Nevertheless I can't
understand them quite well.

I have read that datatypes are used to define new structures. So, is there any
corresponding example in C, sinch I am quite familiar with structures in it? I
hope the word C here is allowed here :o)

Thanks

___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe