Define a recursive function


Lists & Functions Assignment

This assignment consists of three exercises. You are expected to submit a single file named a5.hs containing
• your name(s) and ID(s) (as comments at the top of the file),
• solutions for all exercises, include type definition for every function/value.
Test your final version of a5.hs files and make sure it compiles and has no syntax or logical errors.

Exercise 1

a) Define a recursive function hasD which takes a non-negative integer n and an integer digit d between 0 and 9 inclusive, and returns True if the base-10 representation of n contains the digit d; False otherwise. For example, hasD 3071 7 should evaluate to True, and hasD 1201 4 to False.

b) Define a recursive function count :: [ Int ] ->

Int ->

Int which,

given a list of non-negative integers intL and an integer digit a between 0 and 9 inclusive, returns the number of integers in intL which contain the digit a (again

when written in base 10). For example, count
to 2, and count [98,4,376] 2 to 0.

[102,41,256] 2 should evaluate

c) Re-write the function from point b define using list comprehension-call it countC.

Exercise 2

a) Write a recursive function takeOut::Int->[Int]->[Int] so that

takeOut m

xs removes all multiples of m from xs.

b) Re-write the function from point a using list comptrehension-call it takeOutB.

c) Write a recursive function called count3r that takes a list of triples of Ints as a parameter. The result should be the number of triples for which the last element is the sum of the first two elements. For example:
count3r [(6,-8,-2),( 2,4,6),(8,7,1),(4,5,9)] should return 3

and count3r

[(-1,3,1), (1,5,7)] should return 0

d) Re-write the function from point c) using list comprehension;call it count3L

Exercise 3 Write a Haskell script that will display some information about data stored in a file called DVDdata.hs The file DVDdata.hs contains a list of DVD movies and for each movie it contains its ID number, its title, whether it is available



(yes, no) and if it is overdue, how many days it is overdue. according to the movie ID values.
Here is a possible DVDdata.hs file:

The list is sorted

673_de.png

Your script should print a table with a) proper headings and
b) containing the following information for each DVD:

i. ii. iii. iv)

ID number title availability
if overdue - the required payment . The payment for overdue DVDs is computed by multiplying the number of overdue days by penaltyPerDay.

c) a footer containing the total number of available DVDs.
d) The list of DVDs should be now sorted on the title of the DVDs.
e) The output should be produced when the function printable (see picture below) is invoked at the prompt.

The output obtained from the above file should be like this:

628_d3.png

Be aware that the content of the file DVDdata.hs can be changed (for example more
DVDs can be included in the list), but the format of the information will be preserved.


Submit your solution electronically.

Important:
1. You can define and use other functions in order to solve a problem that asks you to define a function to perform a specific task.
2. Run your final version of a5.hs file on the computer labs and make sure it has no errors. Please note that if we cannot run your a5.hs script you will get 0 marks.

Solution Preview :

Prepared by a verified Expert
Data Structure & Algorithms: Define a recursive function
Reference No:- TGS01130708

Now Priced at $15 (50% Discount)

Recommended (96%)

Rated (4.8/5)