Haskell¶
Functions¶
Picking function based on type¶
Given the type
1 |
|
1 2 |
|
Types¶
In
1 |
|
a
and b
are type variables
* "unknown types that we must find on the way"
* Eq
and Num
are type classes
* defines that every type a
from this class has some functions defined on it
* e.g. Eq a
-- a
has a function ==
with type a -> a -> Bool
Type and Term Constructor¶
Example
1 |
|
BTree
is a type constructor, since it is used to construct a type
* BLeaf
and BBranch
are term constructors, since they are used to build a term
Polymorphism¶
- Ad hoc-polymorphism
- In ad hoc-polymorphism, a polymorphic function really stands for a collection of different implementations corresponding to different uses.
- This is sometimes called overloading.
- Parametric polymorphism
- In parametric polymorphism, a polymorphic function is a general implementation with a general type.
Pattern Matching¶
(x:xs)
(list pattern) matches a non-empty list where the first element is bound tox
and the rest bound toxs
List Comprehension¶
Video about list comprehension
We can generate all even numbers with:
1 |
|
i <- [1..]
)
List comprehensions can be nested such as:
1 |
|
[[(1,1), (2,1)], [(1,2), (2,2)], ...]
- List comprehension =
- ranges (generators) +
- filters (guards)
Last update:
January 17, 2021