Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
NicolasSeinTemplateTest
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Iterations
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
timucinboldt
NicolasSeinTemplateTest
Commits
9ffaf067
Commit
9ffaf067
authored
Jul 14, 2022
by
Nicolas Lenz
Browse files
Options
Downloads
Patches
Plain Diff
Updates
parent
48fcbd90
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
src/00-motivation.md
+301
-0
301 additions, 0 deletions
src/00-motivation.md
src/01-introduction.md
+43
-0
43 additions, 0 deletions
src/01-introduction.md
src/01-motivation.md
+0
-79
0 additions, 79 deletions
src/01-motivation.md
with
344 additions
and
79 deletions
src/00-motivation.md
0 → 100644
+
301
−
0
View file @
9ffaf067
# 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
This diff is collapsed.
Click to expand it.
src/0
0
-introduction.md
→
src/0
1
-introduction.md
+
43
−
0
View file @
9ffaf067
...
...
@@ -6,30 +6,38 @@
Nicolas Lenz
-
Master student
, first semester
-
F
achschaftsrat
-
Master student
-
F
unctional programming & theoretical computer science
-
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
):
\
**Homepage**
(with more contact data):
\
[
eisfunke.com
](
https://www.eisfunke.com
)
**My GitLab**
(with my projects, including Haskell stuff):
\
[
git.eisfunke.com
](
https://git.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!
##
Interaction & Question
s
##
Resource
s
Interaction is important. Don't hesitate to ask questions!
::: important
**These slides:**
\
[
git.eisfunke.com/lab/fupro
](
https://git.eisfunke.com/lab/fupro
)
:::
Questions not relevant to everybody will be answered in group phase.
::: important
***Learn You a Haskell for Great Good!*
by Miran Lipovača:
**
\
[
learnyouahaskell.com
](
http://learnyouahaskell.com
)
## Material
Very good, fun to read, free to read online and base for these slides
:::
Very good, fun to read, free to read online and base for this workshop:
## Interaction & Questions
*Learn You a Haskell for Great Good!*
by Miran Lipovača (
<http://learnyouahaskell.com/>
)
Interaction is important. Don't hesitate to ask questions!
Questions not relevant to everybody will be answered in group phase.
This diff is collapsed.
Click to expand it.
src/01-motivation.md
deleted
100644 → 0
+
0
−
79
View file @
48fcbd90
# 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
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment