chapter1
- 重要的概念 Important Concept
- 函数式 functional
- 纯净 pure
- 惰性 lazy
- 严格类型 statically typed
- 抽象 abstraction
- 基本数据类型 basic type
- Int
- Integer
- Double
- Float
- Bool
- Char
- String
- 模式匹配 pattern matching
- 守卫 guards
- 组 pair
- 列表 List
chapter 2
代数数据类型 Algebraic data type
1
2
3
4data AlgDataType = Constr1 Type11 Type12
| Constr2 Type21
| Constr3 Type31 Type32 Type33
| Constr4x@pat
1
2baz :: Person -> String
baz p@(Person n _ _) = "The name field of (" ++ show p ++ ") is " ++ ncase
1
2case "HELLO" of
_ -> 7递归数据类型 recursive data types
1
2data Tree = Leaf Char
| Node Tree Int Tree
chapter 3
多态 Polymorphism
多态数据 Polymorphic data types
1
data List t = E | C t (List t)
多态函数 Polymorphic functions
1
filterList :: (t -> Bool) -> List t -> List t
标准库 prelude
- map / filter /fold
- Maybe
- 完整和部分函数 Total and partial functions
1
head [] -- 抛出错误 无法处理空列表
chapter 4
匿名函数 anoymous functions
1
\x-> x > 100
函数组合 functions composition
1
(+3) . (+2) $ 2
科里化 curry
- 函数只接受一个参数
- function arrow 是左结合的
- function application 是右结合的
chapter 5
- Parametricity
type class
example
1
2
3
4
5
6
7
8
9
10
11
12class Eq a where
(==) :: a -> a -> Bool
(/=) :: a -> a -> Bool
x /= y = not (x == y)
data Foo = F Int | G Char
instance Eq Foo where
(F i1) == (F i2) = i1 == i2
(G c1) == (G c2) = c1 == c2
_ == _ = False
foo1 /= foo2 = not (foo1 == foo2)common type class
- Eq
- Show
- Ord
- Num
- Read
- Type classes and Java interfaces
chapter 6
- 严格求值 Strict evaluation
- easy to predit
- 副作用 side effect
- time sensitive -> Lazy forces pure
- 惰性求值 Lazy evaluation
- when some expression is given as an argument to a function, it is simply packaged up as an unevaluated expression (called a “thunk”
- 匹配导致求值 Pattern matching drives evaluation
- Expressions are only evaluated when pattern-matched
- …only as far as necessary for the match to proceed, and no farther!