Microsoft Word – Assignment2-2017.docx
159.272 Programming Paradigms
Assignment 1
Functional Programming in Haskell
Total marks: 15 marks
Due: 7pm Thursday 13 April 2017
Your task for this assignment is to write several small programs in Haskell, following the instructions
below. You need to make sure that your programs/functions work by trying different testing scenarios
(test your functions as much as possible!).
Write the following programs in a file called ”Name”_”Number”_Assignment1.hs. Replace “Name” with
your full name and “Number” with your student number. For example, if Amjed submits his work for
marking (which he wouldn’t!), then his file will be named as follow:
AmjedTahir_12345_Assignment1.hs.
You can put your functions/programs in multiple .hs files, however, if you decide to do so then you
will need to compress all your files (i.e., zip your files into .zip or .7z files) and submit your
compressed/zipped file. The mark of each individual question is provided next to the question.
1. Write a function called first which takes an Int value (n) and returns a list of the first
n Int starting from 1.
(1 mark)
Example:
> first 2 []
> [1,2]
2. Draw N different random numbers from the set 1..M.
Example :
>random_range 10 59
>[23,1,17,33,21,37]
Import and use System.Random library
(1 mark)
3. You have been assigned to find a committee of 4 lecturers to examine several final year
projects in a school. The total number of lecturers in the school is 20. Define a function
“combinations” that finds all possible combinations of any number of any given list. As a
rule, you cannot put the same 4 teachers in more than one committee.
You must use recursion in your code
Example:
> combinations 4 [a,b,c,d,e,f]
[“abcd”,”abce”,”abcf”, “acde”,”acdf”,…]
(2 marks)
4. Write a program that can convert Mass from one unit to another. You program should ask
the user for the value that needs to be converted (e.g., n), the original unit of conversion
and the unit that you need to convert to.
You calculator should convert to and from the following units:
1) Gram, 2) Kilogram, 3) Ounce and 4) Pound.
For accurate conversion values, check the following link: here and here. You need to show
only 4 decimal numbers.
You MUST declare new data and type in your code. (3 marks)
For example:
To convert 10 Grams to Ounce
> massFun 10 G O
> 0.3527
5. Encode a list through its length. Your program should take a list of elements, and then
find consecutive duplicates of items. Then you need to encode a list of items and returns a
list of pairs as (E, N), where E is the item, and N is the number of duplicates of E. In case
there is no duplicate of the item, then the item will simply be copied (signally) into the
result list. If the list is empty, then you must print the following error message
“Error!! List is empty – you cannot have an empty list”
Example
> encode_list 4 [1,1,2,3,3,3,3,4,4,5,6,6,6]
[(1,2), 2, (3,4), (4,2), 5, (6,3)]
(3 Marks)
6. The language game!
There are many language games around; one of the well-known ones is the Gibberish
language. Your task is to write a program that converts any written sentence into a
Gibberish sentence. We will not use all of the default Gibberish rules that have been
explained in the above article, as that looks a bit complicated. However, we will create our
own version using some simple rules, as follow:
• convert words that contain more than 4 letters only (spaces between words don’t
count!). Example : “eat a grab” => “eat a grab”.
• if a word begins with one of the five vowels [‘a’, ‘e’, ‘i’, ‘o’, ‘u’], add “idig” to the
beginning of the word (i.e., before the vowel). Example: “eating” => “idigeating”.
• if a word has any of the five vowels (but does not start with one), add “idig” after the
first vowel. Example: “restaurant” => ” reidigstaurant”.
• replace every ‘x’ letter with ‘eks’. Example “complex” => “coidigleeks”.
Remember, these rules should be applied to full sentences and not only single words. If
your program is able to only handle single words, then you will get only1 mark for this
question.
You can test your program with the following sentence:
>convert “I would like to have banana, fig and guava for lunch”
> “I woidiguld like to have baidignana, fig and guidigava for luidignch”
Create some other tests to make sure that your program works.
(3 Marks)
Additonal section of Question 6
– Write another function that remove stop words from any sentence, after converting it into
Gibberish. The list of stop words that your should remove are: ‘a’, ‘an’, ‘am’, ‘I’, ‘he’, ‘she’, ‘it’,
‘are’, ‘as’, ‘and’, ‘for’, ‘in’, ‘on’, ‘him’, ‘her’, ‘they’, ‘my’, ‘to’.
When we apply this, the results for the sentence provided above should be:
> converteWithStopWordsRemoved “I would like to have banana, fig and guava for
lunch”
> “woidiguld like have baidignana, fig guidigava luidignch”
(2 Marks)
If you like (and this is not required for this question) you improve your stop word removal function
bt adding more stop words to your program – see a comperhensive list of stop words here.
Note that some tasks may require importing external modules (libraries) such a Data.List
and System.Random. To import external libraries, you need to use the following command:
Import “module name”
See some examples here.
Submit your work on Stream by 7pm Thursday 13th April. Late submission will not be
considered (unless with approvals). You must submit your final work using the stream
submission system before the deadline.
You are expected to manage your source code, this includes making frequent backups. “The
cat ate my source code” is not a valid excuse for late or missing submissions. Consider
managing your code in a private repository (bitbucket or similar). Private is essential here to
avoid plagiarism, we reserve the right to deduct marks from your submission if it is public
Important Note: this is an individual assignment. You must not share your code with others,
or use other’s code (this will be plagiarism!). If we find two assignments that are copied from
each other, both assignments will receive Zero marks!