Commit abe31bb1 authored by Jochen Schulz's avatar Jochen Schulz
Browse files

actual state

parent c5df575c
......@@ -32,6 +32,7 @@ A lecture to teach Haskell in a 2 weeks block lab course.
* [26 continous optimization](http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/26 continous optimization.ipynb)
* [27 inverse](http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/27 inverse.ipynb)
* [28 minsurf](http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/28 minsurf.ipynb)
* [Untitled](http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/Untitled.ipynb)
* [shortest path](http://nbviewer.jupyter.org/urls/gitlab.gwdg.de/jschulz1/haskell_labcourse/raw/master/lecture/shortest path.ipynb)
## viewing of the notebooks
......
# vim:ft=todo
TODO 2016-03-17 notebook-convertierungen ! lhs <-> ipynb
- motivation fuer haskell: funktionale/mathematische Sichtweise
LECTURE:
......
......@@ -21,8 +21,6 @@ facRf x = fac' x 1 where
fac' 1 y = y
fac' f y = fac' (f-1) $! (f*y)
Fractional
main :: IO ()
main = do
let g = facNE 10000
......
%% Cell type:markdown id: tags:
# Excercises: types
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 1
Use [Hoogle](https://www.haskell.org/hoogle/) or [Hayoo!](http://hayoo.fh-wedel.de/):
- What is the difference between `div` and `quot`?
- Find the definition of the type `Bool` in `GHC.Types`.
- Find `(<=)`. Why is there no implementation?
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 2
Use the function `factorial` from the lecture,
```haskell
factorial :: Integer -> Integer
factorial n = product [1.. n]
```
and change the type to `Int -> Int`. Which consequences does this have, and how can you show it?
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 3
- Write a function taking two numbers as arguments, the edge lengths of a rectangle, and returns the area.
- the function should be able to work with arbitrary type of numbers (besides complex)
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 4
Write a function accepting three strings and returning the lexicographically largest one.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 5
Write a function accepting a list of arbitrary numbers and returning the mean value.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 6
Write a function that accepts a number and a tuple (e.g. `("left", "right")`) and returns the first element of the tuple if the digit `0` is in the number, and the second element otherwise.
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Exercises: higher order functions
## problem 1
- write a function `applyThrice` analogous to `applyTwice` from the lecture.
- write a function `applyThrice'` without using an argument, thus
```haskell
applyThrice' f = ...
```
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 2
write in pointfree-style:
- `f x = (+) 5 $ (/) 8 x`
- `g x = mod 4 $ div 16 $ 2 * $ x + 2`
- `g x = mod 4 $ div 16 $ (2 *) $ x + 2`
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 3
write the following functions as lambda functions
- $h(x) = x^2 + x \% 5$.
- the `hasVowal` function.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 4
Write a function `gt100`, which checks if a parameter is greater than 100
- normal
- pointfree
- as anonymous
- as pointfree anonymous function
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 5
Let
```haskell
multThree :: (Num a) => a -> a -> a -> a
multThree x y z = x * y * z
```
be a function for multiplying three numbers with each other. Use partial function evaluation to get a function which has one parameter and multiplies with 49.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 6
- write a function `flipFunct`, which gets a function of two arguments and returns a function which has the arguments flipped.
- check the function with the `-`
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 7
rewrite the following function such that it only contains function composition and application operators. Avoid as usual brackets.
```haskell
oddity = (odd(negate(sum (take 3 [1..]))))
```
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Excercises: types
%% Cell type:markdown id: tags:
## problem 1
Use [Hoogle](https://www.haskell.org/hoogle/) or [Hayoo!](http://hayoo.fh-wedel.de/):
- What is the difference between `div` and `quot`?
- Find the definition of the type `Bool` in `GHC.Types`.
- Find `(<=)`. Why is there no implementation?
%% Cell type:markdown id: tags:
## problem 2
Use the function `factorial` from the lecture,
```haskell
factorial :: Integer -> Integer
factorial n = product [1.. n]
```
and change the type to `Int -> Int`. Which consequences does this have, and how can you show it?
%% Cell type:code id: tags:
``` haskell
factorial :: Int -> Int
factorial n = product [1..n]
factorial 50
```
%% Cell type:markdown id: tags:
## problem 3
- Write a function taking two numbers as arguments, the edge lengths of a rectangle, and returns the area.
- the function should be able to work with arbitrary type of numbers (besides complex)
%% Cell type:code id: tags:
``` haskell
area :: Num a => a -> a -> a
area x y = x*y
```
%% Cell type:markdown id: tags:
## problem 4
Write a function accepting three strings and returning the lexicographically largest one.
%% Cell type:code id: tags:
``` haskell
lexLargest :: String -> String -> String -> String
lexLargest a b c = if a > b && a > c then a
else if b > a && b > c then b
else c
lexLargest2 a b c = maximum [a, b, c]
lexLargest3 a = maximum a
```
%% Cell type:markdown id: tags:
## problem 5
Write a function accepting a list of arbitrary numbers and returning the mean value.
%% Cell type:code id: tags:
``` haskell
:t length
```
%% Cell type:code id: tags:
``` haskell
mean :: Fractional a => [a] -> a
mean l = sum l / genericLength l
where genericLength :: (Num i) => [a] -> i
genericLength [] = 0
genericLength (_:l) = 1 + genericLength l
```
%% Cell type:code id: tags:
``` haskell
mean2 :: (Real a,Fractional b) => [a] -> b
mean2 l = realToFrac (sum l) / realToFrac (length l)
```
%% Cell type:markdown id: tags:
## problem 6
Write a function that accepts a number and a tuple (e.g. `("left", "right")`) and returns the first element of the tuple if the digit `0` is in the number, and the second element otherwise.
%% Cell type:code id: tags:
``` haskell
containsZero :: (Num a, Show a) => a -> (b,b) -> b
containsZero x y = if '0' `elem` show x
then fst y
else snd y
```
%% Cell type:code id: tags:
``` haskell
containsZero 5 ("left","right")
containsZero 50 ("left","right")
```
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Exercises: higher order functions
## problem 1
- write a function `applyThrice` analogous to `applyTwice` from the lecture.
- write a function `applyThrice'` without using an argument, thus
```haskell
applyThrice' f = ...
```
%% Cell type:code id: tags:
``` haskell
applyThrice f x = f(f(f x))
applyThrice' f = f . f . f
```
%% Cell type:code id: tags:
``` haskell
applyThrice (+2) 5
applyThrice' (+2) 5
```
%% Cell type:markdown id: tags:
## problem 2
write in pointfree-style:
- `f x = (+) 5 $ (/) 8 x`
- `g x = mod 4 $ div 16 $ 2 * $ x + 2`
- `g x = mod 4 $ div 16 $ (2 *) $ x + 2`
%% Cell type:code id: tags:
``` haskell
--f x = (+) 5 ( (/) 8 x)
--f x = ((+) 5) ( ((/) 8) x)
--f x = ((+) 5) . ( ((/) 8) ) x
--f = ((+) 5) . ( (/) 8 )
--f = (5+) . (8/)
```
%% Cell type:code id: tags:
``` haskell
f = (5+) . (8/)
```
%% Cell type:code id: tags:
``` haskell
--g x = mod 4 $ div 16 $ 2 * $ x / 2
--g = mod 4 . div 16 . (2*) . (+2)
```
%% Cell type:code id: tags:
``` haskell
g = mod 4 . div 16 . (2*) . (+2)
```
%% Cell type:code id: tags:
``` haskell
f 2
g 2
```
%% Cell type:markdown id: tags:
## problem 3
write the following functions as lambda functions
- $h(x) = x^2 + x \% 5$.
- the `hasVowal` function.
%% Cell type:code id: tags:
``` haskell
(\x -> x^2 + mod x 5) 10
(\x -> elem 'a' x || elem 'e' x || elem 'i' x || elem 'o' x || elem 'u' x || elem 'A' x || elem 'E' x || elem 'I' x || elem 'O' x || elem 'U' x) "bar"
```
%% Cell type:markdown id: tags:
## problem 4
Write a function `gt100`, which checks if a parameter is greater than 100
- normal
- pointfree
- as anonymous
- as pointfree anonymous function
%% Cell type:code id: tags:
``` haskell
gt100 x = x > 100
gt100' = (>100)
\x -> x > 100
(>100)
```
%% Cell type:markdown id: tags:
## problem 5
Let
```haskell
multThree :: (Num a) => a -> a -> a -> a
multThree x y z = x * y * z
```
be a function for multiplying three numbers with each other. Use partial function evaluation to get a function which has one parameter and multiplies with 49.
%% Cell type:code id: tags:
``` haskell
multThree :: (Num a) => a -> a -> a -> a
multThree x y z = x * y * z
```
%% Cell type:code id: tags:
``` haskell
mult49 = multThree 7 7
```
%% Cell type:code id: tags:
``` haskell
mult49 2
```
%% Cell type:markdown id: tags:
## problem 6
- write a function `flipFunct`, which gets a function of two arguments and returns a function which has the arguments flipped.
- check the function with the `-`
%% Cell type:code id: tags:
``` haskell
flipFunct :: (a -> b -> c) -> b -> a -> c
flipFunct f y x = f x y
```
%% Cell type:code id: tags:
``` haskell
flipFunct (-) 4 7
(-) 4 7
```
%% Cell type:markdown id: tags:
## problem 7
rewrite the following function such that it only contains function composition and application operators. Avoid as usual brackets.
```haskell
oddity = (odd(negate(sum (take 3 [1..]))))
```
%% Cell type:code id: tags:
``` haskell
oddity = odd . negate . sum . take 3 $ [1..]
```
......
%% Cell type:markdown id: tags:
# introduction and motivation
## 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)
### 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
- 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