I have a function(eval
) to evaluate polynomial.
eval coeffs value = .......
coeffs
is list of coefficents.
value
is for x in math
Ex) to calculate 3x^2 + 2x + 1 with x=20
coeffs
is stored reverse order.
when I type coefficient like above, it is stored like this [1,2,3]
program working like this and last part is wrong (the value of the polynomial): here is my code
getCoeff 0 ls = do return ls
getCoeff n ls = do putStr "What is the x^"
putStr (show(n-1))
putStr " coefficient: "
v <- getLine
w <- getCoeff (n-1) ((read v :: Float):ls)
return w
evalpoly = do putStr "What is the degree of polynomial: "
v <- getLine
l <- (getCoeff ((read v :: Int)+1) [])
putStr "What value do you want to evaluate at: "
x <- getLine
putStr "The value of the polynomial is: "
putStr (show (polyEvaluate (l) (read x :: Float)))
putStr "\n"
eval [] x = 0.0
eval (l) x =
What is the degree of polynomial: 2
What is the x^2 coefficient: 3
What is the x^1 coefficient: 2
What is the x^0 coefficient: 1
What value do you want to evaluate at: 20
The value of the polynomial is 1241.0
how can I evaluate the polynomial with coefficient and x value?