And another example:
Now, using these two user-defined datatypes we can make a function that will, when fed a season, will return if that season is generally hot or cold.
This is known as Pattern Matching and is very, very useful indeed! What if we want to return a String from a Season? Well, we could do:
but that is bad because the data constructor itself already has something build in to handle this kind of thing.
Wow, what on earth does that mean? Well, we know that Season is a datatype that has four elements. The deriving part has three distinct little bits. Show converts whatever Season to a String when required. Eq stands for Equality and allows testing if two seasons are the same. Ord means this datatype is ordered and two seasons can be compared to see which is bigger. The code below shows examples of this.
Here is another example using the Shape datatype defined in lectures and classes.
Interestingly, the Bool (True or False) is just another datatype. You can ask hugs about the type of these and they are in fact ordered!
From this we can say that the datatype Bool is probably defined something like this (Although in reality it is probably something different.)