Browse Source

Notes for 07 mar.

Abhinav Sarkar 4 years ago
parent
commit
70d9ff0a1d
1 changed files with 87 additions and 0 deletions
  1. 87
    0
      2016-03-07.md

+ 87
- 0
2016-03-07.md View File

@@ -0,0 +1,87 @@
1
+Day 5
2
+- Homework from day 4
3
+- Higher order programming
4
+  - Anonymous functions
5
+  
6
+```λ> let f = \x y -> x `div` y```
7
+  - Function composition
8
+  
9
+```haskell
10
+λ> let isEvenLengthTree t = even (length (inOrder t))
11
+λ> let compose f g = \x -> f (g x)
12
+λ> :t compose
13
+compose :: (b -> c) -> (a -> b) -> a -> c
14
+λ> let isEvenLengthTree t = (even `compose` length `compose` inOrder) t
15
+λ> :t isEvenLengthTree
16
+isEvenLengthTree :: BST -> Bool
17
+λ> :t (.)
18
+(.) :: (b -> c) -> (a -> b) -> a -> c
19
+λ> let isEvenLengthTree t = (even . length . inOrder) t
20
+λ> :t ($)
21
+($) :: (a -> b) -> a -> b
22
+λ> length $ [1..10]
23
+10
24
+λ> :i ($)
25
+($) :: (a -> b) -> a -> b 	-- Defined in ‘GHC.Base’
26
+infixr 0 $
27
+λ> let isEvenLengthTree t = even . length . inOrder $ t
28
+λ> :t isEvenLengthTree
29
+isEvenLengthTree :: BST -> Bool
30
+```
31
+  - Currying
32
+  - Partial application
33
+```haskell
34
+λ> let t = fromList [1..10]
35
+λ> :t insert t
36
+insert t :: Int -> BST
37
+λ> let insertIntoT = insert t
38
+λ> t
39
+Node (Node (Node (Node (Node (Node (Node (Node (Node (Node EmptyNode 1 EmptyNode) 2 EmptyNode) 3 EmptyNode) 4 EmptyNode) 5 EmptyNode) 6 EmptyNode) 7 EmptyNode) 8 EmptyNode) 9 EmptyNode) 10 EmptyNode
40
+λ> insertIntoT 30
41
+Node (Node (Node (Node (Node (Node (Node (Node (Node (Node EmptyNode 1 EmptyNode) 2 EmptyNode) 3 EmptyNode) 4 EmptyNode) 5 EmptyNode) 6 EmptyNode) 7 EmptyNode) 8 EmptyNode) 9 EmptyNode) 10 (Node EmptyNode 30 EmptyNode)
42
+λ> insertIntoT (-30)
43
+Node (Node (Node (Node (Node (Node (Node (Node (Node (Node (Node EmptyNode (-30) EmptyNode) 1 EmptyNode) 2 EmptyNode) 3 EmptyNode) 4 EmptyNode) 5 EmptyNode) 6 EmptyNode) 7 EmptyNode) 8 EmptyNode) 9 EmptyNode) 10 EmptyNode
44
+λ> :t insertIntoT
45
+insertIntoT :: Int -> BST
46
+λ> :t uncurry
47
+uncurry :: (a -> b -> c) -> (a, b) -> c
48
+λ> :t uncurry insert
49
+uncurry insert :: (BST, Int) -> BST
50
+λ> :t curry . uncurry $ insert
51
+curry . uncurry $ insert :: BST -> Int -> BST
52
+λ> (find 99 (insert 4 (fromList [1..10])))
53
+False
54
+λ> find 99 . insert 4 . fromList $ [1..10]
55
+False
56
+λ> find 99 $ insert 4 $ fromList $ [1..10]
57
+False
58
+λ> let f = find 99 . insert 4 . fromList
59
+λ> f [1..10]
60
+False
61
+λ> :t flip
62
+flip :: (a -> b -> c) -> b -> a -> c
63
+λ> :t find
64
+find :: Int -> BST -> Bool
65
+λ> :t flip find
66
+flip find :: BST -> Int -> Bool
67
+λ> :m +Data.Function
68
+λ> :t (&)
69
+(&) :: a -> (a -> b) -> b
70
+λ> [1..10] & fromList & insert 4 & find 99
71
+False
72
+λ> let f |> g = \x -> g . f $ x
73
+λ> let f = fromList |> insert 4 |> find 99
74
+λ> :t (|>)
75
+(|>) :: (r -> b) -> (b -> c) -> r -> c
76
+λ>  [1..1] & fromList |> insert 4 |> find 99
77
+False
78
+λ> fromList |> insert 4 |> find 99 $ [1..10]
79
+False
80
+λ> fromList |> (find 99 . insert 4) $ [1..10]
81
+False
82
+```
83
+  
84
+Homework: write these functions
85
+- map :: (a -> b) -> [a] -> [b]
86
+- filter :: (a -> Bool) -> [a] -> [a]
87
+- fold :: (r -> a -> r) -> r -> [a] -> r

Loading…
Cancel
Save