of a natural number n can be computed as follows.
fac n = fac_help n 1
where fac_help 0 acc = acc
fac_help n acc = silnia_help (n - 1) (n * acc)
Here the variable acc of function fac_help is called an accumulator.
Please define a function
fib n, whose value is the n-th element of the Fibonacci sequence
(give two versions of fib: one recursive and one using accumulators).
The exponentiation b^e (for natural numbers b and e)
can be computed using b^e = (b^(e/2))^2.
Please write an exponentiation function exp b e using this property
(again one version using recursion and one using accumulators).
Please define the following functions for lists:
a) append l m concatenating l i m.
b) member x l checking whether x is an element of l
c) reverse l reversing l.
d) last l giving the last element of l.
e) delete x l erasing x from l.
f) pairing l1 l2, transforming
l1 i l2 into a list of corresponding pairs. Example:
> pairing [1,2,3] [a,b,c]
[(1,a),(2,b),(3,c)]
g) split x l splitting l into two lists l1 i l2, where
l1 contains the elements smaller than
x and l2 the elements greater than x.
h) map f l applying the one-argument function f
to all elements of l.
a) Please define a function map2 f l1 l2 applying the one-argument function f
to all elements of l1 i l2. Example:
> map2 (+) [1,2,3] [8,9,10]
[9,11,13]
b) Please define a function filter p l, whose value is the list of all element of l
fulfilling the one-argument predicate p.
What are the types of the functions defined in exercises 4,5,6,9 and 10?
a) Please define a function product term next a b analogous to function sum.
Please show, how product can be used to define both function
factorial and the approximation of p using the formula
p/4 = 2 · 4 · 4 · 6 · 6 · 8 ··· / 3
· 3 · 5 · 5 · 7 · 7 ··· .
b) Please define a function accumulate combiner null-value term a next b generalizing functions sum i product:
The arguments term, next, a, and b are the same as in the definitions of
sum i prod.
combiner is a two-argument function describing, how term a is tied together
with the recursive result.
null-value is the initial value used in the basic step of recursion.
Please also show, how sum i product can be defined using
accumulate.
c) Even accumulate can be generalized:
Please define a function filter-accumulate with an additional argument pred.
pred is a one-argument predicate and filter-accumulate ties term a to the result only if
a fulfills pred.
Using filter-accumulate please compute the sum of squares of the prime numbers
in the interval [a,b]
and the product of the natural numbers i smaller than n for which gcd(i,n) = 1.
Please define a function foldr analogous to function foldl.
What is the type of foldr?
Using function foldl or foldr please define the following functions
a) prod
b) length
c) and (for a list of Boolean values)
d) gcd (for a list of natural numbers)
e) reverse
f) delete
g) map
h) filter pred
i) forall pred.
Please define the function insertionsort :: (a -> a -> Bool) -> [a] -> [a]
using an appropriate higher order function.
Please define a function iter f n for a one-argument function f and a natural number n.
The value of iter f n is a function computing fn.
Examples: > iter square 2 3
81
> let f = iter square 2 in f 5
625
> iter square 0 7
7
Please define a function dc realizing the scheme for solving divide-and-conquer-problems.
dc has the following (functional) arguments:
- test testing whether the problem instance is easy enough
- end solving easy enough problem instances
- divide dividing a problem into subproblems
- combine combining solutions of subproblems.
Please define mergesort and Karatsuba-multiplication using dc.
What are the types of the following expressions?
a) +
b) + 37
c) append
d) append [1,2]
e) map
f) map square [1,2,3,4,5]
g) map square [['a']]
h) map length [['a']]
i) foldl
j) foldl (++)
k) foldl (++) []
l) f 7
m) \f -> f 7
n) + (f x) (g x)
o) f 7 (g 'x')
p) \f -> f (g x)
q) (\f -> f (g x)) square