You are on page 1of 3

# Prof.aa Dr. J. Giesl Notes: Please solve these exercises in groups of two!

## Functional Programming SS12 Exercise Sheet 4 (due 16.05.2012) M. Brockschmidt, F. Emmes

The solutions must be handed in directly before (very latest: at the beginning of ) the exercise course on Wednesday, 16.05.2012, 11:45, in lecture hall AH 2. Alternatively you can drop your solutions into a box which is located right next to Prof. Giesls oce (until the exercise course starts). Please write the names and immatriculation numbers of all (two) students on your solution. Also please staple the individual sheets! Exercises marked with V3B are only relevant for students attending the V3B (Bachelor Informatik, Master Mathematik) version of the lecture. Exercises marked with V3M are relevant for all other students. Unmarked exercises are relevant for all students. Exercises marked with a star are voluntary bonus challenge exercises with advanced diculty. The points reachable in these exercises do not count towards the total number of reachable exercise points, so you may ignore them without any disadvantage. However, the bonus points count towards your point total, so you can work on them instead of working on the (more boring) standard exercises.

## Exercise 1 (V3B List Comprehensions):

(1 + 2 + 2 = 5 points)

In this exercise you should learn how to use the Haskell list comprehension syntax. a) In Haskell, there is a pre-dened function replicate :: Int -> a -> [a]. When called with the arguments n 0 and e, it returns a list of length n which contains only elements with value e. For example replicate 3 c returns the string "ccc". Implement the function repl :: Int -> a -> [a], which should do the same as the function replicate, but dene it using only one dening equation and only a list comprehension as its right-hand side. You may use the shorthand notation [a..b], which returns a list of all integers from a to b, but you may not use replicate in your implementation of repl. b) Write a function allStrings :: [Char] -> Int -> [String], such that allStrings s n returns a list of all non-empty strings consisting only of characters from the list s and which have at most length n. The order in which the strings are returned is not important. For example allStrings "ab" 2 might return ["a","aa","ab","b","ba","bb"]. You may use up to two dening equations, but one right-hand side must be a list comprehension. c) Write a function palindromes :: [String] -> [String], that gets a list of strings, and returns a list of all strings s = w1 w2 , where w1 and w2 are strings contained in and s is a palindrome (i.e., s is the same as s reversed). For example palindromes ["say", "on", "not", "to", "as"] yields ["sayas","noton","tonot"]. Use only one dening equation and only a list comprehension as its right-hand side. Hints: You may use the pre-dened function reverse :: [a] -> [a].

## Exercise 2 (Lazy Programming):

a) Consider the following denition of the function minsort: minsort minsort minsort where :: [ Int ] -> [ Int ] [] = [] xs = m : minsort rest m = minimum xs rest = filter (/= m ) xs

(2 + 2 = 4 points)

Functional Programming SS12 Exercise Sheet 4 (due 16.05.2012) Here, minimum is the predened function computing the minimum of a list. We now dene minimum xs = head (minsort xs). The functions minimum and filter (/= m) both have linear complexity in the length of the input list. What is the complexity of the function minimum? b) Implement the innite list rands :: [Int] of pseudo-random numbers following the linear congruential method. For that, we need parameters m (the modulus ), a (the multiplier ), and c (the increment ). Then, for any given number rn , the next pseudo-random number can be computed using the formula rn+1 = ((a rn ) + c) % m.1 Use m = 216 , a = 25713 and c = 13849 and 42 as rst random number. Furthermore, dene a function randSum :: rst k elements of rands. Int -> Int such that randSum k yields the sum of the

## Exercise 3 (V3B Cyclic data):

(4 points)

Implement a method posSublistSums :: [Int] -> [Int] such that the call posSublistSums xs returns a list res whose n-th entry is the sum of the rst n positive numbers in xs. Your solution should work in linear time (w.r.t. the length of the input list) by using a cyclic data object. Hints: Consider that the n-th element of the result res is the sum of the (n 1)-th element of res and the n-th positive number in xs. You may use the predened function zipWith: zipWith :: ( a -> b -> c ) -> [ a ] -> [ b ] -> [ c ] zipWith f ( x : xs ) ( y : ys ) = f x y : zipWith f xs ys zipWith _ _ _ = [] This function can be used to combine two lists element-wise, i.e., zipWith f [x1 , x2 , . . .] [y1 , y2 , . . .] yields the list [(f x1 y1 ), (f x2 y2 ), . . .]. Pre-dened functions such as map, foldr, sum, zipWith, filter work in linear time (w.r.t. the list length) provided that their higher-order parameters work in constant time.

## Challenge Exercise 4 (Lazy Programming):

(5* points)

In this exercise, we want to implement a function repMin that takes a list xs and returns a list of the same length containing only the minimal element of xs. For example, repMin [2,1,3] should yield [1,1,1]. The straighforward implementation would look like this: repMin xs = map (\ _ -> min ) xs where min = minimum xs However, this requires two iterations over the list: rst, minimum searches for the minimal element, and then the map call replaces all elements. Using lazy programming, it is possible to nd the minimum and replace all elements in one iteration. Implement a function repMin :: (Ord a) => [a] -> [a] that does this and briey explain why your solution works as described. Hints: Use a helper function repMin :: (Ord a) => [a] -> b -> (a, [b]) that takes a list xs and a replacement value r and returns a pair where the rst element is the minimal value of xs and the second element is a list of the same length as xs, but where all elements are r. Implement repMin by calling repMin such that the minimal value returned by repMin also appears as input argument to repMin.

1 Here,

% describes the modulo operation, i.e, the remainder of division of one number by another.

## Exercise 5 (V3M Input and Output):

(2 + 4 + 3 = 9 points)