In Haskell: Write a function, evalpoly, that will ask a user for the degree of a
ID: 3685721 • Letter: I
Question
In Haskell:
Write a function, evalpoly, that will ask a user for the degree of a single variable polynomial, then read in the coefficients for the polynomial (from highest power to lowest), then for a value, and will output the value of the polynomial evaluated at that value. As an example run:
> evalpoly
What is the degree of the polynomial: 3
What is the x^3 coefficient: 1.0
What is the x^2 coefficient: - 2.0
What is the x^1 coefficient: 0
What is the x^0 coefficient: 10.0
What value do you want to evaluate at: -1.0
The value of the polynomial is 7.0
Explanation / Answer
import Control.Monad.Cont
evalPoly :: (Integral a)
print "What is the degree of the polynomial: "
degree <- getLine
temp <-degree
total <-0
tempTotal <- 1
forM_ [0..degree :: Int] $ i -> do
print "what is x^"++temp++" coefficient: "
input <- getLine
tempTotal <- 1
forM_ [1..temp :: Int] $ j -> do
tempTotal <- tempTotal * input
temp <- temp-1
total <- total + tempTotal
print "Total result is "++total
main = do
evalPoly()
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.