# What Is an Algebraic Data Type in Haskell?

//

Scott Campbell

An algebraic data type is a fundamental concept in Haskell programming language that allows you to define custom data types with multiple constructors. It is a powerful feature that enables you to represent complex data structures and model real-world entities in a concise and expressive way.

## Defining Algebraic Data Types

In Haskell, you can define an algebraic data type using the data keyword, followed by the name of the type and its constructors. Constructors are essentially functions that create values of the specified type.

Let’s take a simple example of defining a Shape data type:

```data Shape = Circle Float
| Rectangle Float Float
```

In this example, we have defined two constructors for the Shape type: Circle and Rectangle. The Circle constructor takes a single argument of type Float, representing the radius of the circle. The Rectangle constructor takes two arguments of type Float, representing the width and height of the rectangle.

## Pattern Matching with Algebraic Data Types

An important aspect of working with algebraic data types is pattern matching. Pattern matching allows you to deconstruct values of a given type by matching their constructors.

To demonstrate pattern matching, let’s define a function called areaOfShape:

```areaOfShape :: Shape -> Float
areaOfShape (Rectangle width height) = width * height
```

In this example, we use pattern matching to deconstruct values of the Shape type. If the value is a Circle, we calculate its area using the formula for the area of a circle. If the value is a Rectangle, we calculate its area using the formula for the area of a rectangle.

## Recursive Algebraic Data Types

Algebraic data types can also be recursive, meaning they can refer to themselves in their constructors. This allows you to define data structures that have arbitrary levels of nesting.

Let’s define a recursive data type called List, which represents a linked list:

```data List a = Empty
| Cons a (List a)
```

In this example, we have defined two constructors for the List type: Empty and Cons. The Empty constructor represents an empty list, while the Cons constructor represents a non-empty list by combining an element of type a with another list of type (List a).

### List Operations with Pattern Matching

We can perform various operations on lists by pattern matching on their constructors. Let’s define two common operations: lengthOfList, which calculates the length of a list, and reverseList, which reverses the elements in a list:

```lengthOfList :: List a -> Int
lengthOfList Empty = 0
lengthOfList (Cons _ rest) = 1 + lengthOfList rest

reverseList :: List a -> List a
reverseList Empty = Empty
reverseList (Cons x rest) = appendToTail (reverseList rest) x
where
appendToTail Empty element = Cons element Empty
appendToTail (Cons y ys) element = Cons y (appendToTail ys element)
```

In the lengthOfList function, we use pattern matching to handle two cases: when the list is empty (Empty) and when it is non-empty (Cons _ rest). In the non-empty case, we recursively calculate the length of the remaining list (rest) and increment it by 1.

The reverseList function also uses pattern matching to handle two cases: when the list is empty (Empty) and when it is non-empty (Cons x rest). In the non-empty case, we recursively reverse the remaining list (rest) and append the current element (x) to its tail using an auxiliary function called appendToTail.

## Conclusion

In Haskell, algebraic data types provide a flexible and powerful mechanism for defining custom data structures. By using constructors and pattern matching, you can create complex types with ease and perform operations on them in an elegant manner. Understanding algebraic data types is essential for writing expressive Haskell code.