Hello, dear friend, you can consult us at any time if you have any questions, add WeChat: daixieit

COMP2221-WE01

Programming Paradigms

Examination Paper

2022

Section A    Functional programming

Question 1

Except where otherwise stated, any code you write in this question should be in Haskell.

(a) Without using explicit recursion, write a function count  ::  [Int]  ->  Int to count the number of times the ith  entry in a list of integers is smaller than the (i + 1)th  entry.

For example

>   count   [1,  2,   - 1,  4,  5]

3

>   count   [5,  4,  3]

0

>   count   []

0

[4 Marks]

(b) Suppose we generalise countGeneral to lists of any type, with a user-

provided selection function f. Write down the type signature of countGeneral (you do not have to implement it).    [3 Marks]

(c)  Consider a data type for logical expressions

data   Expr

=  Var   String   - -  A  variable   with   given  name

|  Or   [Expr]

|  And   [Expr]

|  Not   Expr

deriving   (Eq,   Show)

Write a function  collect  ::  Expr  ->  ([String],  Int), which col- lects a list of unique variable names in an expression, and the total number of times a Not” appears. You may write helper functions if you wish. For example

>  expr  =  And   [Var   "a",  Or   [Var   "a",  Var   "b"], Not   (Var   "c")]

>   collect   expr

(["a","b","c"],1)

[10 Marks]

(d)  In lectures, we saw two versions of a left-associative fold, foldl which is lazy in its arguments, and foldl’ which applies the combining operator strictly.   Explain the difference between these two versions, and use an example to illustrate why foldl’ is typically preferred.            [8 Marks]

(e) The Functor typeclass is an example of a so-called “principled” typeclass.

An instance declaration must provide an implementation of a single func- tion fmap  ::  Functor  f  =>  (a  -> b)  ->  f  a  ->  f b, which obeys the two functor  laws.   Explain the computational pattern captured by fmap, and why obeying the two laws ensure that fmap behaves correctly for any type implementing the instance.    [5 Marks]

Question 2

(a)  Haskell, and other programming languages with rich type systems, are in-

creasingly being adopted for critical software where minimising the number of bugs is of paramount importance.

This adoption of “high-level” languages that offer significant capabilities for abstraction is even occurring in projects where the computational per- formance of the resulting software is of high importance.

Discuss some of the  reasons you think that  “high-level”  languages are being adopted, and what benefits they provide to the programmer, as well as possible disadvantages. You could consider:

•  How Haskell’s type system helps programmers build APIs that can’t be misused;

• Whether  it  is  possible to automatically  provide guarantees on the correctness of implementations;

• Whether it is possible to automatically use concurrency or other forms of parallelism (and why);

• Whether the efficiency of software designed in this way might suffer (compared to implementing directly in a low-level language like C), and why (or why not).

You may also consider other points (four to five points in total is sufficient). You may wish to illustrate your arguments with pseudocode, or examples from other programming languages.          [20 Marks]

Section B    Object oriented programming

Except where otherwise stated, the questions in this Section should be answered assuming a C# environment using the Microsoft .NET Framework Class Library.

Question 3

The UK has many charity stores that collect and resell second-hand items to raise funds.  The items are in three types:  clothing, electronics and furniture. One of the stores wants to develop an application for managing its items.  You are asked to design the system architecture to facilitate different functionalities.

For each of the required functionalities below: (1) Draw a fragment of a Unified Modeling Language class diagram using notations as shown below to present your system designs, following good principles of OO design. You can use computer drawing program or scan a hand-drawn picture. (2) Write no more than 80 words to explain your key designs and the OOP concepts employed. You do not need to write source code, but you may reference C# functions and classes if needed.

 

UML Notations. 

(a) Storing the features of the items sold in the store, which includes Skirts”,

“Coats”,  “Consoles”,  “Screens”,  “Tables” and  “Bookcases” .  Provide ex- ample features to showcase your design.     [6 Marks]

(b)  Estimating the selling price of all items in the store.  The selling price is

dependent on the age and original price of an item, but different types of item use different formulas to combine the two values.             [4 Marks]

(c)  Printing a line of text for each of the items in the store, which combines the name and age of the item, with the use of the C# IFormattable1  interface. [4 Marks]

(d) Sorting all the items in the store according to their original prices, with the use of the C# IComparable2  and IComparer3  interfaces.  Two sorting functions are required: one in ascending price order and one in descending price order.    [6 Marks]

Question 4

Binary images are images where pixels are either 0 or 1. They are usually stored as a list of Boolean values with the length of w * h, where w is the image width and h is the image height.  It is suggested that this way of storage is memory inefficient. A potentially better way is to use a list to store the positions of only the pixels with a value of 1, which are known as the marked pixels. You are asked to complete the implementation of this idea.

The partial source code of the supporting classes, a main program demonstrating the functionalities and the expected outputs are shown below. You are asked to implement the functionalities that can generate the outputs as shown.  As the aim is to reduce memory usage, you should operate directly on the list of marked positions without creating a new list that requires the full w * h memory.  You should also do proper checking to ensure valid operations and memory storage.

The Pos and BinaryImage Classes

class   Pos

{

public   int  x,  y;

}

class   BinaryImage

{

int   iImageWidth ,   iImageHeight;

private   List <Pos >   lstMarked   =  new   List <Pos >();

public   BinaryImage(int   iImageWidthIn ,   int   iImageHeightIn) {

iImageWidth   =   iImageWidthIn;

iImageHeight   =   iImageHeightIn;

}

//  Extra   functions   to   be   implemented   here

}

The Main Program

static void Main(string[] {

BinaryImage biA = new biA .MarkPos(1, 1);    biA .MarkPos(1, 2);

args)

 

BinaryImage(6, 4);

biA .MarkPos(2, 2);

Console .WriteLine("BinaryImageA:");

biA .Print();

BinaryImage biB = new BinaryImage(6, 4);

biB .MarkPos(1, 2);

biB .MarkPos(2, 2);

biB .MarkPos(3, 3);

Console .WriteLine("BinaryImageB:");

biB .Print();

BinaryImage biC = biA .Union(biB);

Console .WriteLine("BinaryImageC␣=␣AUnionsB:"); biC .Print();

BinaryImage biD = biA . Intersetion(biB);

Console .WriteLine("BinaryImageD␣=␣AIntersetsB:"); biD .Print();

}

 

The output of the main program.

(a) Write the function MarkPos in the BinaryImage class, which adds a posi- tion into the list lstMarked. Then, explain the programming logic with no more than 50 words.                         [6 Marks]

(b) Write the function Print  in the BinaryImage class,  which  prints the content of lstMarked, with marked positions as “*” and unmarked positions as “ .” . Then, explain the programming logic with no more than 50 words. [6 Marks]

(c) The union of two binary images contains all the marked positions that are contained in either image. Write the function Union in the BinaryImage class.  Then, explain the programming logic with no more than 50 words. [7 Marks]

(d) The  intersection  of two  binary  images  contains  only the  marked  posi- tions that are in both images.  Write the function Intersection in the BinaryImage class.  Then, explain the programming logic with no more than 50 words.       [7 Marks]

(e) When using the proposed idea to store real-world binary images, in some images, the memory required is less than that of a Boolean list of w * h. However, in other images, the proposed idea costs more memory.  Explain why with no more than 50 words.                        [4 Marks]