Practical Exam #1

Task 1

We have two lists of positive integers in non-decreasing order. We wish to merge them into a single list also in non-decreasing order. Non-decreasing means that no two adjacent elements are in decreasing order.

Write a function merge which given two lists of positive integers in non-decreasing order returns a single list in non-decreasing order containing the same elements.

You can assume the two lists contain only positive integers.

For example:

? merge [1,3,5] [2,4,6]
[1, 2, 3, 4, 5, 6]
? merge [4,5,6] [1,2,3,4]
[1, 2, 3, 4, 4, 5, 6]
? merge [2,2,8,8] [3,4,5]
[2, 2, 3, 4, 5, 8, 8]
? merge [1,2,3,4,5] [42]
[1, 2, 3, 4, 5, 42]
? merge [1,1] [1,1]
[1, 1, 1, 1]
? merge [1,1,4,5,5,7,7,9,42] [3,3,5,7,8,9,12]
[1, 1, 3, 3, 4, 5, 5, 5, 7, 7, 7, 8, 9, 9, 12, 42]
? merge [1,2,3,4,5] [1,2,3,4,5]
[1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
?  merge [1,2,3,4,5] [3,3]
[1, 2, 3, 3, 3, 4, 5]
? merge [5] [2,4,6,8,10,12]
[2, 4, 5, 6, 8, 10, 12]

Task 2

A list of integers is said to be proper if any maximal subsequence of even numbers in the list is of even length.

For example you won't find a run of three or five consecutive even numbers in a proper list unless it is part of a longer (even length) run. You might however find a run of four, or six, consecutive even numbers in a proper list.

Write a Haskell function proper which given a list of integers returns True if, and only if, the list is proper.

You can assume the list contains only postive integers.

For example:

? proper [5,3]
True
? proper [4,8]
True
? proper [4,8,6]
False
? proper [2,2,2,2]
True
? proper [2,2,2,2,2]
False
? proper [5,6,10,3,5,6]
False
? proper [5,6,10,3,5,6,2]
True
? proper [5,6,4,6,8,4,3,5]
False
? proper [5,6,4,6,8,4,8,3,5]
True
? proper [1,4,6,5,8,6,8,3,6,6,7,4,4]
False
? proper [1,4,6,5,8,6,8,42,3,6,6,7,4,4]
True