Mindmap
Making Our Own Types and Typeclasses

Inhalt der Mindmap
Making Our Own Types and Typeclasses
Creating our own data types
- data means that we're defining a new data type. The part before the = denotes the type, which is Bool. The parts after the = are value constructors.
- fox example to create an own type shape type we can create it as well using constructor types
- We can use the same name for the data type and the value constructor.
- we can make some auxilliary functions that create shapes of some size at the zero coordinates and then nudge those
- we can export our data types writing our type along with the functions we are exporting and then add some parentheses and in them specify the value constructors
- By doing Shape(..), we exported all the value constructors for Shape
- we can export our data types writing our type along with the functions we are exporting and then add some parentheses and in them specify the value constructors
- We can create also own types by using brackets and specifying name of fields and types
- fox example to create an own type shape type we can create it as well using constructor types
- data means that we're defining a new data type. The part before the = denotes the type, which is Bool. The parts after the = are value constructors.
Type parameters
- A value constructor can take some values parameters and then produce a new value.
- type constructors can take types as parameters to produce new types.
- The a here is the type parameter. And because there's a type parameter involved, we call Maybe a type constructor. Depending on what we want this data type to hold when it's not Nothing, this type constructor can end up producing a type of Maybe Int, Maybe Car, Maybe String, etc.
- type constructors can take types as parameters to produce new types.
- A value constructor can take some values parameters and then produce a new value.
Derived instances
- We can derive instances of typeclass to our own types
- When we derive the Eq instance for a type and then try to compare two values of that type with == or /=, Haskell will see if the value constructors match (there's only one value constructor here though) and then it will check if all the data contained inside matches by testing each pair of fields with ==
- We can instantiate several typeclasses adding each of them as a tuple, for example deriving (Eq, Show, Read)
- When we derive the Eq instance for a type and then try to compare two values of that type with == or /=, Haskell will see if the value constructors match (there's only one value constructor here though) and then it will check if all the data contained inside matches by testing each pair of fields with ==
- We can derive instances of typeclass to our own types
Type synonyms
- Using the "type" keyword we can a synonym of an already created existing type
- We introduce type synonyms either to describe what some existing type represents in our functions (and thus our type declarations become better documentation) or when something has a long-ish type that's repeated a lot but represents something more specific in the context of our functions.
- Using the "type" keyword we can a synonym of an already created existing type
Recursive data structures
- A constructor in an algebraic data type can have several (or none at all) fields and each field must be of some concrete type.
- We can make types whose constructors have fields that are of the same type
- data List a = Empty | Cons a (List a) deriving (Show, Read, Eq, Ord)
- With this feature, we can create binary search trees
- data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
- And then create functions to search create and insert eleents in trees
- And then create functions to search create and insert eleents in trees
- data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
- With this feature, we can create binary search trees
- data List a = Empty | Cons a (List a) deriving (Show, Read, Eq, Ord)
- We can make types whose constructors have fields that are of the same type
- A constructor in an algebraic data type can have several (or none at all) fields and each field must be of some concrete type.
Own Definitions of Type Classes
- To create our own type classes we can use the keyword "class" and the type class followed by his functions definitions
- Also, we can create type classes inside the type definitions and defined as instances by using instance keyword
- We can use restrictions to create our ow type classes and receive constructors types such like Maybe
- We can use restrictions to create our ow type classes and receive constructors types such like Maybe
- Also, we can create type classes inside the type definitions and defined as instances by using instance keyword
- To create our own type classes we can use the keyword "class" and the type class followed by his functions definitions
The functor typeclass
- Is basically for things that can be mapped over.
- for list implementations t takes a function from one type to another and a list of one type and returns a list of another type. My friends, I think we have ourselves a functor! In fact, map is just a fmap that works only on lists. Here's how the list is an instance of the Functor typeclass.
- Is basically for things that can be mapped over.
Bilder und Dateien der Mindmap
Informationen
- Beschreibung
- Mindmap am Making Our Own Types and Typeclasses, erstellt von TABARES ROMERO DIEGO MAURICIO am 03/04/2020.
- Erstellt von
- TABARES ROMERO DIEGO MAURICIO · Apr 2020
- Aktualisiert
- 4 Apr 2020
- Aufrufe
- 1
- Bewertung










