Tutorial by Examples

The easiest way to create a custom data type in Haskell is to use the data keyword: data Foo = Bar | Biz The name of the type is specified between data and =, and is called a type constructor. After = we specify all value constructors of our data type, delimited by the | sign. There is a rule in...
Value constructors are functions that return a value of a data type. Because of this, just like any other function, they can take one or more parameters: data Foo = Bar String Int | Biz String Let's check the type of the Bar value constructor. :t Bar prints Bar :: String -> Int -> Foo...
Type constructors can take one or more type parameters: data Foo a b = Bar a b | Biz a b Type parameters in Haskell must begin with a lowercase letter. Our custom data type is not a real type yet. In order to create values of our type, we must substitute all type parameters with actual types. Be...
Assume we want to create a data type Person, which has a first and last name, an age, a phone number, a street, a zip code and a town. We could write data Person = Person String String Int Int String String String If we want now to get the phone number, we need to make a function getPhone :: P...

Page 1 of 1