Вы находитесь на странице: 1из 2

06-02552 Principles of Programming Languages Autumn Semester 2012-13

The University of Birmingham School of Computer Science c Uday Reddy2012-13

Exercise Sheet 1: Functional programming


Assigned: Monday, 8 October, 2012 Due: Monday, 15 October, 2012, 5pm. [This exercise is on functional programming in the Haskell notation. You are welcome to try out your programs on the Haskell system (ghci) in the Linux lab or using the Haskell Platform on your own machines. However, you are not required to run them on the machine. If you do run it on the machine, please submit a print out so that we can read it more easily than handwritten solutions.]

Question 1: Negate a list (5%)


negateAll :: [Integer] -> [Integer] Given a list of integers l, dene a function that returns a new list with all the elements of l with sign negated, i.e., positive integers become negatives and negative integers become positives. Example: [2, -5, 8, 0] ==> [-2, 5, -8, 0]

Question 2: Searching for an element (10%)


find :: Integer -> [Integer] -> [Integer] Given an integer x and a list l, dene a function that returns the position of the rst occurrence of x in l. (You can assume that x does indeed occur in the list.) Positions are counted as 0, 1, 2, . . .. Examples: x: 3 x: 7 l: [7, 5, 3, 8] l: [7, 5, 3, 8] ==> ==> 2 0

Question 3: Check for positive (10%)


allPositive : [Integer] -> Bool Given a list of integers l, return a boolean value indicating whether all its elements are positive, i.e., 0.

Question 4: Find the positives (15%)


positives :: [Integer] -> [Integer] Given a list of integers l, return a new list which contains all the positive elements of l. The elements should appear in the result in the same relative order as in l. Example: [2, 3, -5, 8, -2] ==> [2, 3, 8]

Question 5: Sortedness (15%)


sorted :: [Integer] -> Bool Given a list of integers l, this function should return a boolean value indicating whether l is sorted in ascending order. (There can be duplicate copies of elements. But, sortedness would require that all the duplicate copies would appear together.)

Question 6: Merging (20%)


merge :: [Integer] -> [Integer] -> [Integer] Given two sorted lists l and m, your function must return a new sorted list that contains all the elements of l and all the elements of m. Any duplicate copies of elements in l or m or their combination are retained. Examples: l: [2, 5, 5, 8] l: [2, 5, 5, 8] m:[5, 7, 8, 9] ==> [2, 5, 5, 5, 7, 8, 8, 9] m:[9] ==> [2, 5, 5, 8, 9]

Question 7: Remove duplicates (25%)


removeDuplicates :: [Integer] -> [Integer] Given a sorted list of integers l, this function must return a copy of the list l with all duplicate copies removed. Assume that the input list l is sorted in ascending order. Example: [2, 5, 5, 5, 7, 8, 8, 9] ==> [2, 5, 7, 8, 9] Your function must work in linear time, i.e., O(n) in the length of the input list. Remember that all these functions are to be dened using recursion and functional computation. No assignment statements!

Вам также может понравиться