<div dir="ltr">putStr (show (polyEvaluate (coeffs) (read value :: Float) ))<br><div><br></div><div>Here ``polyEvaluate`` is inferred as a Show(able), However, ``<span style="line-height:1.5">polyEvaluate`` definition on the bottom returns a monad. (In haskell, meaning of return is quite different than imperative programming language).</span></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">It's encouraged to write pure functions without effects, don't use monad unless it's really necessary. rewrite ``polyEvaluate`` as below should make the example compile.</span></div><div><span style="line-height:1.5"><br></span></div><div>polyEvaluate (coeffs) x = map (\(a, b) -> a+b) . zip coeffs . iterate (+1) $ 0<br></div><div><span style="line-height:1.5"><br></span></div><div><span style="line-height:1.5">Thanks</span></div><div><span style="line-height:1.5">baojun</span></div></div><br><div class="gmail_quote"><div dir="ltr">On Thu, Apr 21, 2016 at 8:33 PM Eunsu Kim <<a href="mailto:wntuwntu@gmail.com">wntuwntu@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div style="word-wrap:break-word"><div>Hi</div><div><br></div><div>i have a problem in my code!</div><div><br></div><div>here is my code:</div><div><br></div><div><div>-- Baic I/O and Loop (50 Points)</div><div><br></div><div>evalpoly = do <span style="white-space:pre-wrap">        </span>putStr "What is the degree of polynomial: "</div><div><span style="white-space:pre-wrap">            </span>degree <- getLine</div><div><span style="white-space:pre-wrap">             </span>coeffs <- (funcOfCoeff ((read degree::Int)+1) [])</div><div><span style="white-space:pre-wrap">             </span>putStr "What value do you want to evaluate at: "</div><div><span style="white-space:pre-wrap">               </span>value <- getLine<span style="white-space:pre-wrap">     </span></div><div><span style="white-space:pre-wrap">         </span>putStr "The value of the polynomial is: "</div><div><span style="white-space:pre-wrap">              </span>putStr (show (polyEvaluate (coeffs) (read value :: Float) ))</div><div><span style="white-space:pre-wrap">             </span>putStr "\n"</div><div><br></div><div>--function loop to get coefficient--</div><div><br></div><div>funcOfCoeff 0 coeffs = do <span style="white-space:pre-wrap">             </span>--to check the degree of 0</div><div><span style="white-space:pre-wrap">                       </span>return coeffs<span style="white-space:pre-wrap">   </span>--return list of coefficient</div><div><br></div><div>funcOfCoeff degree coeffs = do <span style="white-space:pre-wrap">     </span></div><div><span style="white-space:pre-wrap">                 </span>putStr ("What is the x^" ++ show(degree-1))<span style="white-space:pre-wrap">   </span></div><div><span style="white-space:pre-wrap">                 </span>putStr " coefficient: "</div><div><span style="white-space:pre-wrap">                        </span>coeff <- getLine</div><div><span style="white-space:pre-wrap">                      </span>loop <- funcOfCoeff (degree-1) ((read coeff :: Int) : coeffs)</div><div><span style="white-space:pre-wrap">                 </span>return loop</div><div><br></div><div><br></div><div><span style="white-space:pre-wrap">                    </span></div><div>polyEvaluate (coeffs) x = do</div><div><span style="white-space:pre-wrap">                      </span>powers <- zip coeffs (iterate (+1) 0)</div><div><span style="white-space:pre-wrap">                 </span>result <- map (\(a,b)-> a+b) powers<span style="white-space:pre-wrap">       </span>—PROBLEM IS HERE!!!!</div><div><span style="white-space:pre-wrap">                   </span>return result</div><div><br></div><div><div>here is error message:</div><div><br></div><div><img height="152" width="375" src="cid:C6FEB6E1-8BDA-4837-AF14-F06EBE296C84"></div><div><br></div><div>in very bottom function (polyEvaluate), why is not working “result <- map (\(a,b) -> a+b) powers” ???</div><div><br></div><div>in Prelude, it is working</div><div><br></div><div><img height="46" width="285" src="cid:B2077ACB-2F62-4D6F-9ECA-92B1F7B0CB36"></div><div><br></div><div>Thanks!</div></div></div></div>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners" rel="noreferrer" target="_blank">http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners</a><br>
</blockquote></div>