Chat with us, powered by LiveChat G51FUN Haskell Programming C.W.1 Exc3of5 | Writedemy

G51FUN Haskell Programming C.W.1 Exc3of5

G51FUN Haskell Programming C.W.1 Exc3of5

School of Computer Science, University of Nottingham

G51FUN Functional Programming, Spring 2012

Neil Sculthorpe

Coursework I, Part 3/5

Tuesday 14 February 2012

Deadline: Friday 24th February 2012, 4pm

This exercise sheet covers material from Lecture 4 (Defining Functions), and is worth 20% of the first coursework. Each question is worth a quarter of the marks.

You should attempt to complete the exercises in your own time, but if you get stuck you can ask for help during the lab sessions.

Assessment will be carried out by oral examination during the lab sessions (nothing needs to be handed in). When you have completed the exercises you should ask a tutor to examine your solution. The tutor will then ask you some questions to test your understanding. You will not receive any marks if you cannot explain your solution.

Note that tutors will only be available to assess your solution during the official lab session (Fridays, 2pm to 4pm, A32).

 

Ensure that your script type checks before submitting it to a tutor. If you are unable to successfully complete some of the definitions then comment out those that are incomplete or incorrect.

1. Simplify the following definitions as much as possible:

verbose :: Bool → Bool

verbose b = if b == True then True else False

complex :: x → y → (Int, y) complex x y = (fst p, snd q)

where p = head [(2, 3), (8, 4), (1, 5)]

q= (x , y)

2. The “exclusive or” operator takes two Booleans and returns a Boolean that is True when exactly one of its arguments is True, and False otherwise.

xor :: Bool → Bool → Bool

Give three definitions of the xor function:

(a) one using pattern matching;

(b) one using conditional expressions;

(c) and one using the / = operator.

Do not use any other library functions or operators. Name your functions xor1 , xor2 and xor3 . Try and simplify your definitions as much as possible.

3. The function third returns the third element in a list.

third :: [a ] → a

Give three definitions of the third function:

(a) one that uses the head and tail library functions;

(b) one that uses pattern matching;

(c) and one that uses list indexing (!!).

4. Bonus Exercise. (This question is optional, but will earn you additional marks if you complete it.)

The Luhn Algorithm is used to detect some simple errors when entering bank card numbers. The algorithm proceeds as follows:

i. Consider each digit as a separate number;

ii. Working from right to left, double every second number (i.e. starting with the penulti-mate digit);

iii. Subtract 9 from each number that is greater than 9;

iv. Add all the numbers together;

v. If the result is divisible by 10, then no error has been detected.

Try it on your own (16-digit) bank card and see!

Your task is to encode the Luhn Algorithm in Haskell. This can be divided into two steps:

(a) Define a function that doubles an integer and subtracts nine if the result is greater than nine:

luhnDouble :: Int → Int

(b) Using luhnDouble and the mod library function, define a function luhn that checks if a bank card number is valid. For simplicity, only consider (mini) bank card numbers that are four digits long.

luhn :: Int → Int → Int → Int → Bool

For example, 1784 is a valid mini bank card number, whereas 4783 is not.

Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.

Do you need an answer to this or any other questions?

About Writedemy

We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.

How It Works

To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.

Are there Discounts?

All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.

Hire a tutor today CLICK HERE to make your first order