i have questions about Haskell

Hi when outputting the polynomial value, actually write out the polynomial, but: - skipping any missing monomials - not including any extraneous signs -not showing the constant term for the above example, the final line would be: The value of 1.0 x^3 - 2.0 x^2 + 10.0 evaluated at -1.0 is 7.0 how can I do this??? i have no idea now…. here is my code: evalpoly = do putStr "What is the degree of polynomial: " degree <- getLine coeffs <- (funcOfCoeff ((read degree::Int)+1) [] ) putStr "What value do you want to evaluate at: " value <- getLine putStr "The value of the polynomial is: " putStrLn (show (getResult (coeffs) (read value :: Float) )) --function loop to get coefficient-- funcOfCoeff 0 coeffs = do --to check the degree of 0 return coeffs --return list of coefficient funcOfCoeff degree coeffs = do putStr ("What is the x^" ++ show(degree-1)) putStr " coefficient: " coeff <- getLine loop <- funcOfCoeff (degree-1) ((read coeff :: Float) : coeffs) return loop getResult (coeffs) x = sum(map(\(a,b) -> a*x^b).zip coeffs.iterate (+1)$0) this is my output so far:
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

Hi!
when outputting the polynomial value, actually write out the polynomial, but: - skipping any missing monomials - not including any extraneous signs -not showing the constant term
for the above example, the final line would be:
The value of 1.0 x^3 - 2.0 x^2 + 10.0 evaluated at -1.0 is 7.0
Because this is apparently a homework problem that you're supposed to solve yourself, I'll only give you a few pointers: What the problem description hints at is a function with type Polynomial -> String. So in a first step you should define that data type Polynomial (or whatever you want to call it). So far you seem to have been using a list of coefficients to represent polynomials, so you might just build on that existing representation, either by creating a type alias or creating a separate "real" type with a constructor. In a second step you need to write a function Polynomial -> String with an appropriate definition. I'd recommend that you start by generating a simpler string representation like "1.0 x^3 + -2.0 x^2 + 0.0 x^1 + 10.0" and refine your definition iteratively. Cheers, Simon
participants (2)
-
Eunsu Kim
-
Simon Jakobi