-1

I'm new at Haskell-programming and have to do some excercises for my functional development lecture at the university. I got the task to create a datatype, called "term" which represents a rational function. The second task is to create a method which differentiates the entered term.

We need a simple solution and constructors for: monomial, Addition, Multiplication, divison

Given:

 data Term = Monom (Int, Int) | Addition[Term] deriving Show
    diff :: Term -> Term
    diff (Monom(a, b)) = Monom(a*b, b-1)

Edit:

Calling the following function will return the corresponsing derivation:

diff (Monom(a,b)) = if b>0 then Monom(a*b, b-1) else Monom(0,0)

same for:

diff (Addition(x:xs)) = diff(x)

Can anyone tell me how to iterate over all elements in the entered list? The function above returns only the first derivated value..

I appreciate every help or hint!

MaMa
  • 31
  • 4
  • 3
    Well first you should think about **how you will represent a rational function**... – Willem Van Onsem May 18 '17 at 15:01
  • 2
    ....in such a way that it will contain all of the information required for easy differentiation.. – Eugene Sh. May 18 '17 at 15:03
  • You should really explain how your `Term` is supposed to represent a rational function; that's quite unclear. It looks more like a representation of a polynomial with integer coefficients. – dfeuer May 18 '17 at 20:00

2 Answers2

0

Haskell has an extremely expressive type system. Don't over think it! Just write a type that can simply capture the definition of a rational function. Since a rational function f(x) is a function that can be written in the form p(x)/q(x) for polynomials p(x) and q(x), all your rational function type needs to do is encompass is two polynomials:

data Polynomial = undefined -- your HW here

data RationalFunction = Polynomial Polynomial

-- or

data RationalFunction = { numerator :: Polynomial
                        , denominator :: Polynomial
                        }
SamTay
  • 31
  • 4
  • With the guidelines of our professor we got the following as far as now: data Term = Monom(Int, Int) | Addition[Term] deriving Show; diff :: Term -> Term diff (Monom(a, b)) = Monom(a*b, b-1) Our struggle is to add the function for the differentiation of a sum. Can you help us with that? – MaMa May 18 '17 at 15:18
  • I see. Your original question says you are tasked with creating a datatype, but your comment here indicates that the datatypes are already created, you are simply implementing the rest of the function `diff` so that it works with all values of type `Term`. I would update your original question, work out the actual logic for subtracting rational functions (or look it up), and then ask a more precise question regarding the Haskell syntax for how to implement it. – SamTay May 18 '17 at 15:31
0

You just need to add to the definition of diff. It has already been defined for one of the two constructors of the Term type, Monom. You now need to define it for the other constructor, Addition:

diff (Monom (a, b)) = Monom (a*b, b-1)
diff (Addition terms) = ???

Think about what the derivative of a polynomial is: the sum of the derivatives of the individual terms.

1) d/dx x^2 + x == 2*x + 1

Now think about how the above polynomial is represented as a Term value:

2 ) Addition [Monom (1,2), Monom (1, 1)] -- x^2 + x

And finally, how would the derivative be represented?

3) Addition [Monom(2, 1), Monom (1, 0)] -- 2*x + 1

Your task is to replace ??? in the definition of diff with code that can take something like 2) and produce something like 3).

chepner
  • 497,756
  • 71
  • 530
  • 681