-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch6.go
173 lines (146 loc) · 2.59 KB
/
ch6.go
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package main
import "fmt"
func main() {
//// Your second function
// xs := []float64{98, 93, 77, 82, 83}
// fmt.Println(average(xs))
//// Variadic functions
// fmt.Println(add(1, 2, 3))
// xs := []int{1, 2, 3}
// fmt.Println(add(xs...))
//// Closure
// add := func(x, y int) int {
// return x + y
// }
// fmt.Println(add(1, 1))
// x := 0
// increment := func() int {
// x++
// return x
// }
// fmt.Println(increment())
// fmt.Println(increment())
// nextEven := makeEvenGenerator()
// fmt.Println(nextEven()) // 0
// fmt.Println(nextEven()) // 2
// fmt.Println(nextEven()) // 4
//// Recursion
// fmt.Println(factorial(10))
//// defer, panic and recover
// defer second()
// first()
//// panic and recover
// defer func() {
// str := recover()
// fmt.Println(str)
// }()
// panic("PANIC")
//// Pointers
// x := 5
// zero(&x)
// fmt.Println(x) // x is 0
//// new
// xPtr := new(int)
// one(xPtr)
// fmt.Println(*xPtr) // x is 1
fmt.Println(exercise2(16))
fmt.Println(exercise3(16, 5))
gen := makeOddGenerator()
fmt.Println(gen())
fmt.Println(gen())
fmt.Println(gen())
fmt.Println(fib(20))
x := 1
y := 2
exercise11(&x, &y)
fmt.Println(x, y)
}
func average(xs []float64) float64 {
total := 0.0
for _, value := range xs {
total += value
}
return total / float64(len(xs))
}
func add(args ...int) int {
total := 0
for _, v := range args {
total += v
}
return total
}
func makeEvenGenerator() func() uint {
i := uint(0)
return func() (ret uint) {
ret = i
i += 2
return
}
}
func factorial(x uint) uint {
if x == 0 {
return 1
}
return x * factorial(x-1)
}
func first() {
fmt.Println("1st")
}
func second() {
fmt.Println("2nd")
}
func zero(x *int) {
*x = 0
}
func one(xPtr *int) {
*xPtr = 1
}
func exercise2(x int) (int, bool) {
return x / 2, x%2 == 0
}
func exercise3(x ...int) int {
var max int
for _, v := range x {
if v >= max {
max = v
}
}
return max
}
func makeOddGenerator() func() uint {
i := uint(1)
return func() (ret uint) {
ret = i
i += 2
return
}
}
func fib(x int) int {
if x == 0 {
return 0
}
if x == 1 {
return 1
}
return fib(x-1) + fib(x-2)
}
func exercise11(x, y *int) {
*x, *y = *y, *x
}
/* Exercises
1: func sum(a []int) uint
Fout: return is int niet uint
2: see exercise2()
3: see exercise3()
4: see makeOddGenerator() / main()
5: see fib()
6:
- defer runs calls after func completion
- panic stops execution
- recover stops the panic and is passed the input of panic
7: & operator
8: * operator; *x = 3
9: new operator; xPtr = new(int) or pass variable with &
10: 2.25
11: see exercise11()
*/