Want to keep learning?

This content is taken from the The University of Glasgow's online course, Functional Programming in Haskell: Supercharge Your Coding. Join the course to learn more.

Skip to 0 minutes and 5 seconds JEREMY: Let’s talk about data types. A type is a set of values that are related, a family of values that belong together. So far we’ve seen the Bool data type, which consists of values True and False. We’ve also used the Int data type, which consists of whole number values from some minimum to some maximum. These bounds may differ based on whether your OS is 32-bit or 64-bit. What else have we looked at? Characters, lists. Oh, yes, and pairs as well. We’ve also looked at function types that describe argument and return values. Now, we want to think about user-defined types, creating custom data types for our own program.

Skip to 0 minutes and 57 seconds First, we’ll consider a simple type that consists of a finite set of alternative values. Apparently, in the Amazon jungle, there is a tribe who count one, two, many. That is, they have no distinct words for larger integers than two. We could represent this type in Haskell as follows– data SimpleNum equals One or Two or Many. If you have GHCi open, why not type this in your interactive session right away? The key word “data” indicates we are defining a new type. The name of the type and the names of the values should start with capital letters. The alternative values are separated with a vertical bar character. Let’s have a look at some of these values. One– ah, we can’t see it.

Skip to 2 minutes and 1 second We need to be able to print it out. Add deriving Show to the type definition.

Skip to 2 minutes and 10 seconds Show is a type class. We’ll talk in more detail about this later. For now, let’s just understand that any type must derive Show if we are to print out its values.

Skip to 2 minutes and 26 seconds Data SimpleNum equals One or Two or Many deriving Show. OK.

Skip to 2 minutes and 40 seconds Great. Now, let’s write a function to convert from int to SimpleNum values. First, I’m going to turn on multi-line support in GHCi so I can format my function definition over several lines. I use the set command to do this. OK. Let’s go for it. Let convert 1 equal One, convert 2 equal Two, convert underscore equal Many. So convert takes an int input– well, really a value that has a type belonging to the Num data class– again, more later– and return a SimpleNum output. Let’s try this out.

Skip to 3 minutes and 32 seconds Convert 1.

Skip to 3 minutes and 36 seconds Convert 300. OK. Map convert 1 to 5. Perfect.

Skip to 3 minutes and 51 seconds Right. That’s a custom data type with alternative values, otherwise known as a Sum data type. Now let’s think about a record data type that stores a portfolio of values. Hm. How about cricket scores? When a team bats in cricket, you need to know two integer values. The first is the number of runs scored by the team. The second is the number of players who are out, i.e. have been eliminated. So a good score for the New Zealand cricket team might be 350 for 4. That’s 350 runs scored for the loss of four players. We can represent this as a Product data type. Data CricketScore equals Score Char list int, int, deriving Show.

Skip to 4 minutes and 58 seconds Score is a type constructor that takes a string and two int arguments and returns a cricket score value.

Skip to 5 minutes and 12 seconds In general, these kinds of custom data types are called algebraic data types. The alternative values relate to algebraic sums, and the record values relate to algebraic products. I’ll spare you the hairy Type Theory for now. But at least you know to Google for algebraic data types if you are keen to discover more about type theory. Let’s conclude with what we’ve learned in this session.

Skip to 5 minutes and 45 seconds First, we use the “data” keyword to define a new custom type. Second, types must derive the Show type class if we want to see their values printed out. Third, we used vertical bars to specify alternatives, or sum data types. And we used type constructors to build record types like score values in cricket, which are product data types. Thanks. Goodbye.

Define Your Own Data Types

So far we have looked at the basic built-in data types like Int and Boolean. We can combine these basic types to generate custom data types, using algebraic sums and products.

Share this video:

This video is from the free online course:

Functional Programming in Haskell: Supercharge Your Coding

The University of Glasgow

Get a taste of this course

Find out what this course is like by previewing some of the course steps before you join: