Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
Jochen Schulz
haskell_labcourse
Commits
d8ca6b17
Commit
d8ca6b17
authored
Mar 16, 2016
by
Christoph Ruegge
Browse files
Fix bug in solution
parent
764573b2
Changes
1
Hide whitespace changes
Inline
Side-by-side
exercises/solutions/08 higher order tools.ipynb
View file @
d8ca6b17
%% 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)) =
\s
um_{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) =
\m
ax
\{
z
\c
olon y|z
\t
ext{ 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$,
$
\t
ext{d} f (x) = f'(x) =
\l
im_{h
\t
o 0}
\f
rac{f(x+h - f(x)}{h}$
and its numerical approximation by finite differences $
\t
ext{d}_h$ for a fixed $h
\i
n
\m
athbb{R}$,
$
\t
ext{d}_h f (x) =
\f
rac{f(x+h - f(x)}{h}$
Implement $
\t
ext{d}_h$ as higher order function and calculate the $
\t
ext{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
```
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment