In haskell's Poly package polynomials like x**2 -1 are represented by [ -1, 0, 2] so for calculate the degree of a polynomial, I need to calculate the length of the polynomial list - 1.
This is what I have done:
polyDegree :: (Num a, Eq a) => Poly a -> Int
polyDegree p = rawPolyDegree (trim (0==) p)
rawPolyDegree :: Poly a -> Int
rawPolyDegree p = rawPolyLength p - 1
rawPolyLength :: Poly a -> Int
rawPolyLength (ListPoly _ _ cs) = length cs
rawPolyLength (VectorPoly _ _ cs) = V.length cs
rawPolyLength (UVectorPoly _ _ cs) = UV.length cs
I tried to compile this code. but doesn't work this code output this error
ghc -o main main.hs
[1 of 1] Compiling Main ( main.hs, main.o )
main.hs:1:32: error: Not in scope: type constructor or class `Poly'
|
1 | polyDegree :: (Num a, Eq a) => Poly a -> Int
| ^^^^
main.hs:4:18: error: Not in scope: type constructor or class `Poly'
|
4 | rawPolyDegree :: Poly a -> Int
| ^^^^
main.hs:7:18: error: Not in scope: type constructor or class `Poly'
|
7 | rawPolyLength :: Poly a -> Int
| ^^^^
main.hs:8:16: error: Not in scope: data constructor `ListPoly'
|
8 | rawPolyLength (ListPoly _ _ cs) = length cs
| ^^^^^^^^
main.hs:9:16: error: Not in scope: data constructor `VectorPoly'
|
9 | rawPolyLength (VectorPoly _ _ cs) = V.length cs
| ^^^^^^^^^^
main.hs:9:39: error:
Not in scope: `V.length'
No module named `V' is imported.
|
9 | rawPolyLength (VectorPoly _ _ cs) = V.length cs
| ^^^^^^^^
main.hs:10:16: error: Not in scope: data constructor `UVectorPoly'
|
10 | rawPolyLength (UVectorPoly _ _ cs) = UV.length cs
| ^^^^^^^^^^^
main.hs:10:38: error:
Not in scope: `UV.length'
No module named `UV' is imported.
|
10 | rawPolyLength (UVectorPoly _ _ cs) = UV.length cs
| ^^^^^^^^^
exit status 1
I researched here in stack overflow about problems compiling haskell function and I found a question about a simple algebra functions here I understand that my problem is that I have not instructed Haskell what the program should do the answer of this question is this:
main :: IO ()
main = print (f 2)
So accordingly to the question need to put this somewhere in my code but I don't know how where I have to put the values for this function?
I am studying with Programming in Haskell 2016 Graham Hutton 2nd Edition the book said is not important learn how to compile the snippets but I want to compile this snippets for check if my code is running. I am new in haskell, I am learning and interested in functional paradigm. I am using GHCi, version 8.6.5 Online Compiler for this.
CodePudding user response:
I haven’t read this book, but I get the impression that it doesn’t intend for you to use a third-party package here.
Instead, directly use a list. A list of type [k]
and length n can represent a polynomial with n coefficients of type k
, in order from least significant (x0) to most significant (x(n − 1)). Then one way of calculating the degree is like you said, simply the length of such a list less one:
degree :: [a] -> Int
degree p = length p - 1
And you can rephrase this by introducing your own type synonym:
type Poly a = [a]
degree :: Poly a -> Int
degree p = length p - 1
This solution has some bugs, for example:
What should happen if
degree
is given an empty list[]
of coefficients? What happens instead?What should
degree [1, 0, 0]
evaluate to? What happens instead? (This is why the code you copied also requires(Num a, Eq a)
.)
In the code you copied from the poly
package, functions like rawPolyLength
are operating on the library’s data representation, also named Poly
. It has a few different constructors for different representations (lists, boxed vectors, and unboxed vectors). You haven’t defined anything like that in your code. If you want to practice defining functions operating on lists, just use [a]
, or add type Poly a = [a]
and use your own Poly a
. You can use the functions of ListPoly
as a reference for how to define these same operations on plain lists, the code just won’t be identical.
As in other languages, you would use an external package like poly
if you wanted to use their implementation of polynomials without having to define them yourself. For learning Haskell, it’s advisable initially to stick to the base
package (Prelude
, Data.List
, &c.) to get comfortable with the core vocabulary of the language.