Commit b92bcbb0 authored by Max Voit's avatar Max Voit
Browse files

typos / small changes

parent ad6e2f41
%% Cell type:markdown id: tags:
# introduction and motivation
## Introduction programming paradigm
## programming paradigms
Programming languages are built upon different programming paradigms.
A programming paradigm is the method of how the programmer gives the computer the instructions and necessary information to accomplish some task. They have significant influence on readability and effectiveness of code.
some paradigms:
- imperative
- objectoriented
- declarative
- functional
- logical
- symbolic
### imperative languages
- commands which are worked on sequentially and which change the *state* of a program.
* control structures: if...then, case...of,while...do, repeat...until
* procedures: small parts of a program which allow reuse of existing code (procedural languages)
(e.g.: C, Pascal, Fortran)
### objectoriented languages
- objects are a compound of procedures and associated data.
- good for mapping structure like hierarchies.
(e.g.: Java, C++)
### declarative languages
- they describe what *is* rather than what you have to *do*.
- they are more or less the opposite of imperative languages
(e.g.: SQL, regular expressions, CSS)
### funktional languages
### functional languages
- *pure* (mathematical) functions (no *side effects*)
- *referential transparency* (function is replacable with its value)
- *functions of higher order* (partial function application, currying)
- avoids mutable (changeable) data
(e.g.: Haskell, Erlang, Lisp)
%% Cell type:markdown id: tags:
## nomenclature
### side-effects
beside the normal return values of a function there are are one or more of the following effects:
- modifies a state.
- has visible interaction with the outer world.
### referential transparency
- the same input gives always the same output.
- $\Rightarrow$ an expression can be replaced through its value or an expression with the same value (*substitution principle*).
### Memoisation
Preq.: functions which are *referential transparent*
- return values can be stored in a cache.
- there is no additional evaluation needed.
### pure (mathematical) functions
- no *side-effects*.
- *referential Transparenz* and thus can be *substituted*
pure functions resemble mathematical functions.
*Remark:* This generally does not hold for procedures of imperative languages!
## View of the natural scientist
## view of the natural scientist
- mathematics as language to describe the nature.
- is used to describe things as what they are (not what they do).
- focus is not on programming itself but to find natures rules and verification (sometimes through simulation).
- $\Rightarrow$ would be best to have mathematical view and language for programming.
- mathematical proofs are (to some degree) possible in functional languages.
This makes functional thinking in programming very useful for the scientist.
%% Cell type:markdown id: tags:
# functional programming language Haskell
## Features of Haskell
- functional
- pure (mathematical) functions (no *side effects*)
- referential transparent (functions can be replaced with a value)
- functions of higher order (partial function evaluation, currying)
- avoids mutable data.
- algebraic type system.
- lazy evaluation (non-strict evaluation)
*important implied facts*:
- recursion instead of loops.
- description of what *is* and mathematical thinking possible.
- type system and pure functions avoid runtime bugs.
## algebraic type system
type
: category of a variable/data
algebraic type
: type which also can contain types
- strong types (types are known and fixed at compile-time)
- infered types (implicit determination of types)
- type classes (together with the algebraic types this enables mostly correct mathematical domain declarations)
## non-strict semantic through lazy evaluation
### non-strict semantic
reduction from outer to inner.
$\Rightarrow$ an expression can have a value, even if some of the sub-expressions does not.
*example*: in $a+(b*c))$ first $+$ is evaluated and then $(b*c)$
- the advantage is to distinguish creation and processing of data.
- allows infinite data structures.
- it does not make calculation more effective.
more information: [Non-strict semantics](http://www.haskell.org/haskellwiki/Non-strict_semantics)
## lazy evaluation
the evaluation of an expression will only happen if the result is needed.
- $\Rightarrow$ definition of infinite data structures possible.
- in best case unnecessary evaluations are avoided.
- in worst case it needs much memory.
- non-strict semantic is realised in Haskell through *lazy evaluation*.
*examples*:
%% Cell type:code id: tags:
``` haskell
x = 5
mod x 3 == 0 && mod x 5 == 0
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
the second expression only will be evaluated if the first is `True`
%% Cell type:code id: tags:
``` haskell
g = mod (1+2) 3
g + 2 -- g evaluated here (because it is shown)
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
## thunks
*thunks* is an internal data structure which holds not yet evaluated expressions.
- expressions are mapped into a graph, reduced and not neccessary *thunks* are discarded.
- expressions are mapped into a graph, reduced and unneccessary *thunks* are discarded.
it is not that useful, if long expression-chains are build up.
*example*: (`{}` entsprechen thunks):
*example*: (`{}` represent thunks):
%% Cell type:code id: tags:
``` haskell
product [1..3]
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
builds up thunks:
builds up thunks in this way:
```haskell
{1 * product [2,3]}
{1 * {2 * product [3]}}
{1 * {2 * {3 * product []}}}
{1 * {2 * {3 * 1}}}
```
## strictness analysis
the GHC compiler does a *strictness analysis*.
- it checks if a function argument is always evaluated in a given function.
- $\Rightarrow$ is this the case the evaulation will be done before calling the function (strict).
- reduces the overhead which happens sometimes with *lazy evaluation*.
further information: [Lazy_vs._non-strict](http://www.haskell.org/haskellwiki/Lazy_vs._non-strict)
## partial functions evaluation
fix on or more arguments of a function, and evaluate them.
fix one or more arguments of a function, and evaluate them.
*example*:
$f: (X \times Y \times Z) \rightarrow N$
transforms to
$\text{partial}(f): (Y \times Z) \rightarrow N$
with fixed $X$
## Currying
## currying
transform a function of several variables in a chain of functions with one argument.
*example*:
$f: (X \times Y \times Z) \rightarrow N$
transforms to
$\text{curry}(f): X \rightarrow (Y \rightarrow (Z \rightarrow N))$
%% Cell type:code id: tags:
``` haskell
```
......
......@@ -1862,6 +1862,12 @@
"display_name": "Haskell",
"language": "haskell",
"name": "haskell"
},
"language_info": {
"codemirror_mode": "ihaskell",
"file_extension": ".hs",
"name": "haskell",
"version": "7.10.2"
}
},
"nbformat": 4,
......
%% Cell type:markdown id: tags:
# lists and tuples
## fun with lists
a list is a finite, ordered set
- [] brackets define lists
[1,2,3,4,5]
- list-elements must be of the same type (or typeclass)
- list-elements must be of the same type
%% Cell type:code id: tags:
``` haskell
:option no-lint
[1,2,3.2]
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
['a','b']
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
[1,2,'a']
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
## Ranges
- from...to (ranges) (be careful with floating points because of rounding errors)
%% Cell type:code id: tags:
``` haskell
[1,3..8]
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
['a'..'z']
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
[0.1,0.3..0.9]
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
- infinite lists possible (but don't do it like this! this will take a while... )
%% Cell type:code id: tags:
``` haskell
[1..]
```
%% Cell type:markdown id: tags:
- should normally be named and then used
%% Cell type:code id: tags:
``` haskell
a = [1,3..]
take 5 a
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
## Indexing and manipulating of lists
get first list element
%% Cell type:code id: tags:
``` haskell
head a
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
get last list element
%% Cell type:code id: tags:
``` haskell
b = [1,3,6,8]
last b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
get list without first element (head)
%% Cell type:code id: tags:
``` haskell
tail b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
get list without last element
%% Cell type:code id: tags:
``` haskell
init b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
first `x` elements
%% Cell type:code id: tags:
``` haskell
take 2 a
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
drop first `x` elements
%% Cell type:code id: tags:
``` haskell
drop 2 b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
element at position a (note: haskell starts index at 0)
%% Cell type:code id: tags:
``` haskell
b
b !! 2
```
%%%% Output: display_data
%%%% Output: display_data
%% Cell type:markdown id: tags:
length of a list
%% Cell type:code id: tags:
``` haskell
length b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
Check if list is empty
%% Cell type:code id: tags:
``` haskell
null b
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
null []
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
reverse a list
%% Cell type:code id: tags:
``` haskell
reverse b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
minimal element of a list (req.: elements are sortable) ($\min (a_1,...,a_n)$)
%% Cell type:code id: tags:
``` haskell
minimum b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
maximal element of a list (req.: elements are sortable) ($\max (a_1,...,a_n)$)
%% Cell type:code id: tags:
``` haskell
maximum b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
sum of all elements in a list ($\sum_i a_i$)
%% Cell type:code id: tags:
``` haskell
sum b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
product of all elements in a list ($\prod_i a_i$)
%% Cell type:code id: tags:
``` haskell
product b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
check if given element is in list ($x \in a$)
%% Cell type:code id: tags:
``` haskell
elem 3 b
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
## concat lists
- adding lists
%% Cell type:code id: tags:
``` haskell
[1,2,3] ++ [4,5,6]
```
%%%% Output: display_data
%% Cell type:code id: tags:
``` haskell
"hello" ++ " " ++ "world"
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
- insert elements at the beginning (operator ``:``)
%% Cell type:code id: tags:
``` haskell
2:[3,4]
[2] ++ [3,4]
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
these two are identical
%% Cell type:code id: tags:
``` haskell
[1,2,3] == 1:2:3:[]
```
%%%% Output: display_data
%% Cell type:markdown id: tags:
## create lists (list comprehensions)
inspired by mathematical notation we can generate lists
[expr | condition, ,...]
while *condition* is one of
1. generator `var <- list`
2. boolean expression
3. binding
%% Cell type:code id: tags: