Question

As usuall I am suffering the problem, that on paper everything should work, but the compiler gives an error which I just cannot understand.

data Polynom = Polynom[Double] 
           deriving Show



calcPolynom :: Double -> Polynom -> Double
calcPolynom _ [] = 0.0
calcPolynom _ Polynom [] = 0.0
calcPolynom n Polynom[h:ts] = h + n* Polynom[ts]

giving the error "Constructor `Polynom' should have 1 argument, but has been given none". But I did and neet it to be defined also for empty polynoms.

Can anybody help? I am really new to haskell and i really apreciate any sort of help :)

Additional information: I want polynomes to be a list of Doubles i.e. 2x³ - 1.4x² + 2.5 = [2.5,0,-1.4,2]

Was it helpful?

Solution

You just need some parentheses and to fix your pattern matching:

calcPolynom _ (Polynom []) = 0.0
calcPolynom n (Polynom (h:ts)) = h + n * ???

I left a blank for you to fill in at the ???, you'll have to figure out what should go there instead of what you have right now. The reason why is that you can't multiply a value of type Double with a value of type Polynom, but you could write this recursively (I'm assuming that was your original intention).

When you have code like

calcPolynom n Polynom [h:ts] = ...

The compiler sees that calcPolynom has 3 arguments and Polynom has none, you use parentheses for grouping.

OTHER TIPS

try

calcPolynom _ (Polynom []) = 0.0
calcPolynom n (Polynom (h:ts)) = h + n * calcPolynom n (Polynom ts)

When you try to pattern-match a constructor, you must enclose it in parenthesis. This not only applies to you Polynom data type, but also to lists. [h:ts] does not something different than you expect, it matches against a list of lists ([[?]])with only one element (i.e. list). Finally, you have to recurse the function, not the data type.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top