Mind map
Making our own types and typeclasses
Map content
Making our own types and typeclasses
Record syntax
- Here's how we could achieve a functionality with record syntax
- data Person Person { firstName:: String, lastName:: String, age:: Int, height: Float, phoneNumber: String, flavor:: String deriving (Show)
- We use curly brackets
- firstNam then we write a double colon
- First we write the name of the field, for instance
- data Person Person { firstName:: String, lastName:: String, age:: Int, height: Float, phoneNumber: String, flavor:: String deriving (Show)
- Here's how we could achieve a functionality with record syntax
Derived instances
- we first make our data type and then we think about what it can act like
- a typeclass is a sort of an interface that defines some behavior
- Haskell can derive the behavior of our types in these contexts if we use the deriving keyword when making our data type
- A type can be made an instance of a typeclass if it supports that behavior
Type parameters
- A value constructor can take some values parameters and then produce a new value.
- data Maybe a Nothing | Just a
- Type parameters are useful because we can make different types with them depending on what kind of types we want contained in our data type.
- A value constructor can take some values parameters and then produce a new value.
Algebraic data types
- data means that we're defining a new data type. Ther part before the=denotes the type
- If we wanted to export the functions and types that we defined here in a module
- module Shapes (Point (..), Shape (..). surface, nudge, baseCircle, baseRect) where
- if we add deriving at the end of a data declaration, Haskell automagically makes that type part of the show typeclass
- data means that we're difining a new data type. The part before the = denotes the type
- To make our own type to represent a shape
- a shape can be a circle or a rectangle
- data Shape = Circle Float Float Float | Rectangle Float Float Float Float
- a shape can be a circle or a rectangle
- To make our own type to represent a shape
- If we wanted to export the functions and types that we defined here in a module
- data means that we're defining a new data type. Ther part before the=denotes the type
Kinds
- :k -> Type of a Type
- * mean a concrete type
- :k -> Type of a Type
The Functor
- For things who can be mapped out
- Using fmap
- For things who can be mapped out
Yes-no classes
- To play with the boolean definition
- class YesNo a where yesno :: a -> Bool
- Made an instance for each type
- To play with the boolean definition
Recursive data structures
- Declaration of inflix functions
- Left-associative
- inflix <code> <operator>
- Right-associative
- ifixr <code> <operator>
- Left-associative
- Declaration of inflix functions
Type synonyms
- They're just about givingsome types different names so that they make more sense to someone reading our code and documentation
- Type synonyms can also be parameterized
- if we want a type that represents an association list type
- type AssocList k v = [(k,v)]
- if we want a type that represents an association list type
- Type synonyms can also be parameterized
- They're just about givingsome types different names so that they make more sense to someone reading our code and documentation
Information
- Description
- Mind Map on Making our own types and typeclasses, created by AGUIAR MARCELLA LUIS CARLOS on 22/09/2019.
- Created by
- AGUIAR MARCELLA LUIS CARLOS
- Views
- 0
- Rating