0% found this document useful (0 votes)
7 views4 pages

August-December 2023 Assignments

This document outlines the assignment instructions for a Functional Programming course at Chinhoyi University of Technology. It includes detailed tasks for students to implement various functions in Haskell, covering topics such as arranging numbers, manipulating strings, calculating grain production, and working with country records. Additionally, it specifies file naming conventions and submission guidelines.

Uploaded by

striveemashava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

August-December 2023 Assignments

This document outlines the assignment instructions for a Functional Programming course at Chinhoyi University of Technology. It includes detailed tasks for students to implement various functions in Haskell, covering topics such as arranging numbers, manipulating strings, calculating grain production, and working with country records. Additionally, it specifies file naming conventions and submission guidelines.

Uploaded by

striveemashava
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

CHINHOYI UNIVERSITY OF TECHNOLOGY

SCHOOL OF ENGINEERING SCIENCES AND TECHNOLOGY

DEPARTMENT OF COMPUTER ENGINEERING

COURSE : FUNCTIONAL PROGRAMMING

CODE : CUCEM 211

DATE : August-December 2023 ASSIGNMENT

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)

For example orderTriple (2, 1, 4) returns (1,2,4) [5 marks]

b. Write the function:


squareOdds :: [Integer] -> [Integer]
that takes a list of Integers, st, and returns a list of Integers that is just like st,
except that each odd element of st is replaced by the square of that element. In your
solution, you might find it helpful to use the built-in predicate odd.
For example,
squareOdds [] returns []
squareOdds [3] returns [9]
squareOdds [1,2,3,4,5,6] returns [1,2,9,4,25,6] [7 marks]

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.

biggest-sum :: [[Int]] -> [Int]

For example, biggest_sum [[2,5], [-1,3,4], [2]] returns [2,5] [8 marks]


If one or more lists return the same result, then return one of them only.
d. Write another function biggest_sum’ that does the same as biggest_sum but
uses folding.

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).

type MonthNo = Int


grain :: MonthNo -> Double
grain n = fromIntegral (n `mod` 12) * 4.6

Complete the definition of the following function:


leastGrain :: MonthNo -> Double
leastGrain n
| n < 1 = 0
| otherwise = -- (complete this case)
such that leastGrain n (when n > 0) gives the least amount of grain produced from
month 1 up to and including month n. For example, suppose that the grain produced in
month 3 is 14.5 (grain 3 returns 14.5). Now if month number 3 is the least producing of
the first 50 months, then leastGrain 50 returns 14.5. [9 marks]
Hint: The function min may be useful.

Question 4
Consider the type of Country database records below:
Data Country = Record {name :: String, gdp :: Integer, pop ::
Integer,
mortality :: Float}

a. Write the function findLDCs :: [Country] -> [(String,Float)]


that takes a list of records of type Country, cs, and returns a list of pairs of
the name and mortality of each country, in the same order as in cs, provided
the mortality is greater than 12.1 .

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)]

b. Write another function findCs :: (Country -> Boolean) ->


[Country] -> [(String,Float)] that takes a predicate p, a list of records
of type Country, cs, and returns a list of pairs of the name and mortality of each
country, in the same order as in cs, that satisfy p (i.e. for which p is true). p is any
function that takes a Country, performs a test and returns a Boolean result. (You do

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

You might also like