Defining a data type

Algebraic datatype: a Traffic-light modelization

A simple traffic light enumeration-like model:

data TrafficLight = Red | Yellow | Green deriving (Show, Eq)

The Traffic light can have three states only:

A transition function could be defined that will give the next state of the traffic light according to its present state.

nextState :: TrafficLight -> TrafficLight -- type annotation
nextState Red = Green
nextState Yellow = Red
nextState Green = Yellow

Binary Tree Data Structure and Depth of a Binary Tree

A simple binary tree structure:

data BinaryTree a = EmptyBinaryTree | Node a (BinaryTree a) (BinaryTree a) deriving (Show, Eq)

This is called a recursive algebraic datatype.

The depth of this binary tree can be computed using this function:

depth :: (BinaryTree a) -> Int
depth EmptyBinaryTree = 0
depth (Node _ left right) = 1 + (max (depth left) (depth right))