Skip to content

Commit b0e6aeb

Browse files
committed
Prepare lecture basics
1 parent aa86cd6 commit b0e6aeb

File tree

13 files changed

+256
-91
lines changed

13 files changed

+256
-91
lines changed

docs/01-1-About.slide

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Welcome
2-
Concepts of Programming Languages - TH Rosenheim - WS 2023/2024
2+
Concepts of Programming Languages - TH Rosenheim - WS 2024/2025
33
Tags: go, golang, programming, master
44

55
Sebastian Macke

docs/02-Go Programming - Basics.slide

Lines changed: 121 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,19 @@ Go Programming - Basics
22
Concepts of Programming Languages
33
Tags: go, programming, master
44

5-
Sebastian Macke, Stefan Langer
5+
Sebastian Macke
66
Rosenheim Technical University
77
Sebastian.Macke@th-rosenheim.de
88
https://www.qaware.de
99

10-
* Organisational matters
11-
- Some missing students last time
12-
- Questions
10+
* Last Lecture:
11+
12+
- Introduction
13+
- The many ways to characterize a language
14+
- Go Introduction
15+
16+
- Characteristics of a Successful Language
17+
-- Miro Board ....
1318

1419
* Characteristics of a Successful Language (Suggestion)
1520
- Simplicity and readability (Small instruction set and easy syntax)
@@ -20,7 +25,6 @@ https://www.qaware.de
2025
- Good support and documentation (public compilers, tutorials, community, IDE)
2126
- Orthogonality
2227

23-
2428
* What is an Orthogonal language
2529

2630
- Def 1: A language is orthogonal if its features are built upon a small, mutually *independent* set of primitive operations.
@@ -32,6 +36,13 @@ https://www.qaware.de
3236

3337
: http://www.javawenti.com/?post=39930
3438

39+
* Orthogonal Design by an example
40+
41+
- Type System is a "feature"
42+
- Functions are a "feature"
43+
- Types are used in functions as parameters or return values.
44+
- Orthogonal would mean, that the type system is independent of the functions.
45+
- E. g. The behaviour of the type is not influenced by a function or vice-versa.
3546

3647
* Types VS. Functions (Non-Orthogonal Design by example)
3748
- E. g. in C, the type system is not orthogonal to the function system.
@@ -126,9 +137,16 @@ https://www.qaware.de
126137
- The entry point for Go executable is always the function main in package main (main.main)
127138
- New packages need a new directory and can then be referenced
128139
- *Public* *functions* *begin* *with* *a* *capital* *letter*
140+
: - Package import names are hierarchical
141+
: - Package names are not hierarchical!
129142

130143
: neues Verzeichnis mit Paket anlegen und zeigen, wie man von main darauf zugreift
131144

145+
* Primitive Types
146+
- Basic building block of a programming language
147+
- They are not defined in terms of other data types
148+
- Usually designed with the CPU architecture in mind
149+
132150
* Primitive Types
133151
.html snippets/basic_types.html
134152
- In Go the type of a variable is behind the name!
@@ -159,13 +177,13 @@ Languages differ of how serious they handle the type
159177
- statically typed: type checking at compile time
160178
- dynamically typed: type checking at runtime
161179

162-
: Frage an Studenten: bitte ausfüllen.
163-
164180
Go uses a strong typing system. But why?
165181

166-
* Weak typing vs. Strong typing II
182+
: Frage an Studenten: bitte ausfüllen.
167183

168-
.image img/02-typing.png 450 _
184+
: * Weak typing vs. Strong typing II
185+
186+
: .image img/02-typing.png 450 _
169187

170188
* Where implicit type conversion in C goes wrong
171189

@@ -180,34 +198,46 @@ Go uses a strong typing system. But why?
180198
return 0;
181199
}
182200

201+
Also: Change int to char
202+
203+
183204
: https://www.onlinegdb.com/online_c_compiler
184205
: change int to char
185206

186207
* Javascript is very creative with type coercion
187208
.image ./img/banana_meme.png 400 _
188209

189210
: https://jsconsole.com/
190-
: 4 + "7"
191-
: 4 * "7"
211+
: 4 + "7" -> conversion to strings
212+
: "4" + 7
213+
: 4 - "7" -> conversion to number
214+
: "4" - "7" -> conversion to number
215+
: "" - "" => 0
216+
: 4 * "7" -> conversion to numbers
192217
: 2 + true
193218
: false - 3
194-
: 0 == "" -> true
195219
: "" - "" => 0
220+
: 0 == "" -> true
196221
: new Array() == false
197222
: {} + {} => "[object Object][object Object]" (typeof ({} + {}) is string)
198223
: {} + [] => 0
199224
: {} + {} + [] => "NaN" as string
200225
: ({} + {}) + [] => ""
201226
: {} + ({} + []) => NaN as number
227+
: ({} + {} + []) => ("[object Object][object Object]")
228+
: ("NaN") => "NaN"
202229
: 0 > null => false
203230
: 0 >= null => true
204231
: 0 === null => false
205232
: [1,2,3] + [4,5,6] => "1,2,34,5,6"
206233
: http://www.jsfuck.com/
207-
: ....
208-
: ("NaN") => "NaN"
209-
: ({} + {} +[]) => ("[object Object][object Object]")
234+
: parseInt(1)
210235
: parseInt(0.0000001)
236+
: "" + 0.00000001
237+
238+
* Dynamically typing in Python
239+
240+
- Demo with state of the art AI libs
211241

212242
* Strong typing in Go
213243

@@ -217,19 +247,43 @@ Go uses a strong typing system. But why?
217247
- The compiler will complain if you try to compare different types.
218248
- You have to do the type conversion explicitly.
219249

220-
* Golang - some details
250+
* The many ways of variable declarations in Go
251+
252+
var foo int // default value zero
253+
foo = 32
221254

222-
* Go has a dynamic type: any
255+
var foo int = 32
256+
257+
var foo = 32
258+
259+
foo := 32 // infers (guesses) the type definition of type int
260+
261+
foo := int(32)
262+
263+
foo, ok := 32, true // foo is an int 32, ok is a boolean with value true
264+
265+
266+
* Go has also a dynamic type: any
223267

224268
.play ../src/basics/dynamic/main.go /START OMIT/,/END OMIT/
225269

226270
: - The downcast is safe (== dynamic_cast in C++ or cast in Java)
227271

272+
273+
* Type systems in other languages
274+
275+
.image img/02-typing.png 450 _
276+
- Miro board ....
277+
278+
* Golang - some details
279+
280+
228281
* Maps
229282

230283
- Maps are Go's built-in associative data type (sometimes called hashes or dicts in other languages).
231284

232-
.play ../src/basics/types/maps/maps.go /START OMIT/,/END OMIT/
285+
.play ../src/basics/types/maps/maps.go
286+
: /START OMIT/,/END OMIT/
233287

234288
* Arrays and Slices
235289
- Arrays have a fixed length and can not be resized
@@ -238,20 +292,9 @@ Go uses a strong typing system. But why?
238292
- The underlying array grows automatically (if needed)
239293
- Both have a length and a capacity: What does that mean?
240294

241-
arr := [5]int{1, 2, 3, 4, 5}
242-
243-
s := arr[0:2]
244-
245-
fmt.Println(len(s), cap(s)) // ==> 2, 5
246-
247-
s = append(s, 8)
248-
s = append(s, 9)
249-
s = append(s, 10)
250-
s = append(s, 11)
251-
252-
fmt.Println(len(s), cap(s)) // ==> 6, 10
295+
* Arrays and Slices
253296

254-
: slicecap zeigen
297+
.play ../src/basics/types/slices/slices.go
255298

256299

257300
* Functions and Control Structures: Example Palindrome
@@ -276,6 +319,12 @@ Go uses a strong typing system. But why?
276319

277320
pos, ch := range runes
278321

322+
: * Functions and Control Structures: Example String Reverse (UTF-8)
323+
324+
: Bad example of usage of multiple parameters
325+
: .code ../src/basics/types/strings/strings.go /Reverse/,/End OMIT/
326+
327+
279328
* Functions and Control Structures: Example Palindrome (Reverse)
280329
.code ../src/basics/palindrome/palindrome.go /IsPalindrome3/,/END3 OMIT/
281330
- Strings, arrays are compared in Go with ==
@@ -354,11 +403,46 @@ A pointer is a variable which contains a memory address
354403
* Exercise 2.1
355404
.link https://github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise2.1.md
356405

406+
* Pointer arithmetic is a "DON'T DO" in Go!
407+
- There is some unsafe pointer arithmetic as in C / C ++
408+
409+
.play ../src/basics/pointers/unsafe/main.go /func main/,
410+
411+
- Because of garbage collection anything can happen here! Even more unsafe than in C!
412+
413+
: In C wäre die Anpassung des Pointers nur y++
414+
415+
: * What are references
416+
417+
: References are kind of safe pointers. In different languages they have slighty different meanings and properties.
418+
419+
: - They all have in common, that they don't allow pointer arithmetic.
420+
: - Usually no dererefence with * necessary
421+
: - Normally strongly typed
422+
: - In C++ a reference must be initialized at creation and can never be null. In this way C++ ist stricter than Java
423+
: - In Java because of Garbage Collection the reference might not be direct pointer to the object.
424+
: - In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.
425+
357426
* Maps and Slices - Example Book Index
358-
.code ../src/basics/index/index.go /Page/,/END OMIT/
427+
428+
Types can be defined with the keyword "type"
429+
430+
// Page contains an array of words.
431+
type Page []string
432+
433+
// Book is an array of pages.
434+
type Book []Page
435+
436+
// Index contains a list of pages for each word in a book.
437+
type Index map[string][]int
438+
439+
// MakeIndex generates an index structure
440+
func MakeIndex(book Book) Index {
441+
....
442+
}
359443

360444
* Stringer Interface
361-
.code ../src/basics/interfaces/interfaces.go
445+
.play ../src/basics/stringer/stringer.go
362446

363447
* The Flag API simplifies Command Line Utilities
364448
.play ../src/basics/flags/main.go /import/,/END OMIT/
@@ -367,6 +451,10 @@ A pointer is a variable which contains a memory address
367451

368452
: vergleich https://stackoverflow.com/questions/7341683/parsing-arguments-to-a-java-command-line-program
369453

454+
: * public static void
455+
: - Thoughts about language design:
456+
: .link https://www.youtube.com/watch?v=5kj5ApnhPAE
457+
370458
* Summary
371459

372460
- Compiled (cross compiler for OS: Mac, Windows, Linux and CPU: ARM, x86 + Amd64)
@@ -375,7 +463,6 @@ A pointer is a variable which contains a memory address
375463
- Simple (less keywords like C - 25/32) and Orthogonal
376464
- Strong type system with runtime support (Reflection, Dynamic Types)
377465

378-
379466
: - Object-oriented, Functional, Parallel, Modular and Versioned
380467

381468
* Exercise 2.2

docs/exercises/Exercise2.2.md

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ A Book Index is an inverted index which lists all pages a word occurs in a book.
88
Write a program which generates an inverted index out of an array of book pages.
99
Each Page contains an array of words.
1010

11-
- Define custom types for Book, Page and Index
11+
- USe the "type" keyword to define custom types for Book, Page and Index.
1212
- Make sure the Stringer() interface is implemented for Book and Index to make them printable
1313
More details about the Stringer interface: https://tour.golang.org/methods/17
1414
The stringer interface will be explained in more detail in the next lecture.
@@ -21,19 +21,18 @@ Each Page contains an array of words.
2121
# Usage of the Library functions and error handling
2222
Write a program "find" that searches the filesystem recursively from a given path and regex expression.
2323

24-
1. Use the flag library to provide the following parameters
24+
1. Use the flag library (https://tour.golang.org/methods/17) to provide the following parameters
2525
```
2626
Usage of find:
2727
-path string
2828
path to search (default ".")
2929
-regex string
3030
path (default ".*")
3131
```
32-
33-
3. Use the ioutil.ReadDir (https://pkg.go.dev/io/ioutil@go1.17.2#ReadDir) function to list the contents of a given directory. Check each file for the given regex with the
32+
2. Use the ioutil.ReadDir (https://pkg.go.dev/io/ioutil@go1.17.2#ReadDir) function to list the contents of a given directory. Check each file for the given regex with the
3433
"regexp.MatchString" (https://pkg.go.dev/regexp#MatchString) function and print its path+name on the screen.
35-
4. Either use "panic" or "log.Fatal" for error handling.
36-
5. Run through directories recursively
34+
3. Either use "panic" or "log.Fatal" for error handling.
35+
4. Run through directories recursively
3736

3837
# Question
3938
Go doesn't support Exceptions but uses multiple return values of which one can be the error information.

go.mod

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
module github.com/s-macke/concepts-of-programming-languages
22

3-
go 1.21
3+
go 1.22.0
4+
5+
toolchain go1.23.0
46

57
require (
68
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210930093333-01de314d7883
79
github.com/coreos/etcd v3.3.26+incompatible
810
github.com/fogleman/gg v1.3.0
911
github.com/jacobsa/fuse v0.0.0-20210904154839-95fc8d118111
10-
golang.org/x/net v0.5.0 // indirect
11-
golang.org/x/sys v0.4.0
12-
golang.org/x/text v0.6.0 // indirect
12+
golang.org/x/net v0.30.0 // indirect
13+
golang.org/x/sys v0.26.0
14+
golang.org/x/text v0.19.0 // indirect
1315
google.golang.org/genproto v0.0.0-20211001223012-bfb93cce50d9 // indirect
1416
google.golang.org/grpc v1.41.0
1517
google.golang.org/protobuf v1.27.1
@@ -21,6 +23,8 @@ require (
2123
github.com/gogo/protobuf v1.3.2 // indirect
2224
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
2325
github.com/golang/protobuf v1.5.2 // indirect
26+
github.com/yuin/goldmark v1.4.13 // indirect
2427
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb // indirect
2528
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
29+
golang.org/x/tools v0.26.0 // indirect
2630
)

0 commit comments

Comments
 (0)