Haskell exercise sheet 2

Basic list processing

Functions on lists

Implement some general functions on lists such that the following expressions evaluate to True. Some of them can be found in the Prelude. Of course, do not use them.

In [ ]:
myLast ['a', 'b', 'c', 'd'] == 'd'
In [ ]:
myElementAt ['a', 'b', 'c', 'd'] 1 == 'b'
myElementAt ['a', 'b', 'c', 'd'] 2 == 'c'

Generate a list

Use a list as an accumulator to build up the list by prepending elements.

In [ ]:
myRange 0 3 == [0, 1, 2, 3]
myRange 5 12 == [5, 6, 7, 8, 9, 10, 11, 12]
In [ ]:
myDuplicate ['a', 'b', 'c'] == ['a', 'a', 'b', 'b', 'c', 'c']
myDuplicate [1, 2, 3] == [1, 1, 2, 2, 3, 3]
In [ ]:
myMulticate ['a', 'b', 'c'] 2 == ['a', 'a', 'b', 'b', 'c', 'c']
myMulticate [1, 2, 3] 3 == [1, 1, 1, 2, 2, 2, 3, 3, 3]
In [ ]:
myEven [5, 6, 7, 8, 9, 10, 11, 12] == [6, 8, 10, 12]
In [ ]:
mySplitAt [5, 6, 7, 8, 9, 10, 11, 12] 3 = ([5, 6, 7], [8, 9, 10, 11, 12])
mySplitAt ['a', 'b', 'c', 'd'] 2 == (['a', 'b'], ['c', 'd'])
In [ ]:

Query a list

Consider the following list of numbers and write functions that answer a few questions about the numbers in that list. Do not use existing functions from the Prelude, except head, tail and (:).

In [ ]:
z = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5, 8, 9, 7, 9]

How many numbers are greater than five?

In [ ]:

What is the smallest number greater than 5?

In [ ]:

What is the average value of the numbers that come after a 5?

In [ ]: