Data types basics

Thomas L. Bevan thomas_bevan at toll.com.au
Wed Nov 5 17:05:19 EST 2003


In Haskell, data types and contructors must be designated by
names whose first letter is capitalised.
So,

> data currency = ...

is illegal.

Instead use,

> data Currency = Dollar Double | Pound Double | Zloty Double | Euro Double

Above are four data constructors and they can be used to contruct a datum of
the type Currency.
They can be exploited in functions through pattern matching as follows,

> toDollar :: Currency -> Currency
> toDollar (Pound d) = Dollar ( d / 2 )
> toDollar (Euro d) = Dollar ( d / 1.01 )
> ...

I think, to answer your last question, that they are functions, however they 
can only
be defined using a much more limited syntax. I'm sure someone else can give a 
fuller answer
here.

Tom




More information about the Haskell mailing list