CA215 Languages and
Computability
Functional Programming Lab
8: Revision
Aim: The aim of this week's lab session is to revise.
1. Single list as input
a) Define 3 different functions which take a list of Int and return a list containing only the even ones. Use recusion in the first definition, list comprehension in the second definition and the standard function filter in the third definition.
Examples of input and output:
Hugs> myEven [1,2,3,4,5,6]
[2,4,6]
Hugs> myEven [1,22,32,42,52,62]
[22,32,42,52,62]
Hugs> myEven [3,5,7,9]
[]
b) Write code which takes a list of Char and returns a list containing only the consonants. Assume
the list contains only lowercase characters. The top-level function must have the type declaration
consonants :: [Char] -> [Char]. Examples of input and output:
Hugs> consonants "abcde"
"bcd"
Hugs> consonants "abcdefghi"
"bcdfgh"
c) Write code which takes a list and returns the reverse. The top-level function must have the type
declaration myReverse :: [a] -> [a]. Examples of input and output:
:
Hugs> myReverse [1,2,2,3,4,4,5,5,5,6,6,7]
[7,6,6,5,5,5,4,4,3,2,2,1]
Hugs> myReverse ['a','b','c','d','e']
"edcba"
Hugs> myReverse "abcde"
"edcba"
Hugs> myReverse [5,4,3,2,1]
[1,2,3,4,5]
d) Write code which takes a list of Chars and returns the number of vowels. The top-level function
must have the type declaration vowels :: [Char] -> Int. Examples of input and output:
:
Hugs> vowels ['a','b','c','d','e','f','g','h','i']
3
Hugs> vowels ['a','e','i','a','e','i']
6
e) Go back to lab 4, exercise 2. Define the function stringToUpper using list comprehension instead of primitive recursion.
f) Define a function convertChar that converts all the characters in a string to their ASCII code (remember that there is a standard function Ord that converts a single caracter into its ASCII code). Define the same function using primitive recursion.
2. Two lists as input
a) Write code which takes 2 lists as input and append the second to the end of the first. The top-
level function must have the type declaration appendLists :: [a] -> [a] -> [a]. Examples of input and output:
:
Hugs> appendLists [1,2,3] [4,5,6]
[1,2,3,4,5,6]
Hugs> appendLists [1,2,3] [3,2,1]
[1,2,3,3,2,1]
Hugs> appendLists [1,2,3] [3,2,1,4,4,4]
[1,2,3,3,2,1,4,4,4]
Hugs> appendLists [1,2,3,5,5,5,5,5,5] [3,2,1,4,4,4]
[1,2,3,5,5,5,5,5,5,3,2,1,4,4,4]
b) Write code which takes 2 lists and merges them into a single list by putting the first element of
list1, then the first element of list2, then the second element of list1, then the second element of
list2 etc. The top-level function must have the type declaration mergeLists :: [a] -> [a] ->
[a]
. Examples of input and output:
:
Hugs> mergeLists [1,2,3] [4,5,6]
[1,4,2,5,3,6]
Hugs> mergeLists [1,2,3] [3,2,1]
[1,3,2,2,3,1]
Hugs> mergeLists [1,2,3] [3,2,1,4,4,4]
[1,3,2,2,3,1]
c) Write code which takes 2 lists of Ints and returns a list of products by multiplying the first ele-
ment of list1 with the first element of list2, the second element of list1 with the second element
of list2 etc. The top-level function must have the type declaration multiplyLists :: [Int] ->
[Int] -> [Int]. Examples of input and output:
:
Hugs> multiplyLists [1,2,3] [4,5,6]
[4,10,18]
Hugs> multiplyLists [1,2,3] [3,2,1]
[3,4,3]
Hugs> multiplyLists [1,2,3] [3,2,1,4,4,4]
[3,4,3]
3. Higher Order Sort
Write a higher order sort
function hosort :: (a
-> a -> Bool) -> [a] -> [a]
which takes two arguments: a relative ordering relation and the list to be
sorted. The list should be sorted according to the given ordering relation (all
consecutive elements in the list satisfy the relation). For example:
hosort
(>) [4,3,1,7,5] = [7,5,4,3,1]