[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


Re: [Haskell-cafe] Datatypes - Haskell

2008-02-09 Thread Brandon S. Allbery KF8NH


On Feb 9, 2008, at 19:09 , Mattes Simeon wrote:


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


struct Pair { a pair_a; b pair_b; };


data Either a b = Left a | Right b


union Either { enum { Left, Right } _tag; a either_left; b  
either_right; };
(except that Haskell makes sure you use it properly, while C will let  
you access foo.either_right when foo._tag == Left).


--
brandon s. allbery [solaris,freebsd,perl,pugs,haskell] [EMAIL PROTECTED]
system administrator [openafs,heimdal,too many hats] [EMAIL PROTECTED]
electrical and computer engineering, carnegie mellon universityKF8NH


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


Re: [Haskell-cafe] Datatypes - Haskell

2008-02-09 Thread Sebastian Sylvan
On Feb 10, 2008 12:09 AM, Mattes Simeon <[EMAIL PROTECTED]> wrote:

> 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)
>

I guess C++ would be closer...

template< class A, class B>
struct Pair
{
A fst;
B snd;
}

template< class A, class B>
struct Either
{
enum {Left, Right} tag;
union{
   A left;
   B right;
   };
}

In the second example the tag would be used to figure out which of the two
alternatives the structure actually is. In Haskell you can just pattern
match on the constructor.

-- 
Sebastian Sylvan
+44(0)7857-300802
UIN: 44640862
___
Haskell-Cafe mailing list
Haskell-Cafe@haskell.org
http://www.haskell.org/mailman/listinfo/haskell-cafe