Lesson 12. Creating your own types

 

After reading lesson 12, you’ll be able to

  • Define type synonyms to clarify code
  • Create your own data type
  • Build types from other types
  • Work with complex types by using record syntax

In the preceding lesson, you learned how to use the basic types in Haskell. Now it’s time to start creating some types of your own. Creating types in Haskell is more important than in most other programming languages, even statically typed ones, as nearly every problem you solve will come down to the types you’re using. Even when using an existing type, you’ll often want to rename it to make understanding your programs easier. For example, take a look at this type signature:

areaOfCircle :: Double -> Double

This is a perfectly fine type signature, but suppose you saw this instead:

areaOfCircle :: Diameter -> Area

With this alternate type signature, you know exactly what type of arguments your function expects as well as what the result means.

You’ll also learn how to create more-complicated types of your own. Creating types for data in Haskell is as important as creating classes in object-oriented languages.

12.1. Using type synonyms

12.2. Creating new types

12.3. Using record syntax

Summary

sitemap