£199.99 £139.99 for one year of Unlimited learning. Offer ends on 14 November 2022 at 23:59 (UTC). T&Cs apply

Find out more
Define Your Own Data Types
Skip main navigation

Define Your Own Data Types

Types are used to describe data. In this video, Dr Jeremy Singer shows how to define custom data types in Haskell.
5
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.
57.1
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.
120.9
We need to be able to print it out. Add deriving Show to the type definition.
130.3
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.
146.3
Data SimpleNum equals One or Two or Many deriving Show. OK.
160
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.
212
Convert 1.
216.4
Convert 300. OK. Map convert 1 to 5. Perfect.
231.2
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.
298
Score is a type constructor that takes a string and two int arguments and returns a cricket score value.
311.8
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.
344.7
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.

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.

This article is from the free online

Functional Programming in Haskell: Supercharge Your Coding

Created by
FutureLearn - Learning For Life

Our purpose is to transform access to education.

We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.

We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.

Learn more about how FutureLearn is transforming access to education