diff --git a/src/00-introduction.md b/src/00-introduction.md deleted file mode 100644 index 04fa14eb073b57dce4f1824f0debae9362e27143..0000000000000000000000000000000000000000 --- a/src/00-introduction.md +++ /dev/null @@ -1,35 +0,0 @@ -# Introduction - -## Me - -**Hello!** - -Nicolas Lenz - -- Master student, first semester -- Fachschaftsrat -- FOSS-AG -- Functional programming/theoretical computer science fan - -## Contact & Links - -**Mail:** \ -[nicolas@eisfunke.com](mailto:nicolas@eisfunke.com) - -**Homepage** (with more contact data and links to projects): \ -[eisfunke.com](https://www.eisfunke.com) - -**My GitLab** (with my projects, including Haskell stuff): \ -[git.eisfunke.com](https://git.eisfunke.com) - -## Interaction & Questions - -Interaction is important. Don't hesitate to ask questions! - -Questions not relevant to everybody will be answered in group phase. - -## Material - -Very good, fun to read, free to read online and base for this workshop: - -*Learn You a Haskell for Great Good!* by Miran Lipovača (<http://learnyouahaskell.com/>) diff --git a/src/00-motivation.md b/src/00-motivation.md new file mode 100644 index 0000000000000000000000000000000000000000..e2df28186220b2f42b23825e6d20564049e294fd --- /dev/null +++ b/src/00-motivation.md @@ -0,0 +1,301 @@ +# Motivation + +## Motivation + +::: important +Why learn functional programming? +::: + +::: {.uncoverenv slides="2-"} +::: center +To answer that question \ +we have to find out \ +what functional programming is. +::: +::: + +## What is Functional Programming? + +- High-level distinction: *imperative* vs. *functional* +- Most popular languages like Java, Python are mainly imperative + +## Imperative + +- Programming lanuages like Java or C are **imperative** +- Program = *sequence of commands*, executed one after another +- Functions = pieces of code to be included, can do anything + +## Imperative Example + +```java +x = 2 +doThing(x) +x = x + 4 +doThing(x) +``` + +- Statements are executed one after another +- Value of `x` is reassigned +- `doThing(x)` uses *current value* of `x` +- `doThing` could do anything + + +## Functional + +- Different approach +- In **functional** programming a program is *not* a sequence of commands +- Program = *collection of definitions* +- All values are constants +- Functions = functions in the mathematical sense + - takes inputs, returns output + - does nothing else, no side-effects + - same input, always same output + +## Functional Example + +```haskell +x = 2 +x = 4 +``` + +- Won't work in a purely functional language +- `x` can be either 2 or 4, only one definition may exist +- No concept of *current* value: `x` is `x` is `x` + +## Functional Example + +``` haskell +y = function x +z = function x +``` + +- `function(x)` will return a value that is assigned to `y`, nothing else +- `y` and `z` are equal: \ + for the same input, `function` always returns the same output + +## Advantages + +::: incremental +- No global state, no side effects +- Don't have to run debugger to find out what `x` is at some point +- Function can't sneakily change some global variable +- Easier optimization + - same input, always same output + - multiple calls of `function(x)` can be optimized into one +- Easier concurrency + - no changes to variables, no mutexes or semaphores needed + - compiler can automatically calculate concurrent paths + - `x = function(3) + function(4)` + - no reason not to run `function(3)` and `function(4)` concurrently +::: + +## Functional Patterns + +::: important +We'll take a look at some programming patterns \ +from the world of functional programming. +::: + +## Recursion + +Repetition and traversing through lists or similar is done *recursively* instead of *iteratively* + +:::::: columns +::: column +### Summing up an array in Java + +``` java +private int sum(int[] arr) { + int result = 0; + for(int i=0; i<arr.length; i++) { + result = result + arr[i]; + } + return result; +} +``` +::: +::: column +### Summing up a list in Haskell + +```haskell +sum :: [Int] -> [Int] +sum [] = 0 +sum (first:rest) = first + sum rest +``` +::: +:::::: + +## Sum Types & Pattern Matching + +- Sum Types: similar to enums in Java, but more powerful +- Means that data can be one of some alternatives +- Which alternative some instance is can then be used for decisions with pattern matching +- Similar to switch-case with Enums in Java, but more powerful +- Many modern imperative languages like Kotlin have them +- Python added them in version 3.10 + +## Sum Types & Pattern Matching Example + +``` haskell +data Color = Red | Green | Blue + +check :: Color -> String +check Red = "It's fine." +check Green = "A very ugly color!" +check Blue = "Yeah, it's okay." +``` + +## Higher-Order Functions + +- Higher-order functions are functions that take other functions as arguments +- Very powerful +- Examples: `map`, `filter` + +## Map + +:::::: columns +::: column +### Doubling all entries of an array in Java + +``` java +private void doubleAll(int[] arr) { + for(int i=0; i<arr.length; i++) { + arr[i] = arr[i] * 2; + } +} +``` +::: +::: column +### Doubling all entries of a list in Haskell + +```haskell +doubleAll :: [Int] -> [Int] +doubleAll list = map (*2) list +``` + +::: info +Usage: `doubleAll [1,2,3]` \ +yields `[2,4,6]` +::: +::: +:::::: + +## Filter + +:::::: columns +::: column +### Only keep elements bigger than 5 in Java + +``` java +List<Integer> result + = new ArrayList<>(); +for(int i=0; i<input.size(); i++) { + if(input.get(i) > 5) { + result.add(input.get(i)); + } +} +``` +::: +::: column +### Only keep elements bigger than 5 in Haskell + +```haskell +biggerThan5 :: [Int] -> [Int] +biggerThan5 list = filter (>5) list +``` +::: info +Usage: `biggerThan5 [4,5,6,7]` \ +yields `[6,7]` +::: +::: +:::::: + +## Higher-Order Functions + +::: incremental +- Many more useful higher-order functions +- Higher-order functions found their way into many imperative languages +- Together with lambda functions (also a functional concept) +- For example Python: + + ``` python + filter(lambda x: x>10, [1,4,5,65,76,87]) + ``` +- Most languages today have higher-order functions +- Kotlin, JavaScript, C# (LINQ), ... +- Even Java (Stream API)! +::: + +## Type Systems + +- Not strictly a functional pattern +- More functional languages tend to have more sophisticated type systems +- Required for things like higher-order functions +- Helps to write safe software and find bugs before even running the program + +## Brevity + +- Using these patterns, a lot of programs can be written very concisely & elegantly +- Example: QuickSort in Haskell + +``` haskell +qsort :: [Int] -> [Int] +qsort [] = [] +qsort (p:xs) = qsort (filter (< p) xs) ++ [p] ++ qsort (filter (>= p) xs) +``` + +## Reasoning + +Functional programs are easier to reason about: + +- No global state or side effects to keep track of +- Recursive algorithms lend itself to inductive proofs +- Makes proving correctness easier + +Side note: proofs can be expressed as functional programs, used in proof assistants + +## Understanding + +- Functional languages can express concepts from various fields in computer science + - Type Theory + - Category Theory + - Formal Logic + - Computability Theory + - … +- Being able to try it out theoretical abstractions in an actual programming language helps with understanding +- Understanding abstract concepts helps when using them in practice + +## Why Learn? + +Functional Programming … + +::: incremental +- … helps avoiding bugs by excluding global state and side effects +- … provides powerful features that imperative languages are copying in their full glory +- … helps understand these features, so we can better utilize them in other languages +- … can express many theoretical concepts, helping us to understand them +::: + +## Functional Languages + +There are many mainly functional programming languages: + +- Scala (Java alternative, also supports object-orientation) +- Elm (purely functional, for web frontends) +- Elixir +- PureScript (purely functional JavaScript alternative) +- Lisp / Scheme / Clojure +- OCaml +- F# (based on .NET) +- … many others in varying degrees + +## Haskell + +We use [Haskell](https://www.haskell.org/) + + + +- Since 1990 +- Purely functional +- Statically & strongly typed +- Most popular purely functional language diff --git a/src/01-introduction.md b/src/01-introduction.md new file mode 100644 index 0000000000000000000000000000000000000000..2cd925d9918745b0fb8a839a025d1023f0aed3d4 --- /dev/null +++ b/src/01-introduction.md @@ -0,0 +1,43 @@ +# Introduction + +## Me + +**Hello!** + +Nicolas Lenz + +- Master student +- Functional programming & theoretical computer science +- FOSS-AG + +## Contact & Links + +**Mail:** \ +[nicolas@eisfunke.com](mailto:nicolas@eisfunke.com) + +**Homepage** (with more contact data): \ +[eisfunke.com](https://www.eisfunke.com) + +**EisfunkeLab:** \ +[lab.eisfunke.com](https://lab.eisfunke.com) \ +You can sign up for the newsletter to get notified of future workshops and events! + +## Resources + +::: important +**These slides:** \ +[git.eisfunke.com/lab/fupro](https://git.eisfunke.com/lab/fupro) +::: + +::: important +***Learn You a Haskell for Great Good!* by Miran Lipovača:** \ +[learnyouahaskell.com](http://learnyouahaskell.com) + +Very good, fun to read, free to read online and base for these slides +::: + +## Interaction & Questions + +Interaction is important. Don't hesitate to ask questions! + +Questions not relevant to everybody will be answered in group phase. diff --git a/src/01-motivation.md b/src/01-motivation.md deleted file mode 100644 index ec2c725589e0512f5e72fb0d619c3f2d3cb550c0..0000000000000000000000000000000000000000 --- a/src/01-motivation.md +++ /dev/null @@ -1,79 +0,0 @@ -# Motivation - -## Motivation - -::: important -Why learn functional programming? -::: - -## What is Functional Programming? - -- To answer that question, we have to first find out what functional programming even is - -## Imperative - -- *Imperative* vs. *Functional* -- Programming lanuages like Java or C are **imperative** -- Program = sequence of commands to be executed one after another - -``` -x = 2 -doThing(x) -x = 4 -doThing(x) -``` - -Works without problem. - -## Functional - -- Different approach -- In **functional** programming a program is **not** a list of commands -- Program = *collection of definitions* - -```haskell -x = 2 -x = 4 -``` - -Will *this* work? - -## Advantages - -TODO side effects - -TODO global state - -TODO higher-order functions (map, filter, etc. fold) - -- https://kotlinlang.org/docs/lambdas.html#higher-order-functions -- https://linqexamples.com/filtering/where.html -- https://www.w3schools.com/python/python_lambda.asp -- https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/operators/lambda-expressions -- https://attacomsian.com/blog/java-filter-map - -TODO brevity (quicksort) - -TODO easer to reason about - -## Functional Languages - -There are many mainly functional programming languages: - -- Scala -- Lisp -- Scheme -- F# -- [Lightfold](https://git.eisfunke.com/software/lightfold/lightfold) -- Many others in varying degrees - -## Haskell - -However, we'll be using [Haskell](https://www.haskell.org/). - - - -- Since 1990 -- Purely functional -- Statically & strongly typed -- Used in academic context