CS计算机代考程序代写 Haskell COMPSCI 1JC3 Introduction to Computational Thinking Spring 2021

COMPSCI 1JC3 Introduction to Computational Thinking Spring 2021
Assignment 4
Dr. William M. Farmer and Curtis D’Alves McMaster University
Revised: May 3, 2021
The requirements for Assignment 4 are given below. Please submit As- signment 4 as a single file, Assign 4.hs, to the Assignment 4 folder on Avenue under Assessments/Assignments. Assignment 4 is due Sunday, June 13, 2021 before midnight. Assignment 4 is worth 10% of your final grade.
Although you are allowed to receive help from the instructional staff and other students, your submitted program must be your own work. Copying will be treated as academic dishonesty!
1 Assignment 4
The purpose of this assignment is to create a Haskell module that imple- ments a derivative calculator that performs minor simplification and does no domain analysis. Derivative calculators on the web usually do major simplification but completely ignore domain analysis.
1.1 Background
For the purposes of this assignment, let a mathematical expression be an expression that is constructed from an indeterminant x and members of R by applying addition (+), multiplication (∗), power (^), cosine (cos), sine (sin), and absolute value (abs). Let M be a set of mathematical expressions. The value of a mathematical expression u ∈ M at r ∈ R is the result of replacing the indeterminant x in u with r. For example, the value of
(x+1)∗(x−1) 5∗x
at 3 is
(3+1)∗(3−1) = 8 .
5∗3 15
It is usually advantageous to simplify, when possible, a mathematical
expression u to another mathematical expression v such that u and v have 1

the same values for all r ∈ R. The following rules show, for example, how mathematical expressions can be simplified:
1. 0+u=u+0=u.
2. 0∗u=u∗0=0.
3. 1∗u=u∗1=u.
4. u+(v+w)=(u+v)+w=u+v+w. 5. u∗(v∗w)=(u∗v)∗w=u∗v∗w.
6. u∗(v+w)=u∗v+u∗w. 7. (u+v)∗w=u∗w+v∗w. 8. u0 =1.
9. abs(abs(x)) = abs(x).
A mathematical expression can be symbolically differentiated by apply- ing the following standard differentiation rules (expressed in Leibniz nota- tion):
Variable Rule
d (x) = 1 dx
Constant Rule
d(r)=0 wherer∈R dx
Sum Rule
d (u+v)= d (u)+ d (v) dx dx dx
Product Rule
d (u∗v)= d (u)∗v+u∗ d (v) dx dx dx
Power Rule
d(un)=n∗un−1∗ d(u) dx dx
Cosine Rule
d (cos(u)) = −sin(u) ∗ d (u) dx dx
2

Sine Rule
d (sin(u)) = cos(u) ∗ d (u) dx dx
Absolute Value Rule
d|u|= u ∗ d(u) dx |u| dx
Derivative calculators on the web usually assume that whenever u is
a mathematical expression, the derivative of a function f(x) = u is the
function f′(x) = d (u). This is not always the case: For example, if f(x) = dx
u=|x|,then d (u)= x butf′(x)= x onlyifx̸=0andf′(x)isundefined dx |x| |x|
if x = 0. For this reason, a proper derivative calculator should do domain analysis and thus, given a mathematical expression u as input, return:
1. d (u) in simplified form. dx
2. The domain D ⊆ R on which the derivative is defined.
So for our example, given |x| as input, a derivative calculator should return:
1. x. |x|
2. {x∈R|x̸=0}. 1.2 Requirements
1. Download from Avenue Assign4 Project Template.zip which con- tains the Stack project files for this assignment. Modify the Assign 4.hs file in the src folder so that the following requirements are satisfied. Also put your testing code for this assignment in the Assign 4 Test.hs file in the test folder.
2. Your name, the date, and “Assignment 4” are in comments at the top of your file. macid is defined to be your MacID.
3. The file contains the following algebraic data type definition:
data MathExpr a = X
| Coef a
| Sum (MathExpr a) (MathExpr a)
| Prod (MathExpr a) (MathExpr a)
| Power (MathExpr a) Int
| Cos (MathExpr a)
| Sin (MathExpr a)
| Abs (MathExpr a) deriving (Eq,Show,Read)
3

4. The file includes a function named eval of type (Floating a, Eq a) => MathExpr a -> a -> a
such that, if e is an expression of type (MathExpr a) and v is a floating point number, eval e v is the value of e at v (i.e. subbing v for X and evaluating).
5. The file includes an instance Num a => Num (MathExpr a) with im- plementations for the following methods (leave the other class methods undefined, i.e. produce an error).
(+):: Numa=>a->a->a
(*):: Numa=>a->a->a negate:: Numa=>a->a
abs:: Numa=>a->a
fromInteger :: Num a => Integer -> a
6. ThefileincludesaninstanceFractional a => Fractional (MathExpr a) with implementations for the following methods (leave the other class methods undefined, i.e. produce an error).
recip :: Fractional a => a -> a
fromRational :: Fractional a => Rational -> a
7. The file includes an instance Floating a => Floating (MathExpr a) with implementations for the following methods (leave the other class methods undefined, i.e. produce an error).
pi :: Floating a => a
sin :: Floating a => a -> a
cos :: Floating a => a -> a
8. The file includes a function named diff of type
(Floating a, Eq a) => MathExpr a -> MathExpr a
such that, if u is an expression of type (MathExpr a), diff u is the result of symbolically differentiating u using the differential rules given above.
4

9. The file includes a function named pretty of type (Show a) => MathExpr a -> String
such that, if u is an expression of type (MathExpr a), pretty u will create a String representation of u. Each constructor will result in the following String representations:
X
“X”
Coef c
“c”
Sum u0 u1
“(u0 + u1)”
Prod u0 u1
“(u0 * u1)”
Power u0 d
“(u0 ^ d)”
Cos u0
“cos(u0)”
Sin u0
“sin(u0)”
Abs u0
“abs(u0)”
Here u0 and u1 are subexpressions that also need to be “pretty” eval- uated and c and d are Num and Int values, respectively, that can be turned into a String value with show. Note: You should be able to copy the output of pretty and “re-enter” it into GHCi, for example:
*Assign_4> pretty (X + 2 * X)
“(X + (2 * X))”
*Assign_4> (X + (2 * X))
Sum X (Prod (Coef 2) X)
10. Your file can be imported into GHCi and all of your functions perform correctly.
5