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

Fix bug in solution

parent 764573b2
%% Cell type:markdown id: tags:
# 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
firstIndexWhere pred xs = fst $ head $ filter (\(n, x) -> pred x) $ zip [0..] xs
```
%% 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
removeChar s c = filter (/= c) s
```
%% Cell type:code id: tags:
``` haskell
countChar s c = length $ filter (== c) s
```
%% Cell type:code id: tags:
``` haskell
firstAfterChar s c = head $ dropWhile (/= c) s
firstAfterChar s c = head . tail $ dropWhile (/= c) s
```
%% 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
sumOfSquares :: Num a => [a] -> a
sumOfSquares = sum . map (^2)
```
%% Cell type:code id: tags:
``` haskell
sumOfSquares [1..5]
```
%% 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
sumCond :: Integral a => (a -> Bool) -> a
sumCond funct = sum (takeWhile (funct) (map (^2) $ filter odd [1..]))
sumCondLC :: Integral a => (a -> Bool) -> a
sumCondLC funct = sum (takeWhile (funct) [n^2 | n <- [1..], odd n])
```
%% Cell type:code id: tags:
``` haskell
sumCond (<100)
sumCondLC (<100)
```
%% 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
largestDivisible :: Integral a => a -> a -> a
largestDivisible x y = head (filter p [x,x-1..])
where p z = z `mod` y == 0
```
%% Cell type:code id: tags:
``` haskell
largestDivisible 99 13
```
%% 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
d h f x = (f (x+h) - f x) / h
```
%% Cell type:code id: tags:
``` haskell
d 0.001 (^2) 2
```
%% Cell type:markdown id: tags:
## problem 7
Create the (infinte) list of Fibonacci numbers using in two different ways using
- `iterate`
- `zipWith`
Use this list to print all even Fibonacci numbers below 1000.
%% Cell type:code id: tags:
``` haskell
fibs = map fst $ iterate (\(a, b) -> (b, a + b)) (1, 1)
```
%% Cell type:code id: tags:
``` haskell
fibs' = 1 : 1 : zipWith (+) fibs (tail fibs)
```
%% Cell type:code id: tags:
``` haskell
filter even $ takeWhile (< 1000) fibs
```
%% Cell type:markdown id: tags:
## problem 8
Quicksort. Quickly:
- choose a pivot element
- split the list into a sublist of smaller elements and a sublist of larger or equal elements
- sort the sublists
See also:
- [Quicksort - on Wikipedia](http://en.wikipedia.org/wiki/Quicksort)
- [Quicksort - in Auckland](https://www.cs.auckland.ac.nz/software/AlgAnim/qsort.html)
- [Quicksort - on Youtube](http://youtu.be/ywWBy6J5gz8)
Implement quicksort using appropriate higher order list functions.
%% Cell type:code id: tags:
``` haskell
quicksort [] = []
quicksort xs@(x:_) = (quicksort $ filter (< x) xs) ++
filter (== x) xs ++
(quicksort $ filter (> x) xs)
quicksort [5, 1, 8, 3, 9, 2]
```
%% Cell type:markdown id: tags:
## problem 9
- Write a function `primes` which lists all prime numbers, and a function `isPrime` which checks whether a number is prime.
- Write a function `primfactors` that factorizes a natural number into its prime factors.
%% Cell type:code id: tags:
``` haskell
import Data.List
primes = 2 : filter isPrime [3,5..]
isPrime p = [] == [d | d <- takeWhile (lessSqrt p) primes, mod p d == 0]
where lessSqrt = (>=) . floor . sqrt . fromIntegral
primfactors :: Integer -> [Integer]
primfactors 0 = []
primfactors 1 = []
primfactors a = smallest_prime : primfactors (div a smallest_prime)
where smallest_prime = (head . dropWhile (\p -> mod a p /= 0)) primes
```
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment