Commit b84d3c05 authored by Christoph Ruegge's avatar Christoph Ruegge
Browse files

Wording

parent 2d8cfe49
%% Cell type:markdown id: tags:
# Exercises 02
## problem 1
- start the notebook
- evaluate $2^{16}$
- write a statement which checks if $2^{23} - 1000$ is greater than $2^{22.9999}$ (what is the difference between `**` and `^`?)
- is $23 \cdot 5 > 45 \wedge ( 169/3 = 56 \vee 4 = (53\cdot3) \% 5 )$ true or false?
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 2
write an own function `tenfoldIfLess30`, which multiplies numbers under 30 with 10.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 3
Write a function `divbl2or3` which returns `True` for numbers which are dividable by 2 or 3 and `False` otherwise.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 4
implement the following functions:
$f : \mathbb{N}^3 \to \mathbb{N}$ with $f(x,y,z)= (x+y+z)/3$
$g : \mathbb{N}^3 \to \mathbb{N}$ with $g(a,b,c)=\begin{cases}a :\quad a > b,c\\b :\quad b > a,c\\c : \quad c \geq a,b\end{cases}$
$h : \mathbb{N}^2 \to \mathbb{N}$ with $h(i,j)=\begin{cases}i :\quad j=0\\ succ(h(i,j-1)) : \quad otherwise\end{cases}$
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 5
- write your own implementation of `even'.
- Look up `even` in _Hoogle_ (<http://www.haskell.org/hoogle/>) and/or
_Hayoo!_ (<http://hayoo.fh-wedel.de/>) (`rem` is aquivalent to `mod` for positive numbers).
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 6
the imaginary weather prediction module `Magic.Weather.Prediction` creates the variables
regen :: Bool
kalt :: Bool
wind :: Bool
rainy :: Bool
cold :: Bool
windy :: Bool
(assume that they are filled)
Use `if ... then ... else` (and boolean operators) to return the following strings:
- "drachenSteigenLassen" _(bei Wind ohne Regen)_
- "regenschirmMitnehmen" _(bei Regen ohne Kälte)_
- "amKaminSitzen" _(bei Regen und Kälte)_
- "sonnenBaden" _(ohne Regen und Wind)_
- "fly kites" _(when windy but not rainy)_
- "take your umbrella" _(when rainy but not cold)_
- "sit at the fireplace" _(when rainy and cold)_
- "enjoy the sun" _(when not rainy or windy)_
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 7
Brackets get very expensive. Try to reduce the number of brackets via using of the application operator `$` from the following expressions:
- `max 23 (max 5 39)`
- `max 23 (5 * (succ 4))`
- `mod 23 (min 7 ((*) 2 3))`
- `min (max 23 44) (max 5 (negate (-39)))`
Which brackets are redundant anyway?
Which expression is an identity?
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Exercises 03
## problem 1
```haskell
alphabet = ['a'..'z']
```
get the following information from `alphabet`:
- length
- first 3 characters
- the 12th character
- the 7-10th character
- the last 5 characters
and then create an `aLPHABET` with upper case characters and add it to the existing one
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 2
implement a function `hasVocal` which checks if a string contains a vocal character. Test the function with "foo", "bar" and "bz".
implement a function `hasVowel` which checks if a string contains a vowel character. Test the function with "foo", "bar" and "bz".
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 3
use list comprehensions for creating the following sets in haskell
- $A = \{ x \,:\, x \in \mathbb{N},\, x \text{ odd}\}$ (only showing the first 5 elements)
- $B = \{ -x \,:\, x \in \mathbb{N},\, 3|x ,\, x < 10\}$ ($3|x$ : 3 is a divisor of x)
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 4
Create the set of all odd numbers smaller than 500, which are dividable through 5 and 7.
$S=\{x : x \neq 0 \land 5|x \land 7|x\}$
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
#problem 5
# problem 5
Let $A=\{1,2,3\}$, $B=\{3,4,5\}$. Calculate the intersection and the union of $A$ and $B$.
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 6
let `lesser` and `greater` be functions which have a value `v` and a list of values `l` of the same type as `v` . return a list of all values which are lesser or greater as `v`.
Let `less` and `greater` be functions which have a value `v` and a list of values `l` of the same type as `v`. Return a list of all values which are less or greater as `v`.
$\text{lesser}(v,l) = \{x : x \in l, x < v\}$
$\text{less}(v,l) = \{x : x \in l, x < v\}$
do the implementation with list comprehensions.
Implement the functions using list comprehensions.
Test: `5 [1..10]`
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:markdown id: tags:
## problem 7
the list generated with `[0.1,0.3..0.9]` has problems with precision (has roundingerror). Do it better via using list comprehensions. (Tip: ranges of integral types do not have rounding errors).
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Exercises 02
## problem 1
- start the notebook
- evaluate $2^{16}$
- write a statement which checks if $2^{23} - 1000$ is greater than $2^{22.9999}$ (what is the difference between `**` and `^`?)
- is $23 \cdot 5 > 45 \wedge ( 169/3 = 56 \vee 4 = (53\cdot3) \% 5 )$ true or false?
%% Cell type:code id: tags:
``` haskell
2^16
2^23-1000 > 2**22.9999
23*5 > 45 && (169/3 == 56 || (53*3) `mod` 5 == 4)
```
%% Cell type:markdown id: tags:
## problem 2
write an own function `tenfoldIfLess30`, which multiplies numbers under 30 with 10.
%% Cell type:code id: tags:
``` haskell
tenfoldIfLess30 x = if x < 30 then x*10 else x
```
%% Cell type:code id: tags:
``` haskell
tenfoldIfLess30 23
```
%% Cell type:code id: tags:
``` haskell
tenfoldIfLess30 42
```
%% Cell type:markdown id: tags:
## problem 3
Write a function `divbl2or3` which returns `True` for numbers which are dividable by 2 or 3 and `False` otherwise.
%% Cell type:code id: tags:
``` haskell
divbl2or3 x = x `mod` 2 == 0 || x `mod` 3 == 0
```
%% Cell type:code id: tags:
``` haskell
divbl2or3 23
```
%% Cell type:code id: tags:
``` haskell
divbl2or3 42
```
%% Cell type:markdown id: tags:
## problem 4
implement the following functions:
$f : \mathbb{N}^3 \to \mathbb{N}$ with $f(x,y,z)= (x+y+z)/3$
$g : \mathbb{N}^3 \to \mathbb{N}$ with $g(a,b,c)=\begin{cases}a :\quad a > b,c\\b :\quad b > a,c\\c : \quad c \geq a,b\end{cases}$
$h : \mathbb{N}^2 \to \mathbb{N}$ with $h(i,j)=\begin{cases}i :\quad j=0\\ succ(h(i,j-1)) : \quad otherwise\end{cases}$
%% Cell type:code id: tags:
``` haskell
f x y z = (x+y+z)/3
```
%% Cell type:code id: tags:
``` haskell
g a b c = if max a b == a && max a c == a
then a
else if max a b == b && max b c == b
then b
else c
```
%% Cell type:code id: tags:
``` haskell
h i j = if j==0
then i
else succ $ h i (j-1)
```
%% Cell type:code id: tags:
``` haskell
f 1 2 3
```
%% Cell type:code id: tags:
``` haskell
g 3 6 8
```
%% Cell type:code id: tags:
``` haskell
h 4 5
```
%% Cell type:markdown id: tags:
## problem 5
- write your own implementation of `even'.
- Look up `even` in _Hoogle_ (<http://www.haskell.org/hoogle/>) and/or
_Hayoo!_ (<http://hayoo.fh-wedel.de/>) (`rem` is aquivalent to `mod` for positive numbers).
%% Cell type:code id: tags:
``` haskell
even' a = mod a 2 == 0
```
%% Cell type:code id: tags:
``` haskell
even' 3
```
%% Cell type:code id: tags:
``` haskell
even' 4
```
%% Cell type:markdown id: tags:
## problem 6
the imaginary weather prediction module `Magic.Weather.Prediction` creates the variables
regen :: Bool
kalt :: Bool
wind :: Bool
rainy :: Bool
cold :: Bool
windy :: Bool
(assume that they are filled)
Use `if ... then ... else` (and boolean operators) to return the following strings:
- "drachenSteigenLassen" _(bei Wind ohne Regen)_
- "regenschirmMitnehmen" _(bei Regen ohne Kälte)_
- "amKaminSitzen" _(bei Regen und Kälte)_
- "sonnenBaden" _(ohne Regen und Wind)_
- "fly kites" _(when windy but not rainy)_
- "take your umbrella" _(when rainy but not cold)_
- "sit at the fireplace" _(when rainy and cold)_
- "enjoy the sun" _(when not rainy or windy)_
%% Cell type:code id: tags:
``` haskell
magicwtodo regen kalt wind =
if regen
then if kalt
then "amKaminSitzen"
else "regenschirmMitnehmen"
else if wind
then "drachenSteigenLassen"
else "sonnenBaden"
magicwtodo rainy cold windy =
if rainy
then if cold
then "sit at the fireplace"
else "take your umbrella"
else if windy
then "fly kites"
else "enjoy the sun"
```
%% Cell type:code id: tags:
``` haskell
magicwtodo True False True
```
%% Cell type:markdown id: tags:
## problem 7
Brackets get very expensive. Try to reduce the number of brackets via using of the application operator `$` from the following expressions:
- `max 23 (max 5 39)`
- `max 23 (5 * (succ 4))`
- `mod 23 (min 7 ((*) 2 3))`
- `min (max 23 44) (max 5 (negate (-39)))`
Which brackets are redundant anyway?
Which expression is an identity?
%% Cell type:code id: tags:
``` haskell
-- die Klammern um succ sind sinnlos.
-- das negate mit der negativen Zahl lässt sich zu id reduzieren, dann das unary-Minus ist eigentlich ein negate.
max 23 (max 5 39)
max 23 $ max 5 39
max 23 (5 * (succ 4))
max 23 $ 5 * (succ 4)
max 23 $ 5 * succ 4
mod 23 (min 7 ((*) 2 3))
mod 23 $ min 7 ((*) 2 3)
mod 23 $ min 7 $ (*) 2 3
min (max 23 44) (max 5 (negate (-39)))
min (max 23 44) $ max 5 (negate (-39))
min (max 23 44) $ max 5 $ negate (-39)
min (max 23 44) $ max 5 $ negate $ -39
```
%% Cell type:code id: tags:
``` haskell
```
%% Cell type:code id: tags:
``` haskell
```
......
%% Cell type:markdown id: tags:
# Exercises 03
## problem 1
```haskell
alphabet = ['a'..'z']
```
get the following information from `alphabet`:
- length
- first 3 characters
- the 12th character
- the 7-10th character
- the last 5 characters
and then create an `aLPHABET` with upper case characters and add it to the existing one
%% Cell type:code id: tags:
``` haskell
alphabet = ['a'..'z']
aLPHABET = ['A'..'Z']
length alphabet
take 3 alphabet
alphabet !! 12
take (10-7) $ drop 6 alphabet
reverse $ take 5 $ reverse alphabet
alphabet ++ aLPHABET
```
%% Cell type:markdown id: tags:
## problem 2
implement a function `hasVocal` which checks if a string contains a vocal character. Test the function with "foo", "bar" and "bz".
implement a function `hasVowel` which checks if a string contains a vowel character. Test the function with "foo", "bar" and "bz".
%% Cell type:code id: tags:
``` haskell
-- simple
hatVokal 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 --nicht so gut
hasVowel 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 --nicht so gut
-- better :
hatVokal x = maximum [elem vokal x | vokal <-"aeiouAEIOU" ]
hasVowel x = maximum [elem vokal x | vokal <-"aeiouAEIOU" ]
hatVokal "foo"
hatVokal "bar"
hatVokal "bz"
hasVowel "foo"
hasVowel "bar"
hasVowel "bz"
```
%%%% Output: display_data
%%%% Output: display_data
%%%% Output: display_data
%% Cell type:markdown id: tags:
## problem 3
use list comprehensions for creating the following sets in haskell
- $A = \{ x \,:\, x \in \mathbb{N},\, x \text{ odd}\}$ (only showing the first 5 elements)
- $B = \{ -x \,:\, x \in \mathbb{N},\, 3|x ,\, x < 10\}$ ($3|x$ : 3 is a divisor of x)
%% Cell type:code id: tags:
``` haskell
take 5 [x | x <- [1..], odd x]
take 5 [1,3..]
[ negate x | x <- [1..10], x `mod` 3 == 0 ]
```
%% Cell type:markdown id: tags:
## problem 4
Create the set of all odd numbers smaller than 500, which are dividable through 5 and 7.
$S=\{x : x \neq 0 \land 5|x \land 7|x\}$
%% Cell type:code id: tags:
``` haskell
[a | a <-[1,3..500], mod a 5==0, mod a 7==0]
```
%% Cell type:markdown id: tags:
#problem 5
# problem 5
Let $A=\{1,2,3\}$, $B=\{3,4,5\}$. Calculate the intersection and the union of $A$ and $B$.
%% Cell type:code id: tags:
``` haskell
a = [1,2,3]
b = [3,4,5]
[x | x<-a, x `elem` b]
a ++ [x | x <- b , x `notElem` a]
```
%% Cell type:markdown id: tags:
## problem 6
let `lesser` and `greater` be functions which have a value `v` and a list of values `l` of the same type as `v` . return a list of all values which are lesser or greater as `v`.
Let `less` and `greater` be functions which have a value `v` and a list of values `l` of the same type as `v`. Return a list of all values which are less or greater as `v`.
$\text{lesser}(v,l) = \{x : x \in l, x < v\}$
$\text{less}(v,l) = \{x : x \in l, x < v\}$
do the implementation with list comprehensions.
Implement the functions using list comprehensions.
Test: `5 [1..10]`
%% Cell type:code id: tags:
``` haskell
greater c xs = [x | x <- xs, x > c]
lesser c xs = [x | x <- xs, x < c]
less c xs = [x | x <- xs, x < c]
lesser 5 [1..10]
less 5 [1..10]
greater 5 [1..10]
```
%%%% Output: display_data
%%%% Output: display_data
%% Cell type:markdown id: tags:
## problem 7
the list generated with `[0.1,0.3..0.9]` has problems with precision (has roundingerror). Do it better via using list comprehensions. (Tip: ranges of integral types do not have rounding errors).
%% Cell type:code id: tags:
``` haskell
[x/10 | x <- [1,3..9]]