Commit 764573b2 by Christoph Ruegge

### Reorder and rename exercises

parent 37a86fb6
 %% Cell type:markdown id: tags: # Exercises 02 # Exercises: first steps ## 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_ () and/or _Hayoo!_ () (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 rainy :: Bool cold :: Bool windy :: Bool (assume that they are filled) Use if ... then ... else (and boolean operators) to return the following strings: - "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 # Exercises: lists and tuples ## 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 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 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 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{less}(v,l) = \{x : x \in l, x < v\}$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: # Excercise 04 # 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: # Exercise 05 # Exercises: pattern matching ## problem 1 Write a function whatToDoToday that accepts three Bools rain, cold and wind and returns a string - "kite flying" on wind and no rain - "don't forget your umbrella" on rain and no cold - "stay at home at the fireplace" on rain and cold - "take a sunbath" without rain and wind Use case ... of ... and pattern matching. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 2 Same as above, but use partial function definition instead of case. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 3 For tuples with 2 elements, fst and snd extract the first and second component, respectively. Write functions fst3, snd3 and thr3 that work on tuples with 3 elements. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 4 Write a function reverseMe :: String -> String that reverses its argument. Proceed as follows: - empty strings are left unchanged - non-empty strings are split into first letter and rest - reverseMe is then recursively applied to the rest, and the first letter is appended (note the type of (++)) %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 5 - Write a function initial that returns the first letter of a string. - Use Hoogle or Hayoo to find a function that splits a string into words (*Hint*: What should be the type of that function? Type signatures can be directly used for searching.) - Write a function initials that accepts a name in the form "Firstname Middlename Lastname" and returns the initials in the form "F. M. L." %% Cell type:code id: tags:  haskell  ... ...  %% Cell type:markdown id: tags: # Exercise 06 # Exercises: syntactic sugar ## problem 1 Rewrite the function cylinder, haskell cylinder :: (RealFloat a) => a -> a -> a cylinder r h = let sideArea = 2 * pi * r * h topArea = pi * r^2 in sideArea + 2 * topArea  to use where instead of let. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 2 Write a function which calculates the area and the centre of a rectangular from given lower left and upper right coordinates. The coordinates are all tuples themselves and all numbers are part of the class RealFloat and are contained in a list of 2. The return values are also in the form of a tuple. Use pattern matching and the where-clause. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: problem 3 --------- Write a function which checks if a given number is inside an also given range. If the number is in range, return its string representation, and if not, return "out of bounds". %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: problem 4 --------- Write a function which takes a list and checks if its empty, has one or more than one element. It returns the following strings depending on the length of the list$0$: "The list is empty"$1$: "The list has one element"$>1$: "The list has more than one element" do not use length but pattern matching and/or case %% Cell type:code id: tags:  haskell  ... ...  %% Cell type:markdown id: tags: # Exercises 10 # 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 %% 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: # Exercise 08 # Exercises: higher order tools ## problem 1 Write a function firstIndexWhere that accepts a predicate a -> Bool and a list [a] and returns the index of the first element at which the predicate returns True. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 2 Write functions accepting a String and a Char c and returning - the string with all occurrences of c removed - the number of occurrences of c - the letter after the first occurrence of c using appropriate higher order list functions. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 3 Write a pointfree function, which calculates the sum of the sqaures of the elements of a given list. %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 4 Write a function$f$, which the calculates the sum of all squares of uneven numbers while a predicate function$g(n)$holds true .$f(g(n)) = \sum_{n=1}^{N} n^2$find two different implementations with and without list comprehensions %% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 5 Write a function which takes and upper limit and a divisor and returns the biggest possible number below the upper limit which has the given divisor.$f(x,y) = \max \{z \colon y|z \text{ and } z < x \}$%% Cell type:code id: tags:  haskell  %% Cell type:markdown id: tags: ## problem 6 A mathematical concept similar to higher order functions are functionals and operators (in the sense of functional analysis). Examples of simple operators are the derivative operator$d$,$\text{d} f (x) = f'(x) = \lim_{h \to 0} \frac{f(x+h - f(x)}{h}$and its numerical approximation by finite differences$\text{d}_h$for a fixed$h \in \mathbb{R}$,$\text{d}_h f (x) = \frac{f(x+h - f(x)}{h}$Implement$\text{d}_h$as higher order function and calculate the$\text{d}_h f(x)$for$f(x) = x^2$and$h = 0.001$at$x = 2\$. %% Cell type:code id: tags:  haskell `