[Haskell-beginners] i have questions about Haskell

Eunsu Kim wntuwntu at gmail.com
Sat Apr 23 00:18:52 UTC 2016


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


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/beginners/attachments/20160422/3b8d20f9/attachment-0001.html>


More information about the Beginners mailing list