diff --git a/cutom-types/declaring-types/README.md b/cutom-types/declaring-types/README.md index 403b594..c13590f 100644 --- a/cutom-types/declaring-types/README.md +++ b/cutom-types/declaring-types/README.md @@ -1,3 +1,3 @@ # Declaring Types -We have already explored the basic types of Haskell in [chapter 2](../../types-in-haskell/basic-types.md), but now we will look into how we can define our own types. There are three different ways in which we can define new types in Haskell – **Type synonyms** (or **Type aliases**), **Data declarations**, and **Newtype declarations**. +We have already explored the basic types of Haskell in [Basic Types](../../types-in-haskell/basic-types.md), so now we will look into how we can define our own types. There are three different ways in which we can define new types in Haskell – **Type synonyms** (or **Type aliases**), **Data declarations**, and **Newtype declarations**. diff --git a/cutom-types/declaring-types/data-declarations.md b/cutom-types/declaring-types/data-declarations.md index 0bcb962..003d008 100644 --- a/cutom-types/declaring-types/data-declarations.md +++ b/cutom-types/declaring-types/data-declarations.md @@ -1,6 +1,6 @@ # Data Declarations -With the keyword `Data`, we can define new types rather than just synonyms for already existing ones. Types declared with `data` are called **algebraic**, referencing **"sum"** and **"product"**. In data types **"sum"** is alteration (`A | B`, meaning `A` or `B` but not both), and **"product"** is combination (`A B`, meaning `A` and `B` together). +With the keyword `Data`, we can define new types rather than just synonyms for already existing ones. Types declared with `data` are called **algebraic**, referencing **"sum"** and **"product"**. In data types, **"sum"** means _alteration_ (`A | B`, meaning `A` or `B` but not both), and **"product"** means _combination_ (`A B`, meaning `A` and `B` together). For example, we can create a type that represents a card suit: @@ -11,15 +11,15 @@ data Suit = Hearts | Clubs ``` -`Suit` is the **type constructor** in this definition, and the values (`Hearts`, `Diamonds`, `Spades` or `Clubs`) are **data constructors**. It states that the new type `Suit` can have **one of the four values** (the `|` symbol stands for "or"). This means `Suit` is a **sum-type**, which is any type that has multiple possible representations. Another example of a sum-type would be `Bool` which can be either `True` or `False:` +`Suit` is the **type constructor** in this definition, and the values (`Hearts`, `Diamonds`, `Spades` or `Clubs`) are **data constructors**. It states that the new type `Suit` can have **one of the four values** (the `|` symbol stands for "or"). This means that `Suit` is a **sum type**, which is any type that has multiple possible representations. Another example of a sum type would be `Bool` which can be either `True` or `False:` ```haskell data Bool = True | False ``` -**Data constructors** can have zero or more arguments. `Hearts | Diamonds / True | False` are all examples of data constructors that have zero arguments - **nullary data constructors**. Likewise, **type constructors** can have zero or more arguments. `Suit` and `Bool` are examples or **nullary type constructors**. +**Data constructors** can have zero or more arguments. `Hearts | Diamonds` and `True | False` are examples of data constructors that have zero arguments - **nullary data constructors**. Likewise, **type constructors** can have zero or more arguments. `Suit` and `Bool` are examples of **nullary type constructors**. -Let's take a look at some type and data constructors that have more than zero arguments. Here is an example of an error type from the [cardano-node repository](https://github.com/input-output-hk/cardano-node/blob/master/cardano-node/src/Cardano/Node/Types.hs): +Let's take a look at some type- and data- constructors that have more than zero arguments. Here is an example of an error type from the [cardano-node repository](https://github.com/input-output-hk/cardano-node/blob/85d05720da7b0c02dfa890079e568c8499a027a7/cardano-node/src/Cardano/Node/Types.hs#L49-L52): ```haskell data ConfigError = @@ -30,7 +30,7 @@ data ConfigError = `ConfigError` is a nullary type constructor, and its data constructors are `ConfigErrorFileNotFound FilePath`, and `ConfigErrorNoEKG`. `ConfigErrorFileNotFound FilePath` is a **unary** data constructor meaning that it actually holds some data, in this case of type `FilePath`. Nullary type constructors we have seen so far contain no data aside from their names. -Both the type name and its constructors must begin with a capital letter, and **constructors must be unique** to that type, i.e. the same constructor cannot be defined in more than one type. Once a new type is defined, it can be used in functions just like any other data type in Haskell. Just to illustrate, a very simple function to get the card suit in a string format would be: +Both the type name and its constructors must begin with a capital letter, and **constructors must be unique** to that type, i.e. the same constructor cannot be defined in more than one type. Once a new type is defined, it can be used in functions just like any other data type in Haskell. To illustrate, a very simple function to get the card suit in a string format using pattern matching would be: ```haskell suitStr :: Suit -> String diff --git a/cutom-types/declaring-types/type-synonyms.md b/cutom-types/declaring-types/type-synonyms.md index 1e1d3be..ec69723 100644 --- a/cutom-types/declaring-types/type-synonyms.md +++ b/cutom-types/declaring-types/type-synonyms.md @@ -1,6 +1,6 @@ # Type Synonyms -**Type synonyms** are the simplest way to declare a new type as they simply provide an alias for one of the already existing types. For example, we already know that `String` is actually just a synonym for list of `Chars`, and it is defined as: +**Type synonyms** are the simplest way to declare a new type as they simply provide an alias for an already existing type. For example, we already know that `String` is actually just a synonym for a list of `Chars`, and it is defined as: ```haskell type String = [Char] @@ -12,7 +12,7 @@ We can use the declared type synonyms to define other types as well. We can defi type StringList = [String] ``` -It is important to note that the type synonyms and their base types are interchangeable in almost all cases. That means that any function that has a type signature including a list of strings (`[String]`) could be used on an element that has the type of `StringList` as they are just synonyms: +It is important to note that the **type synonyms and their base types are interchangeable** in almost all cases. That means that any function that has a type signature including a list of strings (`[String]`) could be used on an element that has the type of `StringList` as they are just synonyms: ```haskell reverseStringList :: StringList -> StringList