[Haskell-beginners] I have simple question about Haskell

Eunsu Kim wntuwntu at gmail.com
Sat Apr 23 03:55:48 UTC 2016


Hi

THIS 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 "
		putStr (printChar coeffs)	
		putStr (" evaluated at "++ value ++" is ")	
		putStrLn (show (getResult (coeffs) (read value :: Float) ))

printChar coeffs =parser([x | x <-reverse (zip coeffs (iterate (+1) 0)), fst x /= 0])		

parser []=""
parser (a:as)=show(fst a) ++ "x^" ++show(snd a) ++ " " ++(parser as)    —PROBLEM IS HERE!!!

---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) --evaluate polynomial with value

HERE IS MY OUTPUT:

*Main> evalpoly
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: 7
the value of 3.0x^2 2.0x^1 1.0x^0  evaluated at 7 is 162.0

in output, on the very bottom part, there should be + or - sign there like this: 3.0x^2 + 2.0x^1 - 1.0x^0  
what should I do? I have no idea now…..

thanks

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


More information about the Beginners mailing list