August-December 2023 Assignments
August-December 2023 Assignments
INSTRUCTIONS
1. Submit soft copies of your files in a folder. The name of the folder must be your student number.
2. At the top of each .hs file, put the following information (NOTE: substitute with your own
details!!!):
{-
NAME = Donna Musiyandaka
STUDENT NO = C21078979Y
-}
3. Save all functions for questions 1,2,3 and 4 in ONE file named studentnoA.hs where for a student
with ID C21078979Y the file name is c21078979yA.hs
4. Save Question 5 in a file named studentnoB.hs where for a student with ID C21078979Y the file
name is c21078979yB.hs
5. Use the function names given in the questions. You are free to implement other helper functions
so that you achieve your results.
Page 1 of 4
6. Remember to adhere to rules of correct indentation.
Question 1
a. Write a function that arranges 3 numbers in a triple in order from lowest to highest.
orderTriple :: (Int,Int,Int) -> (Int,Int,Int)
c. Write a function called biggest_sum that takes a list of one, or more, integer lists
as parameters and returns the list with the greatest sum. The function should make
use of recursion.
Question 2
Suppose you are given x which is a character and xs which is a string,
a. Write a function flip_case x that returns an uppercase version of x if x is [4 marks]
lowercase and a lowercase version of x if x is uppercase. If x is not a letter of the
English alphabet, then the function returns that character. Assume x is a character.
b. Write a function called invert_case1 xs that uses the map function (and no
recursion) and returns a String that is the same as xs, except that lowercase letters [5 marks]
become uppercase and uppercase letters become lowercase. Any other characters
remain the same. For example invert_case1 “Tell Me” returns “tELL mE”.
[5 marks]
c. Write another function called invert_case2 xs that does the same as
invert_case1 but uses recursion.
d. Write another function called invert_case3 xs that does the same as [5 marks]
invert_case1 but uses a list comprehension and no recursion and no map
Page 2 of 4
function.
Question 3
In this question you should assume that you have a function grain which computes the total
grain (in tonnes) produced in Bulawayo for a given month (represented by MonthNo,
where months are numbered from 1 and upwards).
Question 4
Consider the type of Country database records below:
Data Country = Record {name :: String, gdp :: Integer, pop ::
Integer,
mortality :: Float}
For example:
findLDC [Record {name=”Zt”, gdp = 25000000 , pop
=4000000, mortality = 9.6}, Record {name=”Ab”, gdp = [10 marks]
525000000, pop =15000000, mortality = 13.2}, Record
{name=”Gh”, gdp = 54000000 , pop =5000000, mortality =
8.2}]
returns [(“Ab”,13.2)]
Page 3 of 4
not need to give the p function, but simply assume it exists)
For example:
findCs lowgdp [Record {name=”Zt”, gdp = 25000000 , pop
=4000000, mortality = 9.6}, Record {name=”Ab”, gdp = [10 marks]
525000000, pop =15000000, mortality = 13.2}, Record
{name=”Gh”, gdp = 54000000 , pop =5000000, mortality =
8.2}]
returns [(“Zt”,9.6), (“Gh”,8.2)]
where lowgdp tests for gdp values below 60000000.
Question 5
If a 7 metre ladder is placed on the side of a building at a y degree angle, then the
height at which the ladder touches the building can be calculated as height = 20 ×
3/2(sine y)
Write a program to determine and display the various heights attainable (using the above
information) between the range 20 degrees to 90 degrees, at 5-degree intervals. Your
program should include:
a. A function find_degree that accepts an angle as a parameter, determines the
height as described above and outputs the result.
b. A main function that displays the heights for all the angles within the 20 – 90 [10 marks]
degree range, at 5-degree intervals.
c. Write a function procRange that accepts two integers u and v and finds the
heights for all angles (at 5 degree intervals) in the range u to v (when u<v),
otherwise the range used is v to u.
d. Write another function proRange2 that is a generalised version of
procRange and accepts a range made from u and v where u,v are of the same
type but the type can be any number. Also the function procRange has
additional parameters a function f (that takes a number and returns a
number) and intervals s. proRange2 then applies f to each element of the list
constructe using the range u, v and the interval s.
THE END
Page 4 of 4