Thursday, October 23, 2014

Haskell - Defining custom data types

  • allows us to add structure to the values in our programs
  • defining our own types also imrpoves the safety of our code
  • haskell will not allow us to accidentally mix values of two values that are structurally similar but have different names.

-- file: ch03/BookStore.hs
data BookInfo = Book Int String [String]
                deriving (Show)
 
  1. The BookInfo after the data keyword is the name of our new type. We call BookInfo a type constructor
  2. The Book that follows is the name of the value constructor (sometimes called a data constructor)
  3. After Book, the Int, String, and [String] that follow are the components of the type.

No comments:

Post a Comment