Mapa mental
Making Our Own Types and Typeclasses
Contenido del mapa
Making Our Own Types and Typeclasses
Algebraic data types examples
- data Bool = False | True
- data Int = -2147483648 | -2147483647 | ... | -1 | 0 | 1 | 2 | ... | 2147483647
Create type
- data
- To add functionalities
- deriving
- simple implementation
- data Car = Car String String Int deriving (Show)
- best implementation
- data Car = Car {company :: String, model :: String, year :: Int} deriving (Show)
- multiple defining
- |
- To add functionalities
- data
type parameters
- constructors
- data Maybe a = Nothing | Just a
- rename parameters into functions
- tellCar :: Car -> String tellCar (Car {company = c, model = m, year = y}) = "This " ++ c ++ " " ++ m ++ " was made in " ++ show y
- rename parameters into functions
- data Maybe a = Nothing | Just a
- derived instances
- deriving (Eq)
- compare types
- add functionality of class
- deriving (Eq, Show, Read)
- deriving (Eq)
- constructors
Type synonyms
- type String = [Char]
- types are equivalent and interchangeable
- type AssocList k v = [(k,v)]
Recursive data structures
- value of some type contains values of that type
- 3:(4:(5:6:[]))
- data Tree a = EmptyTree | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
- a -> Tree a - > Tree a
- value of some type contains values of that type
Typeclasses 102
- interfaces
- equality
- comparing
- ordering
- enumeration
- class Eq a where
- instance Eq TrafficLight where
- class (Eq a) => Num a where
- interfaces
Functor typeclass
- mapping
- read class
- (==) :: (Eq a) => a -> a -> Bool
- map :: (a -> b) -> [a] -> [b]
- mapping
Kinds and some type-foo
- Family type
- :k <type>
- :k Int
- Int :: *
- :k Int
- :k <type>
- data Barry t k p = Barry { yabba :: p, dabba :: t k }
- Barry :: (* -> *) -> * -> * -> *
- Family type
Información
- Descripción
- Mapa Mental sobre Making Our Own Types and Typeclasses, creado por Samuel DELGADO MUÑOZ el 23/09/2018.
- Creado por
- Samuel DELGADO MUÑOZ
- Visitas
- 0
- Valoración
Más sobre Data Types and Typeclasses