-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathhaskell.txt
52 lines (39 loc) · 1.81 KB
/
haskell.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
Concepts
--------
In purely functional programming you don't tell the computer what to do as such
but rather you tell it what stuff is in the form of functions.
If you say that a is 5, you can't say it's something else, a is immutable.
In purely functional languages, a function has no side-effects. The only thing a
function can do is calculate something and return it as a result. If a function
is called twice with the same parameters, it's guaranteed to return the same
result. That's called referential transparency.
Haskell is lazy. That means that unless specifically told otherwise, Haskell
won't execute functions and calculate things until it's really forced to show
you a result.
Haskell uses a very good type system that has type inference. Type inference
allows your code to be more general.
Functor
-------
instance Functor (Either a) where -- Either a has nothing to do with currying, it is simple substitution
fmap f (Right x) = Right (f x) -- Right is usually used for the result
fmap f (Left x) = Left x -- Left is usually used for the error
:t fmap
Functor f => (x -> y) -> f x -> f y
So we would have ... (x -> y) -> Either a x -> Either a y
-------- --------
To match Either type1 type2, \ /
we use Right x Left unchanged
and Left x
--------------------------------------------------------------------------------
Functions are functors
g
instance Functor ((->) r) where
fmap f g = (\x -> f (g x))
g x f (g x)
OR
fmap = (.), so f . g is the same as fmap f g
a -> b is a type, the type of a function =>
(->) a is a type constructor expecting one parameter only
Resemblances: [2,11,..] :: [Int]
Just 'a' :: Maybe Char
g 2.3 :: r -> Float