<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 13, 2013 at 8:57 AM, Mark Fredrickson <span dir="ltr"><<a href="mailto:mark.m.fredrickson@gmail.com" target="_blank">mark.m.fredrickson@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Fri, Dec 13, 2013 at 8:59 AM, Antonio Nikishaev <span dir="ltr"><<a href="mailto:me@lelf.lu" target="_blank">me@lelf.lu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
<br>
<a href="http://hackage.haskell.org/package/NumInstances" target="_blank">http://hackage.haskell.org/package/NumInstances</a><br></blockquote><div><br>The previous exchange prompted me to whip up a Num instance for lists:<br>
<br> instance Num a => Num [a] where<br> negate = fmap negate<br> (+) = zipWith (+)<br> (*) = zipWith (+)<br> fromInteger x = [fromInteger x]<br> abs = fmap abs<br> signum = fmap signum<br>
<br>It mostly behaves how one would expect:<br><br> let a = [1,2,3]<br> ghci> let b = [4,5,6,7]<br> ghci> a + b<br> [5,7,9]<br> ghci> 1 + a<br> [2]<br> <br>I was wondering whey `1 + a` succeeds. At first I thought it could be the `fromInteger` definition, but this explanation were true, I should be able to add integers and doubles freely, which I can't: <br>
<br> ghci> fromInteger (1::Integer) + (1.0::Double)<br> 2.0<br> ghci> (1::Integer) + (1.0::Double)<br> <br> <interactive>:30:17:<br> Couldn't match expected type `Integer' with actual type `Double'<br>
In the second argument of `(+)', namely `(1.0 :: Double)'<br> ...<br><br>Thanks for enlightening me.</div></div></div></div></blockquote><div><br></div><div>`1 + a` works for the same reason `1 + (1.0::Double)` works: type inference. `1 + a` in this instance is equivalent to `(1 :: Num [Integer]) + a` which should compile down to something equivalent to `fromInteger 1 + a`.</div>
<div><br></div><div>-bob</div><div><br></div></div></div></div>