Lesson 18. Parameterized types

 

After reading lesson 18, you’ll be able to

  • Use parameterized types to make generic data types
  • Understand kinds of types
  • Write code using the Data.Map type to look up values

In this unit so far, we’ve discussed how types can be added and multiplied, like data. Like functions, types can also take arguments. Types take arguments by using type variables in their definitions (so their arguments are always other types). Types defined using parameters are called parameterized types. Parameterized types perform an important role in Haskell, as they allow you to define generic data structures that work with a wide range of existing data.

Consider this

Suppose you want to create a type representing a pair of two values of the same type. They could be a pair of Doubles representing latitude and longitude, a pair of Names representing a couple dating, or a pair of graph nodes representing an edge, for example. You don’t want to use the existing Tuple type because you want to ensure that the elements in your pair are exactly the same type. How can you accomplish this?

18.1. Types that take arguments

 
 

18.2. Types with more than one parameter

 

Summary

 
 
sitemap

Unable to load book!

The book could not be loaded.

(try again in a couple of minutes)

manning.com homepage
test yourself with a liveTest