Skip to content

Commit

Permalink
Prepare lecture basics
Browse files Browse the repository at this point in the history
  • Loading branch information
s-macke committed Oct 13, 2024
1 parent aa86cd6 commit b0e6aeb
Show file tree
Hide file tree
Showing 13 changed files with 256 additions and 91 deletions.
2 changes: 1 addition & 1 deletion docs/01-1-About.slide
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Welcome
Concepts of Programming Languages - TH Rosenheim - WS 2023/2024
Concepts of Programming Languages - TH Rosenheim - WS 2024/2025
Tags: go, golang, programming, master

Sebastian Macke
Expand Down
155 changes: 121 additions & 34 deletions docs/02-Go Programming - Basics.slide
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,19 @@ Go Programming - Basics
Concepts of Programming Languages
Tags: go, programming, master

Sebastian Macke, Stefan Langer
Sebastian Macke
Rosenheim Technical University
Sebastian.Macke@th-rosenheim.de
https://www.qaware.de

* Organisational matters
- Some missing students last time
- Questions
* Last Lecture:

- Introduction
- The many ways to characterize a language
- Go Introduction

- Characteristics of a Successful Language
-- Miro Board ....

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


* What is an Orthogonal language

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

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

* Orthogonal Design by an example

- Type System is a "feature"
- Functions are a "feature"
- Types are used in functions as parameters or return values.
- Orthogonal would mean, that the type system is independent of the functions.
- E. g. The behaviour of the type is not influenced by a function or vice-versa.

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

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

* Primitive Types
- Basic building block of a programming language
- They are not defined in terms of other data types
- Usually designed with the CPU architecture in mind

* Primitive Types
.html snippets/basic_types.html
- In Go the type of a variable is behind the name!
Expand Down Expand Up @@ -159,13 +177,13 @@ Languages differ of how serious they handle the type
- statically typed: type checking at compile time
- dynamically typed: type checking at runtime

: Frage an Studenten: bitte ausfüllen.

Go uses a strong typing system. But why?

* Weak typing vs. Strong typing II
: Frage an Studenten: bitte ausfüllen.

.image img/02-typing.png 450 _
: * Weak typing vs. Strong typing II

: .image img/02-typing.png 450 _

* Where implicit type conversion in C goes wrong

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

Also: Change int to char


: https://www.onlinegdb.com/online_c_compiler
: change int to char

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

: https://jsconsole.com/
: 4 + "7"
: 4 * "7"
: 4 + "7" -> conversion to strings
: "4" + 7
: 4 - "7" -> conversion to number
: "4" - "7" -> conversion to number
: "" - "" => 0
: 4 * "7" -> conversion to numbers
: 2 + true
: false - 3
: 0 == "" -> true
: "" - "" => 0
: 0 == "" -> true
: new Array() == false
: {} + {} => "[object Object][object Object]" (typeof ({} + {}) is string)
: {} + [] => 0
: {} + {} + [] => "NaN" as string
: ({} + {}) + [] => ""
: {} + ({} + []) => NaN as number
: ({} + {} + []) => ("[object Object][object Object]")
: ("NaN") => "NaN"
: 0 > null => false
: 0 >= null => true
: 0 === null => false
: [1,2,3] + [4,5,6] => "1,2,34,5,6"
: http://www.jsfuck.com/
: ....
: ("NaN") => "NaN"
: ({} + {} +[]) => ("[object Object][object Object]")
: parseInt(1)
: parseInt(0.0000001)
: "" + 0.00000001

* Dynamically typing in Python

- Demo with state of the art AI libs

* Strong typing in Go

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

* Golang - some details
* The many ways of variable declarations in Go

var foo int // default value zero
foo = 32

* Go has a dynamic type: any
var foo int = 32

var foo = 32

foo := 32 // infers (guesses) the type definition of type int

foo := int(32)

foo, ok := 32, true // foo is an int 32, ok is a boolean with value true


* Go has also a dynamic type: any

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

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


* Type systems in other languages

.image img/02-typing.png 450 _
- Miro board ....

* Golang - some details


* Maps

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

.play ../src/basics/types/maps/maps.go /START OMIT/,/END OMIT/
.play ../src/basics/types/maps/maps.go
: /START OMIT/,/END OMIT/

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

arr := [5]int{1, 2, 3, 4, 5}

s := arr[0:2]

fmt.Println(len(s), cap(s)) // ==> 2, 5

s = append(s, 8)
s = append(s, 9)
s = append(s, 10)
s = append(s, 11)

fmt.Println(len(s), cap(s)) // ==> 6, 10
* Arrays and Slices

: slicecap zeigen
.play ../src/basics/types/slices/slices.go


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

pos, ch := range runes

: * Functions and Control Structures: Example String Reverse (UTF-8)

: Bad example of usage of multiple parameters
: .code ../src/basics/types/strings/strings.go /Reverse/,/End OMIT/


* Functions and Control Structures: Example Palindrome (Reverse)
.code ../src/basics/palindrome/palindrome.go /IsPalindrome3/,/END3 OMIT/
- Strings, arrays are compared in Go with ==
Expand Down Expand Up @@ -354,11 +403,46 @@ A pointer is a variable which contains a memory address
* Exercise 2.1
.link https://github.com/s-macke/concepts-of-programming-languages/blob/master/docs/exercises/Exercise2.1.md

* Pointer arithmetic is a "DON'T DO" in Go!
- There is some unsafe pointer arithmetic as in C / C ++

.play ../src/basics/pointers/unsafe/main.go /func main/,

- Because of garbage collection anything can happen here! Even more unsafe than in C!

: In C wäre die Anpassung des Pointers nur y++

: * What are references

: References are kind of safe pointers. In different languages they have slighty different meanings and properties.

: - They all have in common, that they don't allow pointer arithmetic.
: - Usually no dererefence with * necessary
: - Normally strongly typed
: - In C++ a reference must be initialized at creation and can never be null. In this way C++ ist stricter than Java
: - In Java because of Garbage Collection the reference might not be direct pointer to the object.
: - In Go, nil is the zero value for pointers, interfaces, maps, slices, channels and function types, representing an uninitialized value.

* Maps and Slices - Example Book Index
.code ../src/basics/index/index.go /Page/,/END OMIT/

Types can be defined with the keyword "type"

// Page contains an array of words.
type Page []string

// Book is an array of pages.
type Book []Page

// Index contains a list of pages for each word in a book.
type Index map[string][]int

// MakeIndex generates an index structure
func MakeIndex(book Book) Index {
....
}

* Stringer Interface
.code ../src/basics/interfaces/interfaces.go
.play ../src/basics/stringer/stringer.go

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

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

: * public static void
: - Thoughts about language design:
: .link https://www.youtube.com/watch?v=5kj5ApnhPAE

* Summary

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


: - Object-oriented, Functional, Parallel, Modular and Versioned

* Exercise 2.2
Expand Down
11 changes: 5 additions & 6 deletions docs/exercises/Exercise2.2.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ A Book Index is an inverted index which lists all pages a word occurs in a book.
Write a program which generates an inverted index out of an array of book pages.
Each Page contains an array of words.

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

1. Use the flag library to provide the following parameters
1. Use the flag library (https://tour.golang.org/methods/17) to provide the following parameters
```
Usage of find:
-path string
path to search (default ".")
-regex string
path (default ".*")
```

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
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
"regexp.MatchString" (https://pkg.go.dev/regexp#MatchString) function and print its path+name on the screen.
4. Either use "panic" or "log.Fatal" for error handling.
5. Run through directories recursively
3. Either use "panic" or "log.Fatal" for error handling.
4. Run through directories recursively

# Question
Go doesn't support Exceptions but uses multiple return values of which one can be the error information.
Expand Down
12 changes: 8 additions & 4 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
module github.com/s-macke/concepts-of-programming-languages

go 1.21
go 1.22.0

toolchain go1.23.0

require (
github.com/antlr/antlr4/runtime/Go/antlr v0.0.0-20210930093333-01de314d7883
github.com/coreos/etcd v3.3.26+incompatible
github.com/fogleman/gg v1.3.0
github.com/jacobsa/fuse v0.0.0-20210904154839-95fc8d118111
golang.org/x/net v0.5.0 // indirect
golang.org/x/sys v0.4.0
golang.org/x/text v0.6.0 // indirect
golang.org/x/net v0.30.0 // indirect
golang.org/x/sys v0.26.0
golang.org/x/text v0.19.0 // indirect
google.golang.org/genproto v0.0.0-20211001223012-bfb93cce50d9 // indirect
google.golang.org/grpc v1.41.0
google.golang.org/protobuf v1.27.1
Expand All @@ -21,6 +23,8 @@ require (
github.com/gogo/protobuf v1.3.2 // indirect
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/yuin/goldmark v1.4.13 // indirect
golang.org/x/exp v0.0.0-20220328175248-053ad81199eb // indirect
golang.org/x/image v0.0.0-20210628002857-a66eb6448b8d // indirect
golang.org/x/tools v0.26.0 // indirect
)
Loading

0 comments on commit b0e6aeb

Please sign in to comment.