Error with floats in implementation of Horner's method.

Hi all, I have implemented Horner's method for evaluating a polynomial as follows: -- the nth horner element, given a list of coefficients and a value at which it needs to be evaluated horner_element l x n | n == (length l)-1 = last l | otherwise = (l !! n) + (horner_element l x (n+1))*x -- compute a polynomial given as a list of coefficients at the value x using horner's method. horner l x = horner_element l x 0

And I have sent the email before it was finished, oops. Evaluating at integer values works fine, but trying to do *Main> let a = [-1,2,-6,2] *Main> horner a 1.23 gives this error <interactive>:4:10: No instance for (Fractional Integer) arising from the literal `1.23' Possible fix: add an instance declaration for (Fractional Integer) In the second argument of `horner', namely `1.23' In the expression: horner a 1.23 In an equation for `it': it = horner a 1.23 I know it has something to do with types, but whenever I try to add a type signature to the functions it just messes everything up even more. Any help would be appreciated Thanks, Andrew On Fri, Dec 13, 2013 at 4:03 PM, Andrew Fleckenstein < andrew.fleckenstein@gmail.com> wrote:
Hi all,
I have implemented Horner's method for evaluating a polynomial as follows:
-- the nth horner element, given a list of coefficients and a value at which it needs to be evaluated
horner_element l x n | n == (length l)-1 = last l | otherwise = (l !! n) + (horner_element l x (n+1))*x
-- compute a polynomial given as a list of coefficients at the value x using horner's method.
horner l x = horner_element l x 0

On Sat, Dec 14, 2013 at 4:07 AM, Andrew Fleckenstein < andrew.fleckenstein@gmail.com> wrote:
I know it has something to do with types,
Right! :)
but whenever I try to add a type signature to the functions it just messes everything up even more.
This works: let a :: [Double]; a = [-1,2,-6,2] This works too: let a :: Num a => [a]; a = [-1,2,-6,2] What do you think are the pros and cons of either? -- Kim-Ee

I know Num is a class, and Double is an instance of the Num class. I guess
using Num would make it more generic, but I don't see why either one would
be better in this particular function.
On Fri, Dec 13, 2013 at 4:20 PM, Kim-Ee Yeoh
On Sat, Dec 14, 2013 at 4:07 AM, Andrew Fleckenstein < andrew.fleckenstein@gmail.com> wrote:
I know it has something to do with types,
Right! :)
but whenever I try to add a type signature to the functions it just messes everything up even more.
This works: let a :: [Double]; a = [-1,2,-6,2]
This works too: let a :: Num a => [a]; a = [-1,2,-6,2]
What do you think are the pros and cons of either?
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

Right. The problem is that when you write let a = [1,2,3,4] at the GHCi prompt, the Evil Monomorphism Restriction forces it to have a monomorphic type, and GHCi picks [Integer]. I recommend that you turn off the Monomorphism Restriction, which you can do in one of the following ways: 1) Type :set -XNoMonomorphismRestriction at the GHCi prompt 2) Add :set -XNoMonomorphismRestriction to your ~/.ghci file. If you do the latter then you only have to do it once, and from then on the MR will always be turned off in GHCi. With the MR turned off, let cs = [1,2,3,4] will result in cs :: Num a => [a] which will work the way you expect. -Brent On Fri, Dec 13, 2013 at 04:51:25PM -0500, Andrew Fleckenstein wrote:
I know Num is a class, and Double is an instance of the Num class. I guess using Num would make it more generic, but I don't see why either one would be better in this particular function.
On Fri, Dec 13, 2013 at 4:20 PM, Kim-Ee Yeoh
wrote: On Sat, Dec 14, 2013 at 4:07 AM, Andrew Fleckenstein < andrew.fleckenstein@gmail.com> wrote:
I know it has something to do with types,
Right! :)
but whenever I try to add a type signature to the functions it just messes everything up even more.
This works: let a :: [Double]; a = [-1,2,-6,2]
This works too: let a :: Num a => [a]; a = [-1,2,-6,2]
What do you think are the pros and cons of either?
-- Kim-Ee
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners
participants (3)
-
Andrew Fleckenstein
-
Brent Yorgey
-
Kim-Ee Yeoh