¡GoConqr estrena diseño! Cuéntanos qué te parece.

Mapa mental

Making Our Own Types and Typeclasses

TABARES ROMERO DIEGO MAURICIO Inglés

Making Our Own Types and Typeclasses

Contenido del mapa

Making Our Own Types and Typeclasses

  1. 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 create also own types by using brackets and specifying name of fields and types
  2. 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.
  3. 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)
  4. 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.
  5. 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
  6. 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
  7. 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.

Imágenes y archivos del mapa

  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses
  • Making Our Own Types and Typeclasses

Información

Descripción
Mapa Mental sobre Making Our Own Types and Typeclasses, creado por TABARES ROMERO DIEGO MAURICIO el 03/04/2020.
Creado por
TABARES ROMERO DIEGO MAURICIO · abr 2020
Actualizado
4 abr 2020
Visitas
1
Valoración

Escríbenos

¿Sobre qué es tu mensaje?

¿Qué te parece el nuevo diseño?

¿Te gusta?