List Arithmetic Program - Haskell
$10-30 CAD
Paid on delivery
Assignment description
You will write a program that implements a custom data type called a CauchyList. A CauchyList is a standard list of integers that has some special arithmetic operations associated with it. Specifically, if a, b are two CauchyLists you are going to overload the operators +, -, *, etc. such that expressions like a + b, a – b, etc. can be evaluated.
The data type representing a CauchyList should contain two fields:
1. An integer which we will call p. All the arithmetic operations are performed mod p.
2. a list of integers called content. The elements of content are always kept in the range [0, p – 1].
For example, two CauchyLists a, b of length 10 and 6, respectively, with p = 31 are:
a = [17, 9, 22, 27, 28, 27, 15, 28, 24, 1], b = [12, 4, 7, 15, 13, 4].
You must implement the following operations for the CauchyList type:
• The + operator:
The sum of two CauchyLists is defined component-wise. That means if a, b are two CauchyLists then we define c = a + b by setting [login to view URL][i] = [login to view URL][i] + [login to view URL][i] where the addition is done mod p.
This method should be able to handle CauchyLists of different lengths. When adding two lists of length m and n, where m > n, the resulting list should be of length m and the higher elements of the shorter list is assumed to be zero. For example, for the above a, b we have a + b = [29, 13, 29, 11, 10, 0, 15, 28, 24, 1].
• The – operator:
Like the + operator. For example, for the above a, b we have a - b = [5, 5, 15, 12, 15, 23, 15, 28, 24, 1].
• The * operator:
Suppose that a, b are two CauchyLists. Then the product c = a*b is defined as:
c[i] = a[0] * b[i] + a[1] * b[i - 1] + a[2] * b[i - 2] + · · · + a[i] * b[0]
where we denoted [login to view URL][k] by a[k] for simplicity.
Remember, c[i] must be reduced mod p at the end. The product of two CauchyLists of lengths m and n is a CauchyList of length m + n - 1. So the index i in c[i] can be larger than the lengths of a and b. You should take care of this, again by assuming that the elements outside the bounds of a list are zero. In the example above, we have a * b = [18, 21, 16, 17, 24, 24, 2, 18, 18, 0, 27, 16, 5, 16, 4].
If the second argument is an integer, not a CauchyList, then this method does a scalar multiplication which is much simpler. That is, if b is an integer then c[i] = a[i] * b. This allows us to be able to, for example, evaluate an expression of the form a * 65.
• The == operator:
Two CauchyLists are equal if they have the same modulus value p and the same content component-wise.
• String representation:
a CauchyList should have a string value of the form
p: p
length: length
content: content
where length is the length of content. In the example above, a is printed as
p: 31
length: 10
content: [17, 9, 22, 27, 28, 27, 15, 28, 24, 1]
Operator overloading in Haskell
To overload the operators +, -, *, we made Pt an instance of the type class Num. The syntax to do so is as follows:
Instance Num Pt2 where
…
To overload the operator ==, we made Pt2 an instance of the type class Eq using the following:
Instance Eq Pt2 where
…
Note: instances of type class Num must implement abs, signum, and fromInteger. For abs and signum you can perform component-wise abs and signum just like in the Pt2 type. For fromInteger, however, you need implement it in a way as to satisfy the requirement on the * operator to correctly evaluate expressions such as a * 65 where a is a CauchyList
All your operations should be able to be linked together. I.e., your code should be able to evaluate expressions like the following: (a * b * b + b * c - a * 32) where a, b, c are all CauchyLists.
Submission & Evaluation
TO SUBMIT:
A Haskell source file named [login to view URL] that contains a module called Cauchy (i.e. module Cauchy where …). This module must contain all the implementation required to satisfy the requirements above, and it must load cleanly into ghci.
Project ID: #25118107
About the project
4 freelancers are bidding on average $23 for this job
I have been using Haskell for over ten years now, and I'm starting to get the hang of it. I was a math and English major and graduated from Caltech in 1975. I've been programming since then in Fortran, Forth, Perl, a More
I can do it for 1 hour. I already did the project in Haskell similar that. Please give me your project Regards Alexei
Hi ,Actually this is the first time that i work with this site , i saw this question so i try the implement this question in haskell than i send offer to you. Code is ready to send.