Taking Notes from http://learnyouahaskell.com
Algebraic Data Types
Option 1: data
data ValueName = Value Constructor
Shape is type, but Circle is not.
[]、False 或 5,它们都是不包含参数的值构造子
|
|
Example
|
|
deriving
|
|
Export in module
|
|
.. means exports all of Value Contructors
Options 2: Record Syntax
|
|
Options 3: Type parameters
|
|
- a is Type Parameter
- Maybe is Type Constructor
- Maybe String is Type
Haskell 中有一个严格的约定,那就是永远不要在 data 声明中添加类型约束
Options 4: Derived instances
|
|
Options 5: Type Synonyms
type alias
|
|
Recursive data structures
|
|
|
|
Infix Op
- 我们可以只用特殊字符来定义函数,这样他们就会自动具有中缀的性质
- 在 Value Constructor 上也可以使用
|
|
priority * > :-:
Typeclasses
Class and Instance
typeclasses are like interfaces
|
|
- class is for defining new typeclasses
- instance is for making our types instances of typeclasses
Subclass
|
|
(Eq a) is class contraints
|
|
- Maybe is not type, it’s type constructor.
- Maybe m is a type.
- so cannot write like this: instance Eq Maybe where
- (Maybe m) is a for preivous example
|
|
This is required for comparing value inside Maybe.
Example: Yes/No Typeclass
|
|
id is function :: a -> a
|
|
A if statement function
Functor Typeclass
|
|
- f needs a Type Constructor
- f is not real type, such as Int Bool and Maybe Int
- Maybe is a Type Constructor
map is a fmap that works only on Lists
Warning: we cannot use [a] at here, Remember we need a Type Constructor
|
|
|
|
- Either a an instance instead of just Either
- Either takes two parameter
Kind
- :k return kind info
- A * means that the type is a concrete type
|
|