[Haskell-cafe] data vs type

Ketil Malde ketil.malde at bccs.uib.no
Wed Jan 4 08:56:01 EST 2006


Daniel Carrera <daniel.carrera at zmsl.com> writes:

> I'm trying to figure out how 'data' and 'type'.

How data and type...what? :-)

Generally, 'type' introduces type synonyms, i.e. just gives a new name
to an existing type, while 'data' defines new (algebraic) types.

So you can use

   type Name = String

in order to make your program (and, if you're lucky, the compiler
error messages) more legible, or you can use

   data Name = Nick String 
             | Initials Char (Maybe Char) Char 
             | Full String String

to deal with names in various forms (pronounce the '|' as 'or')

Note that the initial words on the right side in the data declaration
(i.e. "Nick","Initials", and "Full") are type constructors, not types
(like "Name" and "String" are).

Did that help?

(There's also 'newtype', btw, which lets you add a constructor to a
type synonym.)

-k
-- 
If I haven't seen further, it is by standing in the footprints of giants



More information about the Haskell-Cafe mailing list