Skip to content
Snippets Groups Projects
Commit f3a4224a authored by Nicolas Lenz's avatar Nicolas Lenz :snowflake:
Browse files

Init repo

parent f498143e
No related branches found
No related tags found
No related merge requests found
makefile 0 → 100644
inputs = $(shell ls src/*.md) # $(wildcard src/*.md) outputs in the wrong order
pandoc = pandoc $(inputs) -f markdown -t beamer \
--slide-level 2 -V theme:metropolis -H theme.tex
slides.pdf: $(inputs) res/* theme.tex
$(pandoc) --pdf-engine=xelatex -o $@
slides.tex: $(inputs) theme.tex
$(pandoc) -o $@
clean:
rm -f slides.pdf slides.tex
.PHONY: clean
res/certificate.png

129 KiB

res/logo-haskell.png

7 KiB

File added
---
title: Functional Programming
subtitle: An introductory Workshop
author: Nicolas Lenz
institute: "[eisfunke.com](https://www.eisfunke.com)"
titlegraphic: res/logo-haskell.png
---
# Introduction
## Me
**Hello!**
Nicolas Lenz
- Master student, first semester
- Fachschaftsrat
- FOSS-AG
- Functional programming/theoretical computer science fan
## Contact & Links
**Mail:** \
[nicolas.lenz@udo.edu](mailto:nicolas.lenz@udo.edu)
**Homepage** (with more contact data and links to projects): \
[eisfunke.com](https://www.eisfunke.com)
**My GitLab instance** (with some Haskell stuff): \
[git.eisfunke.com](https://git.eisfunke.com)
You can find the source code of these slides at <https://git.eisfunke.com/education/fupro>.
## 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/>)
## Certificate
\centering
![](res/certificate.png)
## Haskell
We'll be using [Haskell](https://www.haskell.org/).
![](res/logo-haskell.png)
- Since 1990
- Purely functional
- Statically & strongly typed
- Used in academic context
## Other Functional Languages
- Scala
- Lisp
- Scheme
- F#
- [Lightfold](https://git.eisfunke.com/software/lightfold/lightfold)
- Many others in varying degrees
# Basic Usage
## Installation
- Recommended: Install the [Haskell Platform](https://www.haskell.org/platform/)
- Includes everything you need to start with Haskell
- Just installing the GHC alone is enough for this workshop
## GHCI
*"Glasgow Haskell Compiler"* is the Haskell compiler everyone uses.
*GHCi* (i = interactive) is an interactive "language shell" for Haskell (command: `ghci`).
- Enter a Haskell expression to see the result
- Can use definitions from a Haskell program in a file
- Various commands beginning with `:`:
- `:load` / `:l file.hs`: Load the content of a file.
- `:reload` / `:r`: Reload loaded files (use when files changed)
- `:info` / `:i`: Get information on a function, type, …
- `:type` / `:t`: Get the type of an expression
- `:quit` / `:q`: Close GHCi
- `:help` / `:h`/`:?`: Get a list of available commands
**Make sure you can open a file in GHCI!**
## Compiling
Haskell can of course also be compiled into executable programs with `ghc`, but that comes later.
For now we'll use only the GHCi. It's great for trying out stuff.
## Stack
Just so you know: for projects, you'd want to use Stack.
https://docs.haskellstack.org/en/stable/README/
All-in-one build tool & "package manager"
# What is Functional Programming?
## The Difference
- *Imperative* vs. *Functional*
- Programming lanuages like Java or C are imperative
- Imperative: program is list of commands to be executed one after another
```
x = 3
do_thing(x)
x = x + 4
y = 2
do_another_thing(x, y)
```
## Functional
- Different approach
- In functional programming a program is no list of commands
- More a *collection of definitions*
```
x = 4
x = 2
```
What will happen?
# First Functions
## Basic Calculation
`x = 1` defines `x` to hold the value `1` as already seen.
Basic calculations work as expected
- `1 * 2`
- `2 / 2`
- `3 + 1`
- `1 - 3`
`3 + 1 * 20` vs. `(3 + 1) * 20`
## Truth values
Truth values: `True` and `False` (*capitalized!*)
- `True && False`
- `True || False`
- `not False`
## Comparisons
Comparisons also work as expected.
- `5 == 5`
- `3 > 2`
- `1 >= 10`
- `2 /= 2` (note that not `!=`)
## First Functions
```haskell
double x = x * 2
six = double 3
isThree x = x == 3
isOneMore x y = x - 1 == y
result = isOneMore 3 6
```
# First Types
## What is a Type?
- "Classifies" values
- Constrains what a value can be
## Basic Types in Haskell
- `Int`: Integer numbers (`-1`, `0`, `30`)
- `Boolean`: Truth values (`True`, `False`) (*capitalized!*)
- `Char`: Single characters (`'a'`, `'c'`)
- `String`: Text (`"Hello"`, `"Cheese sandwich"`)
- `a -> b`: Function with the
## Specifying a Type
```haskell
x :: Int
x = 5
favoriteAnimal :: String
favoriteAnimal = "Cat, obviously"
firstLetter :: Char
firstLetter = 'A'
double :: Int -> Int
double x = x * 2
```
Recommendation: Always specify the type.
## Lists
Ordered list with elements of type `a`: `[a]`
```haskell
fruits :: [String]
fruits = ["Banana", "Apple", "Raspberry"]
numbers :: [Int]
numbers = [1,5,2]
empty :: [Int]
empty = []
```
## Building Lists
Lists are a *recursive data structure*
A list is always either
- *an empty list* or
- *an element added in front of another list*.
Adding an element is done with `:`
`[1,2,3]` is the same as `1 : (2 : (3 : []))`
```haskell
moreFruits :: [String]
moreFruits = "Orange" : fruits
```
# First Logic
## Decisions
`if .. then .. else ..` is possible
Must always have `else` (otherwise result could be undefined, not functional)!
```haskell
rate :: Int -> String
rate x = if x > 42 then "big" else "small"
```
## Pattern Matching
You can use multiple definitions for different patterns of the input
```haskell
double :: Int -> String
double 1 = "zwei"
double 2 = "vier"
double x = "calculate it yourself"
```
Pattern matching can "disassemble" values
```haskell
tail :: [Int] -> [Int]
tail [] = []
tail (x:xs) = xs
```
## Recursion
Recursion is main method of repetition (loops are not very functional)
```haskell
doubleAll :: [Int] -> [Int]
doubleAll [] = []
doubleAll (x:xs) = (x * 2) : xs
```
# Finding Documentation
## Hoogle
https://hoogle.haskell.org/
\definecolor{primary-medium}{HTML}{F19204}
\definecolor{accent-bright}{HTML}{F07D8F}
\definecolor{accent-light}{HTML}{EB4F67}
\definecolor{accent-medium}{HTML}{E41131}
\definecolor{accent-dim}{HTML}{A90D24}
\definecolor{background-very-dark}{HTML}{232124}
\definecolor{background-dark}{HTML}{3F3C41}
\definecolor{background-super-bright}{HTML}{F8F7F8}
\metroset{sectionpage=progressbar, progressbar=frametitle, titleformat=smallcaps, background=light}
\setbeamercolor{normal text}{fg=black}
\setbeamercolor{background canvas}{fg=background-dark, bg=background-super-bright}
\setbeamercolor*{palette primary}{fg=white,bg=primary-medium}
\setbeamercolor*{progress bar}{fg=accent-dim, bg=accent-bright}
\setlength{\leftmargini}{1em}
\usepackage{sourcesanspro}
\usepackage{sourcecodepro}
\usepackage{polyglossia}
\setdefaultlanguage[variant=american]{english}
\usepackage{listings}
\usepackage{mathtools, amssymb, stmaryrd, unicode-math}
\usepackage{xspace}
\usepackage{fvextra}
\usepackage{color}
\usepackage{fancyvrb}
% \renewcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\},breaklines,fontsize=\small}
\DefineVerbatimEnvironment{verbatim}{Verbatim}{commandchars=\\\{\},breaklines,fontsize=\small}
% \input{macros.tex}
\lstnewenvironment{algorithm}{
\lstset{
mathescape=true,
escapeinside={(*}{*)},
keepspaces=true,
numbers=left,
numberstyle=\tiny,
columns=fullflexible,
keywordstyle=\bfseries,
keywords={repeat, until, for, all, each, in, return, function, if, else, or, and, empty},
numbers=left
}
}{}
kaese :: Int
kaese = 1
brot :: Int
brot = 3
double :: Int -> Int
double x = x * 2
addKaese :: Int -> Int
addKaese x = x + kaese
isEqual :: Int -> Int -> Bool
isEqual x y = x == y
fruits :: [String]
fruits = ["Apple", "Banana"]
doubleName :: Int -> String
doubleName 1 = "Zwei"
doubleName 2 = "Vier"
doubleName x = "Mir egal"
withoutFirst :: [Int] -> [Int]
withoutFirst (x : xs) = xs
withoutFirst [] = []
doubleAll :: [Int] -> [Int]
doubleAll [] = []
doubleAll (x : xs) = x * 2 : doubleAll xs
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment