haskellnoob
haskellnoob

Reputation: 211

Polynomial evaluation in haskell

I've been trying to make a function which evaluates polynomials of integers.

Now I have

> type Poly = [Int]

> polyEval :: [(Float,Int)] -> Float -> [(Float,Int)]
> polyEval [] _ = []
> polyEval ((c,g):xs) n = map (\ (c,g) -> (c*n,g)) xs

If I put in polyEval [] 3 the output is []. But when I want to calculate something like polyEval [3] 2, Hugs says

ERROR - Cannot infer instance
*** Instance   : Num (Float,Int)
*** Expression : polyEval [3] 2

Why?

Upvotes: 1

Views: 1470

Answers (1)

Ilya Rezvov
Ilya Rezvov

Reputation: 944

You promise to compiler put list of tuples (Float, Int) to polyEval function, but you put only 3. Haskell try cast 3 to (Float, Int), but it is impossible. If you call polyEval as polyEval [(1.0, 3)] 2 it will compile fine.

Another point - you make pattern matching on head of the list polyEval ((c,g):xs) and drop results in following computations.

Upvotes: 3

Related Questions