Skip to content

Commit 255955b

Browse files
committed
Prepare lecture impure functional programming
1 parent 2be1fa6 commit 255955b

File tree

9 files changed

+122
-51
lines changed

9 files changed

+122
-51
lines changed

docs/03-Go-Programming-OOP.slide

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,26 +9,26 @@ https://www.qaware.de
99

1010
* Last lecture
1111
- Types (string, int, bool, float64, ...)
12-
- weak vs. strong typing
12+
- weak vs. strong typing, statically vs. dynamically typed.
1313
- Functions and Control Structures
1414
- Arrays, Slices and Maps
1515
- Pointer
1616
- Unit Tests
17-
- Preview into interfaces
1817

1918
* Last Exercise
2019
- Let's look at an example implementation
2120

2221
: Ergebnisse der Übungen zeigen.
2322

2423
* Error Handling
24+
.image ./img/error_meme.jpg 400 _
2525

2626
* Errors in Go
2727

2828
Go doesn't have try-catch exception based error handling.
2929
Go distinguishes between recoverable and unrecoverable errors.
3030

31-
Recoverable: e. g. file not found
31+
_Recoverable_: e. g. file not found
3232

3333
- Go returns the error as one of the return values
3434

@@ -37,7 +37,7 @@ Recoverable: e. g. file not found
3737
log.Error(err)
3838
}
3939

40-
Unrecoverable: array access outside its boundaries, out of memory
40+
_Unrecoverable_: array access outside its boundaries, out of memory
4141

4242
* Go defer: run code before function exists
4343

docs/04-Forth.slide

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,6 @@ Rosenheim Technical University
88
Sebastian.Macke@th-rosenheim.de
99
https://www.qaware.de
1010

11-
* Plan
12-
13-
- Presentations & Forth
14-
- Logic programming
15-
- WebAssembly
16-
- (Summary) / Clean Code / What language to use for which problem?
17-
- Presentations
18-
19-
2011
* Last lecture
2112
- Exception Handling. Error return codes vs. Exceptions
2213
- Dynamic Types in Go
@@ -124,12 +115,13 @@ Evaluate
124115
.image img/stack1.png 200 _
125116

126117
3 push 3 onto stack
127-
9 push 9 onto stack
128-
+ pop 9 and 3 and add them and push the result back on the stack
129-
4 push 4 onto stack
130-
6 push 6 onto stack
131-
+ pop 6 and 4 and add them and push the result back on the stack
132-
* pop 9 and 17 and multiply them and push the result back on the stack
118+
9 push 9 onto stack
119+
+ pop 3 and 9 from stack, add them, push result onto stack
120+
4 push 4 onto stack
121+
6 push 6 onto stack
122+
+ pop 4 and 6 from stack, add them, push result onto stack
123+
* pop 12 and 15 from stack, multiply them, push result onto stack
124+
133125

134126

135127
* Postfix via a stack machine
@@ -835,3 +827,9 @@ Algorithm:
835827

836828
.link https://github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise4.md
837829

830+
* Last lecture
831+
- Prefix, Infix and Post-Notation
832+
- Stack machine
833+
- Forth as one of the most simplistic languages
834+
- Metaprogramming, program new language features while programming
835+
- Shunting-Yard Algorithm

docs/05-Functional-Programming.slide

Lines changed: 64 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,23 @@ https://www.qaware.de
1111
: https://www.digicomp.ch/blog/2015/10/20/einfuehrung-in-die-funktionale-programmierung
1212

1313
* Last lecture
14-
- Prefix, Infix and Post-Notation
15-
- Stack machine
16-
- Forth as one of the most simplistic languages
17-
- Metaprogramming, program new language features while programming
18-
- Shunting-Yard Algorithm
14+
- Exception Handling. Error return codes vs. Exceptions
15+
- Object Oriented Programming without class hierarchy
16+
- Embedding
17+
- Implicit satisfied interfaces with Polymorphism
1918

2019
: Ergebnisse der Übungen zeigen.
2120

21+
* The error interface
22+
23+
.play ../src/oop/errors/error.go /START OMIT/,/END OMIT/
24+
25+
The error is just an interface with one method that should return the error message:
26+
27+
type error interface {
28+
Error() string
29+
}
30+
2231
* What is Functional programming?
2332
- Functional programming is a programming paradigm where functions can not only be defined and applied. Like data, functions can be linked together, used as parameters, and occur as function results.
2433
- In short: Functions are treated as first-class citizens.
@@ -38,7 +47,7 @@ Functional programming is old, but some major languages have adapted them quite
3847
These types of functional languages support only the functional paradigms and have no state. For example − Haskell.
3948

4049
- *Impure* Functional Languages
41-
These types of functional languages support the functional paradigms and imperative style programming. For example − LISP and Go.
50+
These types of functional languages support the functional paradigms and imperative style programming. For example all of the major languages today.
4251

4352
* Impure Functional Languages
4453

@@ -106,6 +115,30 @@ Rewrite it, so that the Bubble Sort algorithm takes a comparison function as inp
106115

107116
BubbleSort(data, func(i, j int) bool {return data[i] > data[j]})
108117

118+
* Closures in JavaScript
119+
120+
const object1 = {
121+
prop: 42,
122+
123+
func: function () {
124+
return this.prop;
125+
},
126+
};
127+
128+
console.log(object1.prop) // 42
129+
console.log(object1.func()) // 42 // At creation time, this points to test.prop
130+
131+
const object2 = {
132+
prop: 43,
133+
func: object1.func // take the function defined in object1.
134+
}
135+
136+
console.log(object2.prop) // 43
137+
console.log(object2.func()) // 43. Now "this" points to object2.
138+
139+
: https://onecompiler.com/javascript
140+
: object1.func = object1.func.bind(object1);
141+
109142
* Pure Functions
110143

111144
* What is a pure function?
@@ -124,23 +157,31 @@ Advantages
124157

125158
* Every Function can be made pure
126159

127-
"An interactive program is a pure function that takes the current 'state of the world' as its arg and produces a modified world as result" - unknown Twitter user
128160

129-
- This one is pure (Go)
161+
- This one is impure
130162

131-
var array []int
132-
newarray := append(array, 1)
163+
var count = 0
164+
func increment() {
165+
count++
166+
}
133167

134-
- This one is not (Java)
168+
- This one is pure
135169

136-
List<Integer> array = new ArrayList<Integer>
137-
arrlist.add(1);
170+
func incrementPure(count int) int {
171+
return count + 1 // This function is pure because it does not modify external state
172+
}
173+
174+
- Often functions are "pure enough" in the practical sense.
175+
var array []int
176+
newarray := append(array, 1)
138177

139178
- Question: How to make a pseudo random() function pure?
140179
- Question: How to make a real random() function pure?
141180

181+
: "An interactive program is a pure function that takes the current 'state of the world' as its arg and produces a modified world as result" - unknown Twitter user
142182
: Question: How to make a real random() function pure?
143183
: How to make a quasi-random number pure?
184+
: newarray := append(array, 1)
144185
: TODO: separate slide maybe?
145186

146187
* Pure Functional Languages
@@ -213,11 +254,11 @@ Functional programming supports distributed computing
213254
.... Do something else
214255
partialApplicatedFunction(3) -> 4
215256

216-
* Currying allows for Partial Application
257+
: * Currying allows for Partial Application
217258

218-
.play ../src/functional/currying/main.go
259+
: .play ../src/functional/currying/main.go
219260

220-
- Partial application is used here as a form of a constructor
261+
: - Partial application is used here as a form of a constructor
221262

222263
* Functional Composition
223264
Functions can be composed to new functions
@@ -313,6 +354,7 @@ _(λx.x)_ _a_
313354
: To apply a function
314355
: (λx.x) 5 = 5
315356

357+
: schlechtes Beispiel
316358
: (λa.λb.(a+b)) 5 6 = 11
317359
: ((λa.λb.(a+b)) 5) 6 = 11
318360
: (λ5.λb.(5+b)) 6 = 11
@@ -321,6 +363,8 @@ _(λx.x)_ _a_
321363

322364
: T := λa.λb.a is the function which takes two parameters and returns the first one
323365
: F := λa.λb.b is the function which takes two parameters and returns the second one
366+
: brackets ein zeichnen
367+
: T 3 5
324368
: (λx.3) 5 = 3
325369
: (λa.λb.a) 5 6 = 5
326370
: (λa.λb.b) 5 6 = 6
@@ -331,15 +375,15 @@ _(λx.x)_ _a_
331375
: AND True True = True True False = True
332376
: AND False True = False True False = False
333377

334-
Alternative:
378+
: Alternative:
335379
: OR := λa.λb.(a b a)
336380
: OR := λa.λb.(a a b)
337-
338-
: ifelse = λp.λa.λb.(p a b)
339-
: ifelse true 42 58 = true 42 58 = 42
340381
: or = λp.λq.(p p q)
341382
: alternative for not = λp.λa.λb.(p b a)
342383

384+
: ifelse = λp.λa.λb.(p a b)
385+
: ifelse T 42 58 = T 42 58 = 42
386+
343387
: Y combinator
344388
: Y := λf.(λx.f (x x)) (λx.f (x x))
345389
: Y g = λf.(λx.f (x x)) (λx.f (x x)) g

docs/img/error_meme.jpg

89 KB
Loading

src/basics/find/find.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,21 @@ package main
33
import (
44
"flag"
55
"fmt"
6-
"io/ioutil"
76
"log"
87
"os"
98
"regexp"
109
)
1110

1211
func ListDir(path string, pattern string) {
13-
files, err := ioutil.ReadDir(path)
12+
files, err := os.ReadDir(path)
1413
if err != nil {
1514
log.Fatal(err)
1615
}
1716
for _, f := range files {
1817
name := path + string(os.PathSeparator) + f.Name()
1918
b, err := regexp.MatchString(pattern, f.Name())
2019
if err != nil {
20+
2121
log.Fatal(err)
2222
}
2323
if b {

src/basics/index/index.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ package index
66
import "fmt"
77

88
// Page contains an array of words.
9-
type Page []string
9+
type Page struct {
10+
words []string
11+
}
1012

1113
// Book is an array of pages.
1214
type Book []Page
@@ -17,8 +19,8 @@ type Index map[string][]int
1719
// MakeIndex generates an index structure
1820
func MakeIndex(book Book) Index {
1921
idx := make(Index)
20-
for i, page := range book {
21-
for _, word := range page {
22+
for i := range book {
23+
for _, word := range book[i].words {
2224
pages := idx[word]
2325
idx[word] = append(pages, i)
2426
}

src/oop/delegation/delegation.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ type B struct {
1919

2020
func (b B) Foo() {
2121
fmt.Print("b.foo ")
22-
b.Bar() //b.A.Bar()
22+
//b.Bar() //b.A.Bar()
23+
b.A.Bar()
2324
}
2425

2526
func (b B) Bar() {
@@ -29,8 +30,8 @@ func (b B) Bar() {
2930
func main() {
3031
a := A{}
3132
a.Foo()
32-
/*
33-
b := B{}
34-
b.Foo() // "a.bar" or "b.bar"?
35-
*/
33+
34+
b := B{}
35+
b.Foo() // "a.bar" or "b.bar"?
36+
3637
}

src/oop/errors/error.go

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package main
2+
3+
import (
4+
"errors"
5+
"fmt"
6+
)
7+
8+
// START OMIT
9+
func divide(a, b int) (int, error) {
10+
if b == 0 {
11+
return 0, errors.New("division by zero")
12+
}
13+
return a / b, nil
14+
}
15+
16+
func main() {
17+
// Successful division
18+
result, err := divide(10, 0)
19+
if err != nil {
20+
fmt.Println("Error:", err)
21+
} else {
22+
fmt.Println("Result:", result)
23+
}
24+
}
25+
26+
// END OMIT

src/oop/rational/rational.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,11 +15,11 @@ type Rational struct {
1515
// END1 OMIT
1616

1717
// NewRational constructor function
18-
func NewRational(numerator int, denominator int) Rational {
18+
func NewRational(numerator int, denominator int) *Rational {
1919
if denominator == 0 {
2020
panic("division by zero")
2121
}
22-
r := Rational{}
22+
r := &Rational{}
2323
divisor := gcd(numerator, denominator)
2424
r.numerator = numerator / divisor
2525
r.denominator = denominator / divisor

0 commit comments

Comments
 (0)