Haskell contains some basic types (primitives) and aggregated types. Type names always start with a capital letter!

Basic Types

Basic types are predefined types that cannot be made up of other types. These include:

  • Bool
  • Char
  • Int (at least 30 bits)
  • Integer (arbitrary size, no overflow possible)
  • Float
  • Double

Enumerations

Enumerations can be implemented like this:

  data Color = Red | Yellow | Green
data ToDo = Stop | Wait | Go deriving (Show)
  

Red for example will now become a value of type Color.

Aggregated Types

Aggregated types are made up of basic types (different or same). This group contains:

  • Pairs / Tuples (e.g. coordinates (Int, Int))
  • Record types (e.g. data Person = Person { name :: String, age :: Int })
  • Lists

Tuples

Tuples are a finite sequence of components of possibly different types. The number of components is called arity.

  ("Hello, World!", 42, True) -- :: (String, Int, Bool)
  

Components of the tuples can be accessed by pattern matching.

Record Types

Records types are similar to classes in object-oriented langauges with the important different: They are immutable!

A record type in Haskell has multiple parts:

  • Type Constructor: Defines the name of the type
  • Value Constructor: Used to create new values of the type
  • Components: Define the fields
  • Deriving Clause: Automatically derives instances for the named classes

Lists

Lists are one of the most important data types in Haskell. They can only contain values of a single type and can be finite or infinite.

More about lists see 02 Lists.

  data Person = Person { name :: String, age :: Int } deriving (Show)
-- data TC = VC { COMPS } deriving (...)
  

Function Types

The type of a function defined as a sequence of types. For example, this function which sums two integers together.

  Sum :: Int -> Int -> Int
  

More about functions see 03 Functions.

Polymorphic Types

To allow functions to work on multiple different types, type variables can be defined. These type variables can contain any type or a set of types (Typeclasses). They are called polymorphic.

  fst :: (a, b) -> a -- a and b are no types (no capital letter!)
fst (x, y) = x     -- get first element regardless of type
  

Type Synonyms

The type keyword can be used to create an alias for an existing type. Synonym and type name can be used intercheangably.

  type Coord = (Int, Int)
  

Typeclasses

Even tough polymorphic types are handy, not all functions can be defined as generic as fst. For example max which retuns the larger of two values must be able to compare the two values.

  max :: Ord a => a -> a -> a
  

max uses a class constraint which only allows types with a notion of order.

Some predefined classes in Haskell:

  • Eq: equality types
  • Ord: ordered types
  • Show: showable types (“ToString()”)
  • Num: numeric types (support algebraic operations)
  • Integral: integral types (subset of Num)
  • Fractional: fractional types (subset of Num)